DSRethinkDBAdapter
rethinkdb adapter for js-data
Example App
js-data-examples/server/rethinkdb
Quick Start
npm install --save js-data js-data-rethinkdb
The adapter will look for a table
option on your resource definitions, or default to using the name of your resource to select from the right table.
var JSData = require('js-data');
var DSRethinkDBAdapter = require('js-data-rethinkdb');
var adapter = new DSRethinkDBAdapter({
host: '123.456.68.987',
db: 'my_db'
});
var store = new JSData.DS();
store.registerAdapter('rethinkdb', adapter, { default: true });
// "store" will now use the rethinkdb adapter for all async operations
Community & Links
- Slack channel - Ask your questions!
- Mailing List - Ask your questions!
- Issues - Found a bug? Feature request? Submit an issue!
- GitHub - View the source code for js-data.
- Contributing Guide
- Changelog
API
new DSRethinkDBAdapter([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 DSRethinkDBAdapter
it will come with some defaults. Here they are:
db
Type: string
Default database to use.
Default: "test"
host
Type: string
Connection hostname.
Default: "localhost"
port
Type: string
Connection port.
Default: 28015
authKey
Type: string
Authorization key.
Default: ""
min
Type: number
Minimum connections in the pool.
Default: 10
max
Type: number
Maximum number of connections in the pool
Default: 50
bufferSize
Type: number
Minimum number of available connections in the pool
Default: 10
See rethinkdbdash for more default connection pool options.
create(resourceConfig, attrs)
Creates a new item in RethinkDB.
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. |
Examples
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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 RethinkDB.
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. |
Examples
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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 RethinkDB.
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: {} . |
Examples
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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);
find(resourceConfig, id[, options])
Get an item from RethinkDB.
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
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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'
}
}
}
});
// 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'] }).then(function (document) {
document; // { id: 5, content: 'foo', userId: 1, user: {...} }
document.user; // { id: 1, name: 'John Anderson' }
});
findAll(resourceConfig, params[, options])
Get a collection of items from RethinkDB.
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
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', adapter, { default: true });
var User = store.defineResource('user');
var params = {
age: {
'>': 30
}
};
// bypass the data store
adapter.findAll(User, params).then(function (users) {
// users[0].age; 55 // etc., etc.
});
// normally you would go through the data store
User.findAll(params).then(function (users) {
// users[0].age; 55 // etc., etc.
});
update(resourceConfig, id, attrs)
Update an item in RethinkDB.
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
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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' }
});
// Normally you would just go through the data store
Document.update(5, { author: 'Johnny' }).then(function (document) {
document; // { id: 5, author: 'Johnny' }
});
updateAll(resourceConfig, attrs, params)
Update a collection of items in RethinkDB.
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
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', 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' }
});
// Normally you would just go through the data store
Document.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {
documents[0]; // { id: 5, author: 'Johnny' }
});
filterSequence(sequence, params)
Apply the proper RQL filter
, orderBy
, skip
, and limit
clauses to the given sequence
according to what is described in params
.
Returns the augments RQL query.
Arguments
name | type | description |
---|---|---|
sequence | object | RQL sequence. |
params | object | Query parameters for filtering, sorting, limiting, etc. Default: {} . |
Examples
var rql = r.table('files').getAll('1234-567-1234567', { index: 'ownerId' });
rql = adapter.filterSequence(rql, {
type: 'pdf'
});
rql.run().then(function (pdfFiles) {...});
selectTable(resource[, options])
Return a RQL select query for the table of the given resource
.
Examples
var adapter = new DSRethinkDBAdapter();
var store = new JSData.DS();
store.registerAdapter('rethinkdb', adapter, { default: true });
var Document = store.defineResource('document');
var rql = adapter.selectTable(Document);
rql.run().then(function (documents) {...});
License
The MIT License (MIT)
Copyright (c) 2014-2015 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.
Updated less than a minute ago