js-data

Respect Your Data

Give your data the treatment it deserves with a framework-agnostic, datastore-agnostic JavaScript ORM built for ease of use and peace of mind. Works in Node.js and in the Browser.

Get Started

DSLocalForageAdapter

localforage adapter for js-data

js-data-localforage Slack Status npm version Circle CI npm downloads Coverage Status

See the DEMO.

Quick Start

bower install --save localforage js-data js-data-localforage or npm install --save localforage js-data js-data-localforage.

Load Mozilla's localforage.nopromises.js.

Load js-data-localforage.js after js-data.js.

var adapter = new DSLocalForageAdapter();

var store = new JSData.DS();
store.registerAdapter('localforage', adapter, { default: true });

// "store" will now use the localforage adapter for all async operations

Community & Links

API

new DSLocalForageAdapter([options])

Need help?

Want more examples or have a question? Post on the Slack channel or mailing list then we'll get your question answered and probably update this wiki.

Default Settings

When you create a new instance of DSLocalForageAdapter it will come with some defaults. Here they are:

basePath

Type: string

Base path.

Default: ""

create(resourceConfig, attrs[, options])

Creates a new item in localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
attrs object The attributes from which to create the item.
options object Configuration options. Also passed through to DSLocalForageAdapter#PUT.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource('document');

// bypass the data store
adapter.create(Document, { author: 'John' }).then(function (document) {
  document; // { id: 5, author: 'John' }
  
  // The new document has NOT been injected into the data store because we bypassed the data store
  Document.get(document.id); // undefined
});

