Simple module that aims to provide snowflake alike ID generator that fits into 53bit number as specificed on IEEE_754.
As being limited to 53bit number, there is some limitations:
- Timespan is limited to 28 years for given epoch
- Machine ID is limited from
0..9
- Sequence is limited to
000..999
<Timestamp><Worker ID><Sequence>
8470564087028
847056408
- timespan7
- worker ID028
- sequence number
npm install flakeid53
epoch: Number
Time to start the epoch of the snowflake generation, it will be used as substract for given current time, to produce the first 12 digits.
workerId: Number
0-9 range machine or worker ID. It is usefull in case of usage on distributed systems.
// Use as node module
const flakeId = require("flakeid53")({
epoch: +new Date("2021-03-03"),
workerId: 2,
});
// Use as ES module
import createFlakeID53 from "flakeid53";
const flakeId = createFlakeID53({
epoch: +new Date("2021-03-03"),
workerId: 2,
});
// Use in Deno
import createFlakeID53 from "https://cdn.jsdelivr.net/npm/flakeid53/index.js"
const flakeId = createFlakeID53({
epoch: +new Date("2021-03-03"),
workerId: 2,
});
nextId()
returns a promise that resolve to next ID number.
await flakeId.nextId();
// Outputs: 8470564087028
flakeId.parse(8470564087028);
// Outputs: { time: 2021-03-12T19:17:36.408Z, workerId: 7, sequence: 28 }
In my specific case, using snowflake ID or some other 64bit alternative is not an option.