0.0.29 • Published 9 years ago

mongo-wrapper v0.0.29

Weekly downloads
24
License
BSD
Repository
github
Last release
9 years ago

mongo-wrapper

This is a lightweight wrapper around the mongodb driver for Node.js. It simplifies connecting and adds a few helper methods.

Installation

$ npm install mongo-wrapper

Usage

Configuration

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');

Query

// 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
});

Additional Helper Functions

findArray()

// findArray() simply calls toArray() on the cursor returned from
// Collection.find()
db.users.findArray(function(err, users) {
  // use users
});

findById(), updateById(), findAndModifyById()

// 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
});

bind()

// 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);

db.id()

// a wrapper around ObjectId
db.id(); // creates a new ObjectId
db.id('53683ca19c49151345e479ad'); // creates the ObjectId
db.id('invalid id'); // returns null
0.0.29

9 years ago

0.0.28

10 years ago

0.0.27

10 years ago

0.0.26

10 years ago

0.0.25

10 years ago

0.0.24

10 years ago

0.0.23

10 years ago

0.0.22

10 years ago

0.0.21

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

11 years ago

0.0.6

11 years ago

0.0.5

11 years ago

0.0.4

11 years ago

0.0.3

11 years ago

0.0.2

11 years ago

0.0.1

11 years ago

0.0.0

11 years ago