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.
Target | Links | Browser Support? | Node.js Support? | Bower | NPM |
---|---|---|---|---|---|
firebase | Docs, Issues | Yes | Yes | ||
http | Docs, Issues | Yes | No | ||
levelup | Docs, Issues | No | Yes | n/a | |
localforage | Docs, Issues | Yes | No | ||
localstorage | Docs, Issues | Yes | No | ||
mongodb | Docs, Issues | No | Yes | n/a | |
nedb | Docs, Issues | No | Yes | n/a | |
redis | Docs, Issues | No | Yes | n/a | |
rethinkdb | Docs, Issues | No | Yes | n/a | |
sql | Docs, Issues | No | Yes | n/a |
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
Updated less than a minute ago