jsonapi-datastore

Lightweight library for reading, serializing, and synchronizing relational JSON API data.
Homepage:
1 person uses it
Author: 2215778?v=3 beauby

jsonapi-datastore

Build Status

JavaScript client-side JSON API data handling made easy.

Current version is v0.2.0-alpha. It is still a work in progress, but should do what it says.

Description

The JSONAPI standard is great for exchanging data (which is its purpose), but the format is not ideal to work directly with in an application. jsonapi-datastore is a JavaScript framework-agnostic library (but an AngularJS version is provided for convenience) that takes away the burden of handling JSONAPI data on the client side.

What it does: - read JSONAPI payloads, - rebuild the underlying data graph, - allows you to query models and access their relationships directly, - create new models, - serialize models for creation/update.

What it does not do: - make requests to your API. You design your endpoints URLs, the way you handle authentication, caching, etc. is totally up to you.

Installing

Install jsonapi-datastore by running: $ bower install jsonapi-datastore

Parsing data

Just call the .sync() method of your store. javascript var store = new JsonApiDataStore(); store.sync(data); This parses the data and incorporates it in the store, taking care of already existing records (by updating them) and relationships.

Retrieving models

Just call the .find(type, id) method of your store. javascript var article = store.find('article', 123); All the attributes and relationships are accessible through the model as object properties. javascript console.log(article.author.name); In case a related resource has not been fetched yet (either as a primary resource or as an included resource), the corresponding property on the model will contain only the type and id (and the ._placeHolder property will be set to true). However, the models are updated in place, so you can fetch a related resource later, and your data will remain consistent.

Serializing data

Just call the .serialize() method on the model. javascript console.log(article.serialize());

Examples

Documentation

See DOCUMENTATION.md.

What's missing

Currently, the store does not handle links and meta attributes.

Notes

AngularJS

jsonapi-datastore is bundled with an AngularJs wrapper. Just include ng-jsonapi-datastore.js in your index.html and require the module beauby.jsonApiDataStore in your application. You can then use the JsonApiDataStore factory, which is essentially defined as follows: javascript { store: new JsonApiDataStore(), Model: JsonApiDataStoreModel } so that you can use it as follows:

Contributing

All pull-requests welcome!

comments powered by Disqus
This page was last updated about 2 years ago.