Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Elegant MongoDB driver for Node.js.
$ npm i mongolass --save
Just like mongoose:
'use strict';
let Mongolass = require('mongolass');
let mongolass = new Mongolass();// let mongolass = new Mongolass('mongodb://localhost:27017/test');
mongolass.connect('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();
mongolass.connect('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);
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, only used for parameter validation before insert document to mongodb.
Awesome plugin system. eg: beforeInsert
, afterFind
and so on.
see another-json-schema.
Mongolass has some built-in plugins, only for find
and findOne
.
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.
$ npm test (coverage 100%)
MIT
1.0.1/2016-03-09
FAQs
Elegant MongoDB driver for Node.js.
The npm package mongolass receives a total of 12 weekly downloads. As such, mongolass popularity was classified as not popular.
We found that mongolass demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.