DSRedisAdapter

redis adapter for js-data

js-data-redis Slack Status npm version Travis CI npm downloads Coverage Status

Quick Start

npm install --save js-data js-data-redis

var JSData = require('js-data');
var DSRedisAdapter = require('js-data-redis');

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

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

Community & Links

API

new DSRedisAdapter([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 DSRedisAdapter it will come with some defaults. Here they are:

See the configuration options for node-redis.

create(resourceConfig, attrs)

Creates a new item in Redis.

Returns a promise.

Arguments
nametypedescription
resourceConfigobjectThe resource definition created by DS#defineResource.
attrsobjectThe attributes from which to create the item.
Examples
var adapter = new DSRedisAdapter();
var store = new JSData.DS();

store.registerAdapter('redis', 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' }
});

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

destroy(resourceConfig, id)

Delete an item from Redis.

Returns a promise.

Arguments
nametypedescription
resourceConfigobjectThe resource definition created by DS#defineResource.
idstring or numberThe primary key of the item to destroy.
Examples
var adapter = new DSRedisAdapter();
var store = new JSData.DS();

store.registerAdapter('redis', adapter, { default: true });

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

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

// bypass the data store
adapter.destroy(Document, 5);

// Normally you would just go through the data store
Document.destroy(5);

destroyAll(resourceConfig, params)

Delete a collection of items from Redis.

Returns a promise.

Arguments
nametypedescription
resourceConfigobjectThe resource definition created by DS#defineResource.
paramsobjectQuery parameters for selecting the items to destroy. Default: {}.
Examples
var adapter = new DSRedisAdapter();
var store = new JSData.DS();

store.registerAdapter('redis', adapter, { default: true });

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

var params = {
  author: 'John'
};

// bypass the data store
adapter.destroyAll(Document, params);

// normally you would go through the data store
Document.destroyAll(params);

[block:api-header]
{
  "type": "fn",
  "title": "find(resourceConfig, id[, options])"
}
[/block]

Get an item from Redis.

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. |
| options.with | array of strings | Array of names of relations to load. |

###### Examples

[block:code]
{
  "codes": [
    {
      "code": "var adapter = new DSRedisAdapter();\nvar store = new JSData.DS();\n\nstore.registerAdapter('redis', adapter, { default: true });\n\nvar Document = store.defineResource({\n  name: 'document',\n  relations: {\n    belongsTo: {\n      user: {\n        localKey: 'userId',\n        localField: 'user'\n      }\n    }\n  }\n});\nvar User = store.defineResource({\n  name: 'user',\n  relations: {\n    hasMany: {\n      document: {\n        foreignKey: 'userId',\n        localField: 'documents'\n      }\n    }\n  }\n});\n\n// bypass the data store\nadapter.find(Document, 5, { with: ['user'] }).then(function (document) {\n  document; // { id: 5, content: 'foo', userId: 1, user: {...} }\n  document.user; // { id: 1, name: 'John Anderson' }\n});\n\n// Normally you would just go through the data store\nDocument.find(5, { with: ['user'] }).then(function (document) {\n  document; // { id: 5, content: 'foo', userId: 1, user: {...} }\n  document.user; // { id: 1, name: 'John Anderson' }\n});",
      "language": "javascript"
    }
  ]
}
[/block]



[block:api-header]
{
  "type": "fn",
  "title": "findAll(resourceConfig, params[, options])"
}
[/block]

Get a collection of items from Redis.

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. |
| options.with | array of strings | Array of names of relations to load. |

###### Examples

[block:code]
{
  "codes": [
    {
      "code": "var adapter = new DSRedisAdapter();\nvar store = new JSData.DS();\n\nstore.registerAdapter('redis', adapter, { default: true });\n\nvar User = store.defineResource('user');\n\nvar params = {\n  age: {\n    '>': 30\n  }\n};\n\n// bypass the data store\nadapter.findAll(User, params).then(function (users) {\n  // users[0].age; 55 // etc., etc.\n});\n\n// normally you would go through the data store\nUser.findAll(params).then(function (users) {\n  // users[0].age; 55 // etc., etc.\n});",
      "language": "javascript"
    }
  ]
}
[/block]



[block:api-header]
{
  "type": "fn",
  "title": "update(resourceConfig, id, attrs)"
}
[/block]

Update an item in Redis.

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. |

###### Examples

[block:code]
{
  "codes": [
    {
      "code": "var adapter = new DSRedisAdapter();\nvar store = new JSData.DS();\n\nstore.registerAdapter('redis', adapter, { default: true });\n\nvar Document = store.defineResource('document');\n\nDocument.inject({ id: 5, author: 'John' });\n\n// bypass the data store\nadapter.update(Document, 5, { author: 'Johnny' }).then(function (document) {\n  document; // { id: 5, author: 'Johnny' }\n});\n\n// Normally you would just go through the data store\nDocument.update(5, { author: 'Johnny' }).then(function (document) {\n  document; // { id: 5, author: 'Johnny' }\n});",
      "language": "javascript"
    }
  ]
}
[/block]



[block:api-header]
{
  "type": "fn",
  "title": "updateAll(resourceConfig, attrs, params)"
}
[/block]

Update a collection of items in Redis.

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: `{}`. |

###### Examples

[block:code]
{
  "codes": [
    {
      "code": "var adapter = new DSRedisAdapter();\nvar store = new JSData.DS();\n\nstore.registerAdapter('redis', adapter, { default: true });\n\nvar Document = store.defineResource('document');\n\nDocument.inject({ id: 5, author: 'John' });\nDocument.inject({ id: 6, author: 'John' });\n\n// bypass the data store\nadapter.updateAll(Document, { author: 'Johnny' }, { author: 'John' }).then(function (documents) {\n  documents[0]; // { id: 5, author: 'Johnny' }\n});\n\n// Normally you would just go through the data store\nDocument.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {\n  documents[0]; // { id: 5, author: 'Johnny' }\n});",
      "language": "javascript"
    }
  ]
}
[/block]



[block:callout]
{
  "type": "info",
  "title": "License",
  "body": "The MIT License (MIT)\n\nCopyright (c) 2014-2015 Jason Dobry\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE 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."
}
[/block]