mongo-incremental-id-generator

0.1.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i mongo-incremental-id-generator

Weekly Downloads

2

Version

0.1.0

License

MIT

Last publish

Collaborators

  • lionvs