fast, distributable, serverless, and customizable unique ID number generator based on Snowflake
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.babelrc
.gitignore
LICENSE
README.md
gulpfile.babel.js
npm-shrinkwrap.json
package.json

README.md

MayLily

distributable, serverless, and customizable unique ID generator based on Snowflake

NPM

How to install

Install by npm.

npm install -S maylily

How to use

No external servers needed. Just import and call maylily()!

JavaScript

var maylily = require("maylily").default;

function main() {
    // returns a Promise object
    maylily()
        .then(function(id) {
            // do something...
        })
        .catch(function(err) {
            // err is instance of Error
        });
}

main();

ECMAScript 7 (async/await)

import maylily from "maylily";

async function main() {
    try {
        // returns a Promise object
        const id = await maylily();
        // do something...
    }
    catch(err) {
        // err is instance of Error
    }
}

main();

How to customize

name description default
radix radix of generated ID (2-36) 10
timeBase base time in unixtime(millisec) 946684800000 (2000-01-01T00:00:00Z)
machineId identifier of machine; must be unique in service 0
machineBits required bits to represent machineId 3
generatorId identifier of generator; must be unique in machine process ID
generatorBits required bits to represent generatorId 10
sequenceBits required bits to represent sequence 8

Generated value is stringified multiple precision integer (in specified radix).

 000001011100000101111010101110101010111101 001 1101101010 00000110
                                                          |--------| sequence number (uses sequenceBits bits)
                                               |----------|          generatorId (uses generatorBits bits)
                                           |---|                     machineId (uses machineBits bits)
|------------------------------------------|                         current time from timeBase in millisec

example:

// holds the change until next change
maylily({
    timeBase: Date.parse("2017-01-01T00:00:00Z"),   // if your service starts in 2017, this is enough.
    machineBits: 1                                  // if required number machines are up to 2, this is enough.
});

Release note

  • 2018-04-01 version 2.0.0
    • supports multiple precision integer
      • type of return value is string (even if less than 53-bits)
    • supports various radix (2-36)
    • change default bits of some elements
  • 2017-01-21 version 1.0.0
    • First release.