node-sequential-ids
A Node.js module that allows centralized generation of
sequential and human-readable ids.
Sample Id: ACB - 00423
aspect | detail |
---|
version | 0.0.0 |
dependencies | none |
node | 0.11, 0.10 |
last updated | 11th December, 2014 |
installation
From Npm:
$ npm install sequential-ids --save
usage
var sequential = require("sequential-ids");
var generator = new sequential.Generator({
digits: 6, letters: 3,
store: function(key, ids) {
db.store(key, ids[ids.length - 1]);
},
restore: "AAB - 000"
});
generator.add('otherKey', {
digits : 3, letters: 1,
store: function(key, ids) {
db.store(key, ids[ids.length - 1]);
},
restore: "A - 00"
});
generator.start();
var new_id_1 = generator.generate();
var new_id_2 = generator.generate();
var other_id_1 = generator.generate('otherKey');
var other_id_2 = generator.generate('otherKey');
var accessor = new sequential.Accessor();
accessor.next(function(err, id) {
console.log("new id: %s", id);
});
accessor.next('otherKey',function(err, id) {
console.log("new id (otherKey): %s", id);
});
notes
- new Generator([options])
-
you only require to create a single generator instance
-
options is an object having the following attributes:
-
in a case where you may require more than one generator, you
would allocate them to different ports. See ahead on how to target
each of the different generators.
var sequential = require("sequential-ids");
var generatorA = new sequential.Generator({port: 8998});
var generatorB = new sequential.Generator({port: 7667});
- new Accessor([port])
-
used to access IDs.
-
port is the port number of your generator. In case where, you
did not specify a port when creating a Generator instance, you may
leave this out. Defaults to 9876
.
-
an accessor may be initialized in a separate file. Ensure you got the
port numbers correct.
-
an accessor has the following methods:
Accessor#next(key,callback)
:
- callback signature:
function(err, id)
- asks the generator a new ID for key. If no key is
given, uses the default one.
- The new ID is passed to the callback, on success.
- If the key doesn't exist and the generator doesn't have
autoAddKeys
set, an error is passed to the callback, and no
ID is generated.
Accessor#ping(callback)
- callback signature:
function(err)
- pings the generator to see if it is online
-
All methods are asynchronous, the Node.js way
TODO
- Robust Error handling
- Implement these features:
session(callback)
- passes the number of IDs generated in the
session..used(callback)
- passes the total number of IDs generated..semantics(callback)
- passes the remaining no. of IDs to be
generated before breaking our semantics specified while creating
the generator.
contribution
- Source Code is hosted on Github
- Pull requests be accompanied with tests as in the
/tests
directory - Issues may be filed here
A list of contributors:
- GochoMugo
license
Copyright (c) 2014 Forfuture LLC
Sequential Ids and its source code are licensed under the MIT
license. See LICENSE file accompanying this text.