// Normally you would just go through the data store
Document.create({ author: 'John' }).then(function (document) {
  document; // { id: 5, author: 'John' }
  
  // the new document has been injected into the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

DEL(path[, options])

Remove an item from localforage.

Returns a promise.

Arguments
name type description
key string Key of the item to remove.
Examples
var adapter = new DSLocalForageAdapter();

adapter.DEL('/user/1').then(...);

destroy(resourceConfig, id[, options])

Delete an item from localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
id string or number The primary key of the item to destroy.
options object Configuration options. Also passed through to DSLocalForageAdapter#DEL.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });

// bypass the data store
adapter.destroy(Document, 5).then(function () {
  // the document is still in the data store because we bypassed the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

// Normally you would just go through the data store
Document.destroy(5).then(function () {
  // the document has been ejected from the data store
  Document.get(document.id); // undefined
});

destroyAll(resourceConfig, params[, options])

Delete a collection of items from localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
params object Query parameters for selecting the items to destroy. Default: {}.
options object Configuration options. Also passed through to DSLocalForageAdapter#DEL.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource('document');

var params = {
  author: 'John'
};

// bypass the data store
adapter.destroyAll(Document, params).then(function () {
  // the documents have NOT been ejected from the data store because we bypassed the data store
  Document.filter(params); // [{...}, {...}, ...]
});

// normally you would go through the data store
Document.findAll(params).then(function () {
  // the documents have been ejected from the data store
  Document.filter(params); // []
});

find(resourceConfig, id[, options])

Get an item from localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
id string or number The primary key of the item to retrieve.
options object Configuration options. Also passed through to DSLocalForageAdapter#GET.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
options.with array of strings Array of relation names or relation field names to eager load with the retrieved item. Supports nested relations, e.g. adapter.find(Post, 1, { with: ['user.organization'] })
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource({
  name: 'document',
  relations: {
    belongsTo: {
      user: {
        localKey: 'userId',
        localField: 'user'
      }
    }
  }
});
var User = store.defineResource({
  name: 'user',
  relations: {
    hasMany: {
      document: {
        foreignKey: 'userId',
        localField: 'documents'
      }
    },
    belongsTo: {
      organization: {
        localKey: 'organizationId',
        localField: 'organization'
      }
    }
  }
});
var Organization = store.defineResource({
  name: 'organization',
  relations: {
    hasMany: {
      user: {
        foreignKey: 'organizationId',
        localField: 'users'
      }
    }
  }
});

// bypass the data store
adapter.find(Document, 5, { with: ['user'] }).then(function (document) {
  document; // { id: 5, content: 'foo', userId: 1, user: {...} }
  document.user; // { id: 1, name: 'John Anderson' }
});

// Normally you would just go through the data store
Document.find(5, { with: ['user', 'user.organization'] }).then(function (document) {
  document; // { id: 5, content: 'foo', userId: 1, user: {...} }
  document.user; // { id: 1, name: 'John Anderson', organizationId: 10 }
  document.user.organization; // { id: 10, name: 'Company' }
});

findAll(resourceConfig, params[, options])

Get a collection of items from localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
params object Query parameters for selecting the items to retrieve. Default: {}.
options object Configuration options. Also passed through to DSLocalForageAdapter#GET.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
options.endwithoint array of strings Array of relation names or relation field names to eager load with the retrieved items. Supports nested relations, e.g. adapter.findAll(Post, null, { with: ['user.organization'] })
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource({
  name: 'document',
  relations: {
    belongsTo: {
      user: {
        localKey: 'userId',
        localField: 'user'
      }
    }
  }
});
var User = store.defineResource({
  name: 'user',
  relations: {
    hasMany: {
      document: {
        foreignKey: 'userId',
        localField: 'documents'
      }
    },
    belongsTo: {
      organization: {
        localKey: 'organizationId',
        localField: 'organization'
      }
    }
  }
});
var Organization = store.defineResource({
  name: 'organization',
  relations: {
    hasMany: {
      user: {
        foreignKey: 'organizationId',
        localField: 'users'
      }
    }
  }
});

var params = {
  size: {
    '>': 15000000
  }
};

// bypass the data store
adapter.findAll(Document, params).then(function (documents) {
  documents[0].size; 15063940
});

// normally you would go through the data store
Document.findAll(params).then(function (documents) {
  documents[0].size; 94839687
});

adapter.findAll(Document, params, { with: ['user', 'user.organization'] }).then(function (documents) {
  documents[0].size;
  documents[0].user; // {...}
  documents[0].user.organization; // {...}
});

GET(key)

Get an item from localforage.

Returns a promise.

Arguments
name type description
key string Key of the item to get.
Examples
var adapter = new DSLocalForageAdapter();

adapter.GET('/user/1').then(function (data) {
  // ...
});

PUT(key, value)

Update an item in localforage.

Returns a promise.

Arguments
name type description
key string Key.
value string Value.
Examples
var adapter = new DSLocalForageAdapter();

adapter.PUT('/user/1', { name: 'Johnny' }).then(...);

update(resourceConfig, id, attrs[, options])

Update an item in localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
id string or number The primary key of the item to retrieve.
attrs object The attributes with which to update the item.
options object Configuration options. Also passed through to DSLocalForageAdapter#PUT.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });

// bypass the data store
adapter.update(Document, 5, { author: 'Johnny' }).then(function (document) {
  document; // { id: 5, author: 'Johnny' }
  
  // The updated document has NOT been injected into the data store because we bypassed the data store
  Document.get(document.id); // { id: 5, author: 'John' }
});

// Normally you would just go through the data store
Document.update(5, { author: 'Johnny' }).then(function (document) {
  document; // { id: 5, author: 'Johnny' }
  
  // the updated document has been injected into the data store
  Document.get(document.id); // { id: 5, author: 'Johnny' }
});

updateAll(resourceConfig, attrs, params[, options])

Update a collection of items in localforage.

Returns a promise.

Arguments
name type description
resourceConfig object The resource definition created by DS#defineResource.
attrs object The attributes with which to update the items.
params object Query parameters for selecting the items to update. Default: {}.
options object Configuration options. Also passed through to DSLocalForageAdapter#PUT.
options.basePath string Override the basePath. Default: resourceConfig.basePath.
options.endpoint string Override the endpoint. Default: resourceConfig.endpoint.
Examples
var adapter = new DSLocalForageAdapter();
var store = new JSData.DS();
store.registerAdapter('lf', adapter, { default: true });

var Document = store.defineResource('document');

Document.inject({ id: 5, author: 'John' });
Document.inject({ id: 6, author: 'John' });

// bypass the data store
adapter.updateAll(Document, { author: 'Johnny' }, { author: 'John' }).then(function (documents) {
  documents[0]; // { id: 5, author: 'Johnny' }
  
  // The updated documents have NOT been injected into the data store because we bypassed the data store
  Document.filter({ author: 'John' }); // [{...}, {...}]
  Document.filter({ author: 'Johnny' }); // []
});

// Normally you would just go through the data store
Document.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {
  documents[0]; // { id: 5, author: 'Johnny' }
  
  // the updated documents have been injected into the data store
  Document.filter({ author: 'John' }); // []
  Document.filter({ author: 'Johnny' }); // [{...}, {...}]
});

License

The MIT License (MIT)

Copyright (c) 2014 Jason Dobry

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

DSLocalForageAdapter

localforage adapter for js-data