This is a lightweight wrapper around the mongodb driver for Node.js. It simplifies connecting and adds a few helper methods.
$ npm install mongo-wrapper
var mongo = require('mongo-wrapper');
var config = {
username: 'admin_user',
password: 'secret',
hosts: [
{name: 'primary-1.db.com', port: 27017},
{name: 'secondary-1.db.com', port: 27017},
{name: 'secondary-2.db.com', port: 27017}
],
database: 'db_1',
options: {
replicaSet: 'replicaset_1',
readPreference: 'primaryPreferred'
},
indexes: {
users: [
{index: 'email', options: {unique: true}}
]
}
};
db = mongo.setup(config);
// add your collections
db.add('users');
// once the db is setup and collections are added you can use any
// methods found in the Mongo Driver Collection class
// http://mongodb.github.io/node-mongodb-native/api-generated/collection.html
db.users.insert({email: 'dan@email.com'}, function(err) {
// inserted
});
db.users.findOne({email: 'dan@email.com'}, function(err, user) {
// use user
});
// findArray() simply calls toArray() on the cursor returned from
// Collection.find()
db.users.findArray(function(err, users) {
// use users
});
// These functions make it simple to query by ObjectId and they
// accept either and ObjectId object or a JavaScript String.
db.users.findById('53683ca19c49151345e479ad', function(err, user) {
// use user
});
// This allows you to bind a function on a collection so you can
// easily use mongo-wrapper with libraries like async
var user_id = db.id('53683ca19c49151345e479ad');
async.parallel({
user: db.users.bind('findById', user_id),
reservations: db.reservations.bind('findArray', {user_id: user_id})
}, callback);
// a wrapper around ObjectId
db.id(); // creates a new ObjectId
db.id('53683ca19c49151345e479ad'); // creates the ObjectId
db.id('invalid id'); // returns null