DSMongoDBAdapter
MongoDB adapter for js-data
Quick Start
npm install --save js-data js-data-mongodb
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter('mongodb://localhost:27017');
// "store" will now use the MongoDB adapter for all async operations
store.registerAdapter('mongodb', adapter, { default: true });
var User = store.defineResource({
// Resource name is required
name: 'user',
// Why couldn't Mongo just use "id"?
idAttribute: '_id',
// map this resource to a collection, default is Resource#name
table: 'users'
});
Community & Links
- Slack Channel
- 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 DSMongoDBAdapter(uri)
// or
new DSMongoDBAdapter(options)
Options
uri
MongoDB server uri, e.g. 'mongodb://localhost:27017'
Need help?
Want more examples or have a question? Ask on the Slack channel or post on the mailing list then we'll get your question answered and probably update this wiki.
Adapter Methods
- create - Creates a new item in MongoDB.
- destroy - Delete an item from MongoDB.
- destroyAll - Delete a collection of items from MongoDB.
- find - Get an item from MongoDB.
- findAll - Get a collection of items from MongoDB
- update - Update an item in MongoDB.
- updateAll - Update a collection of items in MongoDB.
create(resourceConfig, attrs)
Creates a new item in MongoDB.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The Resource definition created by DS#defineResource . |
attrs | object | The properties from which to create the item. |
Examples
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', adapter, { default: true });
var Document = store.defineResource('document');
// bypass the data store
adapter.create(Document, { author: 'John' }).then(function (document) {
document; // { _id: '590a8sd0f9a8sdf9jas', author: 'John' }
});
// Normally you would just go through the data store
Document.create({ author: 'John' }).then(function (document) {
document; // { _id: '590a8sd0f9a8sdf9jas', author: 'John' }
});
destroy(resourceConfig, id)
Delete an item from MongoDB.
Returns a promise.
resourceConfig | object | The Resource definition created by DS#defineResource . |
id | string or number | The primary key of the item to destroy. |
Examples
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ _id: '590a8sd0f9a8sdf9jas', author: 'John' });
// bypass the data store
adapter.destroy(Document, '590a8sd0f9a8sdf9jas');
// Normally you would just go through the data store
Document.destroy('590a8sd0f9a8sdf9jas');
destroyAll(resourceConfig, params)
Delete a collection of items from MongoDB.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The Resource definition created by DS#defineResource . |
params | object | Query parameters for selecting the items to destroy. See Query Syntax. Default: { } |
Examples
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', 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 MongoDB.
Returns a promise.
Argument | 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 | Optional configuration. |
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 JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', 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, '34j493j9sasdf9s', { with: ['user'] }).then(function (document) {
document; // { _id: '34j493j9sasdf9s', content: 'foo', userId: '590a8sd0f9a8sdf9jas', user: {...} }
document.user; // { _id: '590a8sd0f9a8sdf9jas', name: 'John Anderson' }
});
// Normally you would just go through the data store
Document.find('34j493j9sasdf9s', { with: ['user', 'user.organization'] }).then(function (document) {
document; // { _id: '34j493j9sasdf9s', content: 'foo', userId: '590a8sd0f9a8sdf9jas', user: {...} }
document.user; // { _id: '590a8sd0f9a8sdf9jas', name: 'John Anderson', organizationId: '9a9sdf8as0d9gas9' }
document.user.organization; // { _id: '9a9sdf8as0d9gas9', name: 'Company' }
});
findAll(resourceConfig, params[, options])
Get a collection of items from MongoDB.
Returns a promise.
Argument | Type | Description |
---|---|---|
resourceConfig | object | The Resource definition created by DS#defineResource . |
params | object | Query parameters for selecting the items to retrieve, limit, sort, etc. See Query Syntax. Default: { } |
options | object | Optional configuration. |
options.with | 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, 1, { with: ['user.organization'] }) |
Examples
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', 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; // {...}
});
update(resourceConfig, id, attrs)
Update an item in MongoDB.
Returns a promise.
Argument | 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 JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ _id: '590a8sd0f9a8sdf9jas', author: 'John' });
// bypass the data store
adapter.update(Document, '590a8sd0f9a8sdf9jas', { author: 'Johnny' }).then(function (document) {
document; // { _id: '590a8sd0f9a8sdf9jas', author: 'Johnny' }
});
// Normally you would just go through the data store
Document.update('590a8sd0f9a8sdf9jas', { author: 'Johnny' }).then(function (document) {
document; // { _id: '590a8sd0f9a8sdf9jas', author: 'Johnny' }
});
updateAll(resourceConfig, attrs, params)
Update a collection of items in MongoDB.
Returns a promise.
Argument | 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. See Query Syntax. Default: { } |
Examples
var JSData = require('js-data');
var DSMongoDBAdapter = require('js-data-mongodb');
var store = new JSData.DS();
var adapter = new DSMongoDBAdapter({
uri: 'mongodb://localhost:27017'
});
store.registerAdapter('mongo', adapter, { default: true });
var Document = store.defineResource('document');
Document.inject({ _id: '590a8sd0f9a8sdf9jas', author: 'John' });
Document.inject({ _id: '54lsd8f9as8df9a8sd9', author: 'John' });
// bypass the data store
adapter.updateAll(Document, { author: 'Johnny' }, { author: 'John' }).then(function (documents) {
documents[0]; // { _id: '590a8sd0f9a8sdf9jas', author: 'Johnny' }
});
// Normally you would just go through the data store
Document.updateAll({ author: 'Johnny' }, { author: 'John' }).then(function (documents) {
documents[0]; // { _id: '590a8sd0f9a8sdf9jas', author: 'Johnny' }
});
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