Mongorito
ES6 generator-based MongoDB ODM for Node.js v0.11.x.
Features
- Based on ES6 generators, which means no callbacks
- Common, established API you've already used to
- Hooks (before:save, around:create, after:remove, etc)
- Very simple and easy-to-understand implementation
- Fully covered by tests
Installation
npm install mongorito --save
Warning: You should have Node.js v0.11.x installed (or newer). Run node with --harmony
option:
node --harmony something.js
Note: In order for the following examples to work, you need use something like co.
Getting Started
var Mongorito = require('mongorito');
var Model = Mongorito.Model;
Connect
Mongorito.connect('localhost/database_name');
Mongorito.connect('db1/database_name', 'db2/database_name');
Disconnect
Mongorito.disconnect();
Mongorito.close();
Define a model
That's right. No schema.
var Post = Model.extend({
collection: 'posts'
});
Find all
Returns all documents in the collection.
var posts = yield Post.all();
Find with query
Always returns an array.
var posts;
posts = yield Post.find(); // same as .all()
// match documents with title = "Great title"
posts = yield Post.find({ title: 'Great title' });
posts = yield Post.where('title', 'Great title').find();
posts = yield Post.where('title').equals('Great title').find();
// match documents with regex
posts = yield Post.where('title', /great/i).find();
// match documents matching sub-property
posts = yield Post.where('author.name', 'John Doe').find();
// match documents matching sub-documents
// matching posts where at least one comment.body is "Nice comment"
posts = yield Post.where('comments', { body: 'Nice comment' }).find();
// match documents with limit
posts = yield Post.limit(5).find();
// match documents with skip
posts = yield Post.skip(5).find();
// match documents where some field exists
posts = yield Post.where('title').exists().find();
// match documents with $in
posts = yield Post.where('title').in(['First title', 'Second title']).find();
// match documents with $gt, $gte, $lt, $lte
posts = yield Post.where('position').gt(5).find();
posts = yield Post.gt('position', 5).find();
Find one
Finds only one document. Returns either Model instance (Post, in these examples) or undefined.
var post;
post = yield Post.findOne();
post = yield Post.findOne({ title: 'Great title' });
Save
Automatically determines when to create or update a document based on _id field.
var post = new Post({
title: 'Some title'
});
post.set('title', 'Good title');
post.set({
title: 'Great title',
body: 'Hot body'
});
yield post.save();
var id = post.get('_id');
post.set('title', 'Updated title');
yield post.save();
Remove
yield post.remove();
Or you can remove multiple documents using:
yield Post.remove({ title: 'Some title' });
Hooks
Mongorito models support before, create and around (before + create) hooks for these events:
- save (executes on both create and update)
- create
- update
- remove
To setup a hook for some event:
var Post = Model.extend({
collection: 'posts',
customHandling: function *(next) {
yield next;
}.before('save')
});
Same goes for other hooks:
myAfterCreateHook: function *(next) {
yield next;
}.after('create'),
myAroundRemoveHook: function *(next) {
yield next;
}.around('remove')
If you want to break the chain and prevent executing of the next hooks, just throw an Error:
myHook: function *(next) {
throw new Error('Next hooks will not be executed');
}.before('update')
Tests
To execute tests run:
npm test
License
The MIT License (MIT)
Copyright © 2014 Vadim Demedes vdemedes@gmail.com
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.