Skip to content

A node.js module for mongodb, that generates auto incremental ids for specified string keys.

License

Notifications You must be signed in to change notification settings

lionvs/mongo-incremental-id-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongo-incremental-id-generator

A node.js module for mongodb, that generates auto incremental ids for specified string keys.

Build Status js-standard-style

Don't use this generator unless you actually need auto incremental ids because it will limit scalability as you are essentially creating a single point of failure/bottleneck where you are maintaining the sequence values for each key.

Can you re-use generator values?

Well – yes, technically you can. But – NO, you shouldn't. Never. Never ever. Not only that this would destroy the nice chronological sequence, the more you think about it the more headaches it'll give you. Moreover it is an absolute contradiction to the entire concept of unique identifiers.

So unless you have good reasons to re-use generator values, JUST DON'T DO IT!

Usage

mongo-incremental-id-generator is very small mudule and easy to use:

var idGenerator = require('mongo-incremental-id-generator')(connectionString)

The connection string should follow the format described in the mongo connection string docs.

After we connected we can generate incremental ids for specific keys. Key is always string and step is always number. The format for callbacks is always callback(error, value) where error is null if no exception has occured.

// generate new id for the string key with increment step. By default step is equal to 1
idGenerator.generateId(key, step, function (err, id) {
	// id - auto generated id
})

idGenerator.generateId(key, function (err, id) {
})

// pick last generated id for the key
idGenerator.pickLastId(key, function (err, id) {
	// id - last generated id
})

// reset last generated id for the key to the newId. By default it resets to 0.
idGenerator.resetId(key, newId, function (err, id) {
	// id - newId
})

idGenerator.resetId(key, function (err, id) {
	// id - 0
})

About

A node.js module for mongodb, that generates auto incremental ids for specified string keys.

Resources

License

Stars

Watchers

Forks

Packages

No packages published