Introduction to Data Modeling with JSData
Inspired by Ember Data, JSData is a framework-agnostic, datastore-agnostic ORM for Node.js and the Browser.
JSData’s adapters handle communication with various storage layers, such as localStorage
, Firebase, RethinkDB, or your RESTful backend.
In a typical scenario, you load data into the store, which maintains a single representation of every unique record coming from your storage layer. The data store offers an API for read, update, and delete operations, which are executed in your storage layer by an adapter, with the results finally synced back to the store. This is your Data or Model layer.
The Model layer is typically where your business logic resides–where you manipulate your data. There are many variations on this pattern, and JSData can work with your preferences.
JSData runs in the browser, communicating with storage layers such as localStorage
, Firebase, your RESTful backend (HTTP target), etc.
JSData also runs in NodeJS, where adapters for MongoDB, Redis, RethinkDB, MySql/Postgres/SQLite, etc. are available.
JSData presents a uniform API for executing your typical CRUD operations against any storage layer for which an adapter is available. You can easily combine adapters for more complicated data management.
It is easy to add JSData to your project. Let’s get started:
Install
<npm|bower> install --save js-data
JSData is available on cdnjs and jsDelivr.
You can add JSData to your app as a script tag, via import JSData from 'js-data'
, require('js-data')
, define(['js-data'], ...)
, etc.
Create a store
The DS
constructor function takes an optional options
object which can be used to override the default settings for your new store. You can also create a store via JSData.createStore([options])
.
Connect to storage
We’re not actually working with any storage layers yet, so let’s register an adapter.
<npm|bower> install --save js-data-firebase
js-data-firebase is available on cdnjs and jsDelivr.
You can add js-data-firebase to your app as a script tag, via import DSFirebaseAdapter from 'js-data-firebase'
, require('js-data-firebase')
, define(['js-data', 'js-data-firebase'], ...)
, etc.
Model your data
You start modeling your data by registering Resources with the store:
A JSData Resource defines metadata that the store uses to interact with data of that type. The Resource object returned by DS#defineResource(options)
exposes an API for working directly with data of that Resource.
With a few lines of code, we can already do all kinds of things with data of type User
.
Create a user in Firebase
Update the user
Destroy the user
The tip of the iceberg
I hardly had to write an code, and I already have a fully functional CRUD API on top of Firebase! I can easily add more resources, setup relations between them, and even add another adapter for a more complex application.
You know when you show a treat to a starving (but obedient) puppy, and the puppy just starts drooling all over the floor? That puppy is you right now.
JSData will save you time. It’s the Twitter Bootstrap of data layers.
Knock yourself out: