Mongo plugin for modella. Thinly built on top of kissjs/node-mongoskin.
npm install modella-mongo
var model = require('modella');
mongo = require('modella-mongo')('localhost/db');
var User = model('user')
.attr('_id')
.attr('name')
.attr('email', { unique: true })
.attr('password');
User.use(mongo);
/**
* Initialize
*/
var user = new User;
user.name('matt');
user.save(function(err) {
console.log(user.toJSON());
});
By adding the plugin, modella 0.2.0 compliant sync layer methods are added. This
enables instance#save()
and instance#remove()
to work with mongo.
By loading this plugin, model inherits:
Object pointing to the raw mongoskin database. Use it to manipulate the collection directly.
Index an attribute in mongo.
User.index('email', { unique : true });
Alternatively, you can specify unique: true
when defining an attribute.
User.attr('username', {unique: true});
// Equivilent to...
User.attr('username');
User.index('username', {unique: true, sparse: true});
Queries for all users in the collection that match the given query
. Additional
options can be passed in (eg. {sort: {age: -1}}
).
Calls fn(err, instances)
where instances
is an array of Model instances. If
no documents match, instances
will be empty.
User.all({emailConfirmed: true}, {sort: {emailConfirmationDate: 1}}, function(err, users) {
console.log("Users with confirmed emails: ");
users.forEach(function(u) {
console.log(u.username());
});
});
Queries for one user in the collection that match the given query
. Additional
options can be passed in (eg. {sort: {age: -1}}
).
query
can also be a string, in which case it will be converted to an
ObjectId
.
Calls fn(err, instance)
where instance
is an instance of Model. If
no documents match, instance
will be false
.
User.get == User.find // true
User.find('528263fa996abeabbe000002', function(err, u) {
console.log(u.username());
});
Calls fn(err, instances)
where instances
is an array of Model instances. If
no queries match, instances will be false
.
Removes all records that match the given query
.
Calls fn(err, count)
where count
is the number of records removed. If
no queries match, instances will be false
.
User.removeAll({emailConfirmed: false}, function(err, count) {
console.log("%d users were deleted", count);
});
Returns a wrapped instances of mquery
. See mquery support below.
Returns a wrapped instances of maggregate
. See maggregate support below.
modella-mongo
provides a wrapped version of the wonderful mquery
query builder. To get it, simply call Model.query()
.
This allows you to build readable and robust queries easily. When approprirate,
modella-mongo will return instances of modella
models, instead of just
documents. Aside from that, it follows the mquery
API completely.
User.query().findOne().where({username: 'Bob'}).exec(function(err, u) {
u.username() // => 'Bob'
});
modella-mongo
uses the maggregate
aggregation builder. To use it, simply call Model.aggregate()
.
This allows you to build readable aggregations easily. By default it wraps
responses in Model
instances, but can be disabled by passing skipWrap
as
true
. It also follows the maggregate
api completely.
var skipWrapping = true;
User.aggregate(skipWrapping).group({_id: '$location', locationCount: {$sum: 1}}, function(err, res) {
res.forEach(function(loc) {
console.log("In location %s there are %d users", loc._id, loc.locationCount);
});
});
Each call to modella-mongo
will open up a mongo connection, and return a function that can be used as a plugin for ANY Modella model.
As such it is recommended that you export the result of modella-mongo
and then use that for all of your models.
...
var mongo = require('modella-mongo')('localhost/my-db');
User.use(mongo);
...
...
var mongo = require('modella-mongo')('localhost/my-db');
Post.use(mongo);
...
In the above example both the User
and Post
model will open a connection to the mongo database.
var mongo = module.exports = require('modella-mongo')('localhost/my-db');
...
var configuredDb = require('../config/modella-db');
User.use(configuredDb);
...
...
var configuredDb = require('../config/modella-db');
Post.use(configuredDb);
...
Here modella-db.js
configures the mongo database, and then both models use it.
MIT