A small but fierce wrapper around the native mongodb driver - leveraging schemas - a syntactic mirror of the mongo shell interface.
Abiding by the the LOTR philosophy (one API to rule them all), mongorules adds a little extra sauce on top of the node-mongodb-native driver:
Schemas enforce consistency to operations:
insert()update()save()findAndModify()
Transforms easily modify the payload.
Promises wrap all mongodb native methods and thus become yieldable.
Model methods can be attached to collection models.
- Node >= v6.0.0
- Mongodb >= 2.6
- API
- Connecting
- Schemas
- Document Transformation
- Document Validation
- Model methods
- Error handling
- Misc
First, install mongorules and mongodb:
npm install --save mongorules@0.1.4-alpha mongodbSecond, init mongodb:
const mongorules = require('mongorules');
const mongodb = require('mongodb');
const url = process.env.MONGO_URL;
const connection = yield mongorules.connect('local', url, mongodb);
const db = mongorules.addDatabase('local', 'api-development', connection);Third, add models:
const mongorules = require('mongorules');
const schema = require('./schemas/users.js');
const methods = require('./methods/users.js');
mongorules.addModels('local', 'api-development', {
users: {
schema,
methods,
onError: function(errors, info) {}
}
});Add default db (recommended).
const mongorules = require('mongorules');
mongorules.setDefaultDb('local', 'api-development');Now, write queries:
var result, users;
const {db} = require('mongorules'); // This works because we setDefaultDb().
result = yield db.users.insert({ name: 'jay' });
result = yield db.users.find({ name: 'jay' });
users = yield result.toArray(); All mongodb native operations are supported and are wrapped in promises.
The following operations will enforce schema validation:
insert()update()save()findAndModify()
Mongorules supports validation for the following mongodb update operators:
$set$addToSet$push$inc$mul$min$max
Upsert operations are supported as well (validated as an insert).
See performance tests against the native mongodb driver and mongoose at the mongorules-performance-analysis repo.
MIT