0.0.2 • Published 9 years ago

mongo-sequential v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Mongo Sequential

Create primary keys or another sequential number with a mongodb hosted counter.

Uses the findOneAndUpdate from mongodb to guarantee unique keys.

Use Promises or callbacks. If you pass a callback it will be called with standard node convention of cb(err, result). Promises are implemented using bluebird.

Exports: increment(collection, name[, cb(err, count)]) -> (Promise -> Integer)

Install

npm install mongo-sequential

Usage

var mongodb = require('mongodb');
var increment = require('mongo-sequential');

mongodb.connect('mongodb://localhost/test', function( err, db ){
	var collection = db.collection('counters');
	var name = 'users';

	increment(collection, name)
		.then(console.log.bind(console));	// 1

	// use bind to pre-configure
	var incrementer = increment.bind(null, collection, name);

	incrementer()
		.then(console.log.bind(console));	// 2
});

Test

Requires a running mongodb on localhost

npm test