![Codacy](https://img.shields.io/codacy/88b55f71c45a47838d24ed1e5fd2476c.svg)
js-data is a framework-agnostic, datastore-agnostic ORM/ODM for Node.js and the
Browser.
Adapters allow js-data to connect to various data sources such as Firebase,
MySql, RethinkDB, MongoDB, localStorage, Redis, a REST API, etc. With js-data
you can re-use your data modeling code between environments, keep your data
layer intact when transitioning between app frameworks, and work with a unified
data API on the server and the client. js-data employs conventions for rapid
development, but allows for endless customization in order to meet your
particular needs.
To get started, visit http://js-data.io.
Table of contents
Quick Start
This example shows setting up js-data to use the Http adapter in the browser:
npm i --save js-data js-data-http
See installation instructions for more
information.
import {DataStore} from 'js-data'
import HttpAdapter from 'js-data-http'
const store = new DataStore()
store.registerAdapter('http', new HttpAdapter(), { 'default': true })
store.defineMapper('user')
let user = await store.find('user', 1)
console.log(user)
console.log(store.get('user', user.id))
console.log(user === store.get('user', user.id))
user = await store.update('user', user.id, { name: 'Johnny' })
console.log(store.get('user', user.id))
console.log(user === store.get('user', user.id))
await store.destroy('user', user.id)
console.log(store.get('user', 1))
ES5:
var store = new JSData.DataStore()
store.registerAdapter('http', new HttpAdapter(), { default: true })
store.defineMapper('user')
store.find('user', 1)
.then(function (user) {
console.log(user)
console.log(store.get('user', user.id))
console.log(user === store.get('user', user.id))
return await store.update('user', user.id, { name: 'Johnny' })
})
.then(function (user) {
console.log(store.get('user', user.id))
console.log(user === store.get('user', user.id))
return store.destroy('user', user.id)
})
.then(function () {
console.log(store.get('user', 1))
})
Background
Most ORMs/ODMs only work with a single datastore, and most JavaScript ORMs only
work in Node.js or the Browser. Wouldn't it be nice if you could use the same
ORM/ODM on the client as you do on the backend? Wouldn't it be nice if you could
switch databases without having to switch out your data layer code? Enter
js-data.
Originally inspired by the desire to have something like Ember Data that
worked in Angular.js and other frameworks, js-data was created. Turns out,
js-data works in Node.js, so server-side adapters were written. js-data is the
Model layer you've been craving. It consists of a convenient
framework-agnostic, datastore-agnostic ORM for managing your data, which
uses adapters to connect to various persistence layers.
The most commonly used adapter is the http adapter, which is perfect for
connecting your frontend to your backend. localStorage, localForage,
Firebase and other adapters are available for the browser. On the
server you could hook up to the SQL adapter (Postgres/MySQL/MariaDB/SQLite3)
or the MongoDB adapter. More adapters are coming, and you're free to
implement your own. See Adapters.
![MtnWestJS Conf 2015 Presentation](http://img.youtube.com/vi/8wxnnJA9FKw/0.jpg)
Dependencies
js-data and its adapters depend on a global ES2015 Promise
constructor. In
the browser, window.Promise
must be available. In Node.js, global.Promise
must be available. Here is a handy ES2015 Promise polyfill
if you need it.
js-data and its adapters require full ES5 support from the runtime. Here is a
handy ES5 polyfill if you need it.
Guides and Tutorials
Get started at http://js-data.io
API Reference Docs
Visit http://api.js-data.io.
Explore the Community.
Support
Find out how to Get Support.
Contributing
Read the Contributing Guide.
License
The MIT License (MIT)
Copyright (c) 2014-2016 js-data project authors