plwalters / staurelia-wars

Aurelia using the SWAPI.co API
MIT License
2 stars 0 forks source link

Add breeze.js for caching #1

Open plwalters opened 9 years ago

plwalters commented 9 years ago

Would like to add Breeze.js to manage caching and use as a fun aurelia sample but the data structure isn't typical of relational data. Any ideas @wardbell ?

  1. There is no unique identifier (except for a url which appears to be unique)
  2. All of the arrays are url references to other objects.
  3. There are no identifiers showing relations such as species_id

I could add a json results adapter but it feels like it could get unwieldy quickly and might be not the best for a community sample.

plwalters commented 9 years ago

I think I'd have to wire up the parent ids in the json results adapter if I chose to go that route.

wardbell commented 9 years ago

If you show me a JSON data sample of a few related "entities", I might have some ideas. What are these entities anyway?

plwalters commented 9 years ago

It's the swapi.co API serving Star Wars data :)

{
    "birth_year": "19 BBY",
    "eye_color": "Blue",
    "films": [
        "http://swapi.co/api/films/1/",
        ...
    ],
    "gender": "Male",
    "hair_color": "Blond",
    "height": "172",
    "homeworld": "http://swapi.co/api/planets/1/",
    "mass": "77",
    "name": "Luke Skywalker",
    "skin_color": "Fair",
    "created": "2014-12-09T13:50:51.644000Z",
    "edited": "2014-12-10T13:52:43.172000Z",
    "species": [
        "http://swapi.co/api/species/1/"
    ],
    "starships": [
        "http://swapi.co/api/starships/12/",
        ...
    ],
    "url": "http://swapi.co/api/people/1/",
    "vehicles": [
        "http://swapi.co/api/vehicles/14/"
        ...
    ]
}

which might have some related starships -

{
    "MGLT": "10 MGLT",
    "cargo_capacity": "1000000000000",
    "consumables": "3 years",
    "cost_in_credits": "1000000000000",
    "created": "2014-12-10T16:36:50.509000Z",
    "crew": "342953",
    "edited": "2014-12-10T16:36:50.509000Z",
    "hyperdrive_rating": "4.0",
    "length": "120000",
    "manufacturer": "Imperial Department of Military Research, Sienar Fleet Systems",
    "max_atmosphering_speed": "n/a",
    "model": "DS-1 Orbital Battle Station",
    "name": "Death Star",
    "passengers": "843342",
    "films": [
        "http://swapi.co/api/films/1/"
    ],
    "pilots": [],
    "starship_class": "Deep Space Mobile Battlestation",
    "url": "http://swapi.co/api/starships/9/"
}
plwalters commented 9 years ago

The issue I see with why this won't work with Breeze.js is because the references are backwards to the traditional references. The parent references it's children instead of the other way around.

wardbell commented 9 years ago

I'm cool that we're not dealing with a relational source. Many sources aren't.

I suspect you're looking at a read-only source. If not, talk to me and we can apply a variation of the Abstract REST Adapter in Breeze Labs.

As for modeling, I think I know what I'd do:

  1. Create array properties for the URL strings with a 'Url' extension (filmsUrl, speciesUrl, etc).
  2. Write a JsonResultsAdapter that morphed nodes to the actual JSON field names. This could be driven by custom metadata or, perhaps better, driven by property naming conventions (e.g., metadata property names ending in 'Url').
  3. Define EntityTypes for the corresponding objects that I'll be using in my demo (e.g. Film, Species).
  4. Add an Object.definedProperty for each "navigation" (xxx from set {film, species, ...}) that uses the xxxUrl array to deliver instances of the related xxx entity from Breeze cache. Again this could be driven by convention.
  5. Think about how i wanted to populate the cache with those related entities. One trick might be to have the xxx property itself populate the result async ... a kind of lazy load. Breeze doesn't do lazy loading (for good reasons) so this might seem weird; could fall back to an "eager fetch" or a "fetch-on-demand" approach.

All of this is fun speculation. It depends upon what you're really trying to achieve and whether you think we'd be demonstrating value with these kinds of games.