Skip to content

iamdevonbutler/mongorules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

296 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongorules (alpha)

Build Status

A small but fierce wrapper around the native mongodb driver - leveraging schemas - a syntactic mirror of the mongo shell interface.

Intro

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.

Requirements

  • Node >= v6.0.0
  • Mongodb >= 2.6

Docs

Getting started

First, install mongorules and mongodb:

npm install --save mongorules@0.1.4-alpha mongodb

Second, 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();  

Supported operations

All mongodb native operations are supported and are wrapped in promises.

The following operations will enforce schema validation:

  • insert()
  • update()
  • save()
  • findAndModify()

Update operations

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).

Performance

See performance tests against the native mongodb driver and mongoose at the mongorules-performance-analysis repo.

License

MIT

About

A small but fierce wrapper around the native mongodb driver - leveraging schemas - a syntactic mirror of the mongo shell interface.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors