Mongolass
Elegant MongoDB driver for Node.js.
Installation
$ npm i mongolass --save
Introduction
Just like mongoose:
'use strict';
let Mongolass = require('mongolass');
let mongolass = new Mongolass();
mongolass.connect('mongodb://localhost:27017/test');// let mongolass = new Mongolass('mongodb://localhost:27017/test');
let User = mongolass.model('User');
User
.find()
.select({ name: 1, age: 1 })
.sort({ name: -1 })
.exec()
.then(console.log)
.catch(console.error);
or use optional schema:
'use strict';
let Mongolass = require('mongolass');
let Schema = Mongolass.Schema;
let mongolass = new Mongolass('mongodb://localhost:27017/test');
let UserSchema = new Schema('UserSchema', {
name: { type: 'string' },
age: { type: 'number' }
});
let User = mongolass.model('User', UserSchema);
User
.insertOne({ name: 'nswbmw', age: 'wrong age' })
.exec()
.then(console.log)
.catch(console.error);
/*
{ [Error: ($.age: "wrong age") ✖ (type: number)]
validator: 'type',
actual: 'wrong age',
expected: { type: 'number' },
path: '$.age',
schema: 'UserSchema',
model: 'User',
plugin: 'MongolassSchema',
type: 'beforeInsertOne',
args: [] }
*/
ObjectId schema:
'use strict';
let Mongolass = require('mongolass');
let Schema = Mongolass.Schema;
let mongolass = new Mongolass('mongodb://localhost:27017/test');
let PostSchema = new Schema('PostSchema', {
author: { type: Mongolass.Types.ObjectId },
});
let Post = mongolass.model('Post', PostSchema);
Post.insertOne({ author: '111111111111111111111111' })
.then(function () {
return Post.find({ author: '111111111111111111111111' });
})
.then(console.log);
/*
[ { _id: 57caed24ecda6ffb15962591,
author: 111111111111111111111111 } ]
*/
What about Mongolass
Mongolass retains the api of node-mongodb-native, and draws useful features of mongoose. Compared with node-mongodb-native, Mongolass has following three features:
-
Elegant connection. eg:
mongodb
MongoClient.connect(..., function(err, db) {
db.listCollections()
})
Mongolass
mongolass.connect(...)
mongolass.listCollections()
-
Optional schema, used for validating and formatting parameters before query or update.
-
Awesome plugin system. eg: beforeInsert
, afterFind
and so on. You can define custom plugins.
-
Detailed error information.
Schema
see another-json-schema.
Built-in plugins
Mongolass has some built-in plugins, only for find
and findOne
.
Register plugin
mongolass.plugin(pluginName, hooks);// register global plugin
User.plugin(pluginName, hooks);// register model plugin
examples:
User.plugin('mw2', {
beforeInsert: function (...args) {
},
afterFind: function* (result, ...args) {
console.log(result, args);
...
}
});
mongolass.plugin('mw1', {
beforeFind: function (...args) {
console.log(ctx._op);
console.log(ctx._args);
console.log(args);
...
}
});
yield User.find().mw1().mw2().exec()// equal: yield User.find().mw1().mw2()
User.find().mw2().mw1().exec().then(...).catch(...)
User.find().mw1().mw2().exec(function (err, res) {
console.log(err, res)
})
NOTE: Different order of calling plugins will output different results.
see mongolass-plugin-populate.
Test
$ npm test (coverage 100%)
License
MIT