Robe
Robe wraps around monk to provide a simple yet effective ODM library for MongoDB.
Features:
- Work with ODM-style documents or raw Mongo data - the choice is yours
- Add before and after hooks for inserts, updates and removals
- Cursor mode (for streaming results)
- Schema validation (simple-nosql-schema).
- Indexes and replica sets supported
- Mongo oplog tailing (like in Meteor)
- and more...
Installation
This package requires Node 4+
$ npm install robe
Examples
Detailed documentation is available at https://hiddentao.github.io/robe.
The basics
"use strict"; var co = Robe = ; ;
Raw querying mode
In this mode we won't make use of Robe.Document
and will instead deal
directly with Mongo data objects.
// insert a record collection; // find itvar item = collection; console; // falseconsole; // _id, name, age // update collection; // remove collection;
You can also enable rawMode
querying at the collection level:
var collection = db; collection;
Hooks
You can add multiple before
and after
hooks for insertions, updates and
removals. Hooks get triggered even when calling the save()
and remove()
methods on a Robe.Document
instance.
collection; collection; // remove collection; /*Ouptut: Before hook { name: 'john', age: 54 } After hook: 1*/
Schema validation
Schema definitions are as supported by simple-nosql-schema. Inserts and updates trigger schema validation checks. Any keys not specified in the schema get ignored during validation, i.e. a schema can be a partial definition of a document.
// get a collectionvar collection = db; // insert a recordtry collection; catch err console; /* Error: Validation failed */ console; /* [ "/isMarried: must be true or false", "/numCars: must be a number", ] */
Indexes
Robe supports the full Mongo index spec and can ensure that indexes you define are present within a collection:
// get a collectionvar collection = db; // setup all indexes collection;
Oplog tailing
If you are connecting to a Mongo replica set, then you can tail the oplog through Robe, allowing you to be notified when collections within your database get updated (even by other processes).
// connect to replica set// (note that replicaSet parameter MUST be set to actual replica set name)var db = Robe; var collection = db; // watch for any changes to the collection collection; /* you can also access the oplog directly on the `db` */ // get the oplogvar oplog = db; // start it oplogstart; // listen for any operation on any collectionoplog; // listen for any operation on the "test" collectionoplog; // listen for delete operations on the "test" collectionoplog;
Building
To run the tests:
$ npm install -g gulp
$ npm install
$ npm test
Contributing
Contributions are welcome! Please see CONTRIBUTING.md.
Inspiration and thanks
License
MIT - see LICENSE.md