Adapters

How the data store communicates with persistence layers

The data store is an organized in-memory cache and has no knowledge of persistence layers. Adapters are used by the data store to communicate with persistence layers. When you use an async method of the data store, some of the work will most likely be delegated to an adapter.

TargetLinksBrowser Support?Node.js Support?BowerNPM
firebaseDocs, Issues YesYesbower versionnpm version
httpDocs, IssuesYesNobower versionnpm version
levelupDocs, IssuesNoYesn/anpm version
localforageDocs, IssuesYesNobower versionnpm version
localstorageDocs, IssuesYesNobower versionnpm version
mongodbDocs, IssuesNoYesn/anpm version
nedbDocs, IssuesNoYesn/anpm version
redisDocs, IssuesNoYesn/anpm version
rethinkdbDocs, IssuesNoYesn/anpm version
sqlDocs, IssuesNoYesn/anpm version

Custom Adapters

Implementing your own adapter is easy. You can view the source code of the existing adapters for examples.

Here is a pseudo example:

function MyCustomAdapter() {
  
}

// All of the methods shown here must return a promise

// "definition" is a resource defintion that would
// be returned by DS#defineResource

// "options" would be the options argument that
// was passed into the DS method that is calling
// the adapter method

MyCustomAdapter.prototype.create = function (definition, attrs, options) {
  // Must return a promise that resolves with the created item
};

MyCustomAdapter.prototype.find = function (definition, id, options) {
  // Must return a promise that resolves with the found item
};

MyCustomAdapter.prototype.findAll = function (definition, params, options) {
  // Must return a promise that resolves with the found items
};

MyCustomAdapter.prototype.update = function (definition, id, attrs, options) {
  // Must return a promise that resolves with the updated item
};

MyCustomAdapter.prototype.updateAll = function (definition, attrs, params, options) {
  // Must return a promise that resolves with the updated items
};

MyCustomAdapter.prototype.destroy = function (definition, id, options) {
  // Must return a promise 
};

MyCustomAdapter.prototype.destroyAll = function (definition, params, options) {
  // Must return a promise 
};

Then use it:

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

// the data store will now use your custom adapter by default