百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jsonapi-datastore

> 编程语言
开源

JavaScript 客户端 JSON API 数据处理变得更加简单。

199 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

JavaScript 客户端 JSON API 数据处理变得更加简单。

jsonapi-datastore

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.

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.

Parsing with meta data

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

javascript
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.

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

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

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

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 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:

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

so that you can use it as follows:

javascript
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!

Issues· 14 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptjson-api

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言