Mongoat
Description
Mongoat is a MongoDB lightweight wrapper adding hooks (pre/post), automatic createdAt/updatedAt, in a native MongoDB experience. It is written on top of the mongodb npm package.
It does not provides any ODM, model specifications, validation, or things that would force you to use it in a specific way. Mongoat is designed to be used in a MongoDB way: your way.
Features
- Hooks (pre/post actions: afterSave, beforeSave, etc.)
- Datetime documents easily (createdAt & updatedAt)
- Native MongoDB experience and usage (you actually use the native MongoDB driver when you use Mongoat)
- Versions documents easily (Vermongo specifications)
ToDo:
- Event triggering on oplog actions
Installation
npm install mongoat
Basic usage
var mongoat = ; //instead of require('mongodb');
And then your mongoat
object is to be used like the MongoDB native node.js driver. We just add some features on top of it, see below:
Hooks
You can add multiple before and after hooks for insertions, updates and removals:
before hooks:
db; db; db;
after hooks:
db; db; db;
Datetime
Enable datetime feature:
db; // Default is false
createdAt:
it will add a createdAt
field to all new inserted documents using:
db;
or using one of the following method within the option upsert: ture
db;db;
updatedAt:
it will add a updatedAt
field to all updated documents using:
db;// ordb;
Versioning
Enable versioning feature:
dbversiontrue; // Default is false
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;
- 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; // restore the last version
db; // restore the last version -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