MayLily


distributable, serverless, and customizable unique ID generator based on Snowflake
Features
- distributable / scalable
- no external servers required
- customizable
- supports 2-36 radix
- supports multiple precision integer
- supports CommonJS, ES Modules, TypeScript
How to install
Install by npm
.
npm install -S maylily
How to use
No external servers needed.
Just import and call maylily()
!
JavaScript
Traditional syntax.
This code will run on most JavaScript engine.
var maylily = require("maylily");
(function() {
maylily()
.then(function(id) {
})
.catch(function(err) {
});
}();
ECMAScript 7
Modern syntax.
Async/await syntax is easy to read
const maylily = require("maylily");
(async() => {
try {
const id = await maylily();
}
catch(err) {
}
})();
ES Modules / Babel / TypeScript (import, async/await)
VERY modern syntax!
import maylily from "maylily";
(async() => {
try {
const id = await maylily();
}
catch(err) {
}
})();
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
|------------------------------------------| current time from timeBase in millisec
|---| machineId (uses machineBits bits)
|----------| generatorId (uses generatorBits bits)
|--------| sequence number (uses sequenceBits bits)
example:
maylily({
timeBase: Date.parse("2017-01-01T00:00:00Z"),
machineBits: 1
});
Changelog
See CHANGELOG.md.