MongoDB storage plugin for Mio.
Install using npm:
npm install mio-mongo
var mio = require('mio');
var MongoDB = require('mio-mongo');
var User = mio.Resource.extend({
attributes: {
_id: {
primary: true,
objectId: true
}
}
});
User.use(MongoDB({
url: 'mongodb://db.example.net:2500',
collection: 'Users'
}));
User.Collection.get()
.where({ active: true })
.sort({ createdAt: 1 })
.exec(function (err, users) {
users.at(0).set({ active: false }).patch(function (err) {
// ...
});
});
Automatically stringify and cast ObjectId's by setting objectId: true
.
var User = mio.Resource.extend({
attributes: {
companyId: {
objectId: true
}
}
});
User.find({
companyId: '547dfc2bdc1e430000ff13b0'
}).exec(function (err, user) {
console.log(typeof user.companyId); // => "string"
});
Post.belongsTo('author', {
target: User,
foreignKey: 'authorId'
});
User.hasMany('posts', {
target: Post,
foreignKey: 'authorId'
});
// fetch posts for user `123`
Post.Collection.get()
.where({ 'author.id': 123 })
.exec(function (err, posts) {
// ...
});
// fetch users with their posts included
User.Collection.get()
.withRelated('posts')
.exec(function (err, users) {
users.pop().posts;
});
var User = mio.Resource.extend({
attributes: {
name: {
alias: 'fullName'
}
}
});
// MongoDB query uses "fullName"
User.find({ name: 'Alex' }).exec(...);
Access the mongo client directly via Resource.options.mongo.db
and the
resource's collection via Resource.options.mongo.dbCollection
.
It is recommended to share the same settings.db
object between
different resources so they can share the same mongo client and connection
pool.
A connection to mongo will be established automatically before any query is run.
If you'd like to use the mongo client directly, the db
is available via
Resource.options.mongo.db
.
Kind: Exported function
Returns: function
- returns Mio plugin
Param | Type | Description |
---|---|---|
settings | Object |
|
settings.collection | String |
mongodb collection for this resource |
[settings.connectionString] | String |
mongodb connection string. required if settings.db is not provided. |
[settings.connectionOptions] | Object |
mongodb connection options |
[settings.db] | mongodb.MongoClient.Db |
reuse node-mongo-native db connection |
Prepare resource attributes.
- Translate attribute aliases
- Stringify ObjectIDs
- Remove undefined attributes
Kind: inner method of module.exports
Param | Type |
---|---|
resource | Resource |
attributes | Object |
Emitted with query
argument whenever a query
is received and before it
is processed, to allow for transformation.
Kind: event emitted by module.exports
Param | Type |
---|---|
query | Object |
Emitted whenever a collection of resources is returned. Collections returned
by mio-mongo
include size
and from
pagination properties.
Kind: event emitted by module.exports
Param | Type |
---|---|
collection | external:mio.Resource.Collection |
collection.from | Number |
collection.size | Number |
Please submit all issues and pull requests to the mio/mongo repository!
Run tests using npm test
or gulp test
.
Generate code coverage using gulp coverage
and open coverage.html
in your
web browser.
If you have any problem or suggestion please open an issue here.