DailyJS

JSData

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

data localStorage rest cache

JSData

Posted by Alex R. Young on .
Featured

data localStorage rest cache

JSData

Posted by Alex R. Young on .

Asaf Katz sent in JSData (GitHub: js-data/js-data, License: MIT, npm: js-data, Bower: js-data) by Jason Dobry, a model layer for caching data. It supports adapters for sending data to persistence layers, so you can install DSHttpAdapter (npm/Bower: js-data-http) to send data to a REST server or DSLocalStorageAdapter (npm/Bower: js-data-localstorage) for browser-based storage. There are even adapters for RethinkDB, Firebase, and Redis.

One nice thing about JSData is it uses Object.observe to watch for changes to data, so it doesn't need any special getters or setters. It also supports promises, so you can use then after finding or inserting data.

It also has more advanced ORM-style features like relations, computed properties, lifecycle methods, and data validation. The schema and validation API isn't based on JSON-Schema, but it has the kinds of features you've probably seen in other ORMs and ODMs like Mongoose:

var store = new JSData.DS();

var User = store.defineResource({  
  name: 'user',
  schema: {
    id: {
      type: 'string'
    },
    name: {
      type: 'string',
      maxLength: 255
    },
    email: {
      type: 'string',
      nullable: false
    }
  }
});

User.create({  
  name: 'John'
}).catch(function(err) {
  // Email errors will be in err.email.errors
});

This is an example I adapted from the Schemata and Validation documentation.

JSData was originally written for AngularJS, but is now framework-agnostic. However, there is an Angular integration guide which shows how it can be used with Angular applications.

Because JSData can be used with Node and browsers, you could use it to define reusable models for single-page apps that sync when the server is available. It'll also work well if you're used to Rails-inspired frameworks like Ember, and you're used to working with fat models that include data validation and lifecycle methods.