Mongoat
Description
Mongoat is a MongoDB ODM. It is written on top of the mongodb npm package to add new features.
Documentation
Because Mongoat is written on top of the mongodb native driver, you can find more about here
Features
- Hooks
- Enabling datetime (createdAt & updatedAt)
- Transparent usage (raw access to native MongoDB driver)
- Versioning of documents
- Oplogs events (not yet)
Installation
$> npm install mongoat
Basic usage
var mongoat = require('mongoat');
var url = 'mongodb://localhost:27017/myproject';
mongoat.MongoClient.connect(url)
.then(function (db) {
db.collection('Person').insert({ name: 'myName' })
.then(function (ObjectReturnedByMongodb) {
})
.catch(function (err) {
});
db.collection('Person').insert({ name: 'myName' },
function (err, ObjectReturnedByMongodb) {
});
});
Hooks
You can add multiple before and after hooks for insertions, updates and removals:
before hooks:
db.collection('collectionName').before('insert', function (docToInsert) {
});
db.collection('collectionName').before('update', function (docToUpdate) {
});
db.collection('collectionName').before('remove', function (docToRemove) {
});
after hooks:
db.collection('collectionName').after('insert', function (docToInsert) {
});
db.collection('collectionName').after('update', function (docToUpdate) {
});
db.collection('collectionName').after('remove', function (docToRemove) {
});
Datetime
Enable datetime feature:
db.collection('collectionName').datetime(true);
createdAt:
it will add a createdAt
field to all new inserted documents using:
db.collection('collectionName').insert(document, options);
or using one of the following method within the option upsert: ture
db.collection('collectionName').update(query, update, options);
db.collection('collectionName').findAndModify(query, sort, update, options);
updatedAt:
it will add a updatedAt
field to all updated documents using:
db.collection('collectionName').update(query, update, options);
db.collection('collectionName').findAndModify(query, sort, update, options);
Versioning
Enable versioning feature:
db.collection('collectionName').version(true);
Enabling this feature for a collection, so each time you perform an insert/update/remove it will create a document in the collection collectionName.vermongo and increment the version of the updated document. The _id
in this collection is a composite ID, { _id: _id, _version: _version }
.
The document in the MyCollection collection will also receive a _version field.
If we want to restore a version
db.collection('collectionName').restore(id, version);
- id of the document to restore is required
- if version is greater than 0, it will be considered as the version to restore
- if version is equal or lower than 0, it will be considered as the starting
- if version is not provided, it will takes default value 0
point of the version to restore (starting form last) ex:
db.collection('collectionName').restore(0);
db.collection('collectionName').restore(-2);
more about versioning feature here
Tests
- Run a MongoDb server if not yet
- Run tests
npm test
Or to show up code coverage npm run cover
it will generate ./coverage
folder
Contribution
Please read our Contributing Guidlines before submitting a pull request or an issue !
License
The MIT License MIT
Copyright (c) 2015 Dial Once