All Projects → beauby → Jsonapi Datastore

beauby / Jsonapi Datastore

Licence: mit
JavaScript client-side JSON API data handling made easy.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jsonapi Datastore

Ember Jsonapi Resources
Lightweight persistence for an Ember CLI app following the JSON API 1.0 spec
Stars: ✭ 127 (-33.16%)
Mutual labels:  json-api
Kitsu Server
🚂 Rails API server for Kitsu
Stars: ✭ 145 (-23.68%)
Mutual labels:  json-api
Aimeos Typo3
TYPO3 e-commerce extension for professional, ultra fast online shops, complex B2B applications and #gigacommerce
Stars: ✭ 157 (-17.37%)
Mutual labels:  json-api
Jsonapi Authorization
Authorization for JSONAPI::Resource
Stars: ✭ 130 (-31.58%)
Mutual labels:  json-api
Bricks
A standard library for microservices.
Stars: ✭ 142 (-25.26%)
Mutual labels:  json-api
Yang
The efficient and elegant, PSR-7 compliant JSON:API 1.1 client library for PHP
Stars: ✭ 148 (-22.11%)
Mutual labels:  json-api
Contenta vue nuxt
Start in minutes a Drupal 8 with JSON API and Vue.js : a Nuxt.js ( Vue.js SSR ) consumer for Contenta CMS
Stars: ✭ 125 (-34.21%)
Mutual labels:  json-api
Kitsu
🦊 A simple, lightweight & framework agnostic JSON:API client
Stars: ✭ 166 (-12.63%)
Mutual labels:  json-api
Jsona
Data formatter that creates simplified objects from JSON or stored reduxObject, creates JSON from the same simplified objects (in according with JSON API specification)
Stars: ✭ 144 (-24.21%)
Mutual labels:  json-api
Nginx upstream module
Tarantool NginX upstream module (REST, JSON API, websockets, load balancing)
Stars: ✭ 157 (-17.37%)
Mutual labels:  json-api
Frequest
FRequest - A fast, lightweight and opensource desktop application to make HTTP(s) requests
Stars: ✭ 130 (-31.58%)
Mutual labels:  json-api
Yii2 Json Api
Implementation of JSON API specification for the Yii framework
Stars: ✭ 139 (-26.84%)
Mutual labels:  json-api
Coloquent
Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord-like (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects.
Stars: ✭ 149 (-21.58%)
Mutual labels:  json-api
Katharsis Framework
Katharsis adds powerful layer for RESTful endpoints providing implementenation of JSON:API standard
Stars: ✭ 129 (-32.11%)
Mutual labels:  json-api
Json Api Client
A PHP package for mapping remote {json:api} resources to Eloquent like models and collections.
Stars: ✭ 159 (-16.32%)
Mutual labels:  json-api
Arduinosim800l
Arduino HTTP & FTP client for SIM800L/SIM800 boards to perform GET and POST requests to a JSON API as well as FTP uploads.
Stars: ✭ 127 (-33.16%)
Mutual labels:  json-api
Weld
Full fake REST API generator written with Rust
Stars: ✭ 146 (-23.16%)
Mutual labels:  json-api
Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (-10%)
Mutual labels:  json-api
Tokenbalance
Simple Ethereum API to get your ERC20 Token Balance along with useful information
Stars: ✭ 163 (-14.21%)
Mutual labels:  json-api
Rodauth Rails
Rails integration for Rodauth authentication framework
Stars: ✭ 150 (-21.05%)
Mutual labels:  json-api

jsonapi-datastore

Build Status

JavaScript client-side JSON API data handling made easy.

Current version is v0.4.0-beta. 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 with bower by running:

$ bower install jsonapi-datastore

or with npm by running:

$ npm install jsonapi-datastore

Parsing data

Just call the .sync() method of your store.

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.

Parsing with meta data

If you have meta data in your payload use the .syncWithMeta method of your store.

var store = new JsonApiDataStore();
store.syncWithMeta(data);

This does everything that .sync() does, but returns an object with data and meta split.

Retrieving models

Just call the .find(type, id) method of your store.

var article = store.find('article', 123);

or call the .findAll(type) method of your store to get all the models of that type.

var articles = store.findAll('article');

All the attributes and relationships are accessible through the model as object properties.

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.

console.log(article.serialize());

Examples

// Create a store:
var store = new JsonApiDataStore();

// Then, given the following payload, containing two `articles`, with a related `user` who is the author of both:
var payload = {
  data: [{
    type: 'article',
    id: 1337,
    attributes: {
      title: 'Cool article'
    },
    relationships: {
      author: {
        data: {
          type: 'user',
          id: 1
        }
      }
    }
  }, {
    type: 'article',
    id: 300,
    attributes: {
      title: 'Even cooler article'
    },
    relationships: {
      author: {
        data: {
          type: 'user',
          id: 1
        }
      }
    }
  }]
};

// we can sync it:
var articles = store.sync(payload);

// which will return the list of synced articles.

// Later, we can retrieve one of those:
var article = store.find('article', 1337);

// If the author resource has not been synced yet, we can only access its id and its type:
console.log(article.author);
// { id: 1, _type: 'article' }

// If we do sync the author resource later:
var authorPayload = {
  data: {
    type: 'user',
    id: 1,
    attributes: {
      name: 'Lucas'
    }
  }
};

store.sync(authorPayload);

// we can then access the author's name through our old `article` reference:
console.log(article.author.name);
// 'Lucas'

// We can also serialize any whole model in a JSONAPI-compliant way:
console.log(article.serialize());
// ...
// or just a subset of its attributes/relationships:
console.log(article.serialize({ attributes: ['title'], relationships: []}));
// ...

Documentation

See DOCUMENTATION.md.

What's missing

Currently, the store does not handle links attributes or resource-level or relationship-level meta.

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:

{
  store: new JsonApiDataStore(),
  Model: JsonApiDataStoreModel
}

so that you can use it as follows:

angular
  .module('myApp')
  .controller('myController', function(JsonApiDataStore) {
    var article = JsonApiDataStore.store.find('article', 1337);
    var newArticle = new JsonApiDataStore.Model('article');
    newArticle.setAttribute('title', 'My cool article');
    console.log(newArticle.serialize());
  });

Contributing

All pull-requests welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].