0.1.3 • Published 5 years ago
@luvaeria/snowflake-js v0.1.3
Snowflake-JS
Twitter's Snowflake implementation for NodeJS.
Snowflake is a network service for generating unique ID numbers at high scale with some simple guarantees.
As we at Twitter move away from Mysql towards Cassandra, we've needed a new way to generate id numbers. There is no sequential id generation facility in Cassandra, nor should there be.
Installing
You will need NodeJS 10.4+. Refer to BigInt Compatibility for more details.
npm install @luvaeria/snowflake-js
Snowflake Generator Example
const Snowflake = require('@luvaeria/snowflake-js');
const options = {
epoch: 1577833200000,
workerId: 0,
datacenterId: 0,
workerIdBits: 5,
datacenterIdBits: 5,
sequence: 0,
sequenceBits: 12,
};
const Generator = new Snowflake(options); // new Snowflake.Generator(options);
console.log(Generator.getId()); // 103057029651759104n (type bigint)
console.log(Generator.getId().toString()); // '103057029681119232' (type string)
IDs
All generated identifiers are BigInt and can be easily converted into strings.
let id;
id = Generator.getId(); // 103057029681119233n
id = Generator.getId().toString(); // '103057029681119234'
id = Generator.getId(); // 103057029681119235n
id = id.toString(); // '103057029681119235'
Snowflake Server example
Refer to example/snowflake-server.js
Snowflake Generator Options
Property | Description | Optional? | Default Value | |
---|---|---|---|---|
epoch | Custom epoch for timestamp generation. By default, the number of milliseconds since the first second of 2020 | Yes | 1577833200000 | |
workerId | Worker ID for this generator | Yes | 0 | |
workerIdBits | Number of usable bits for Worker Id. 5 by default, allows up to 31 Workers | Yes | 5 | Worker ID for this generator |
datacenterId | Datacenter ID for this generator | Yes | 0 | |
datacenterIdBits | Number of usable bits for Datacenter Id. 5 by default, allows up to 31 Datacenters | Yes | 5 | |
sequence | For every ID that is generated on that process, this number is incremented | Yes | 0 | |
sequenceBits | Number of usable bits for Sequence Id. 12 by default, allows up to 4095 generations per millisecond per Worker per Datacenter | Yes | 12 |
Useful Links
- The original Twitter's Snowflake repository
- Announcing Snowflake
- Twitter Ids
- The GitHub repo
- The NPM package
License
Refer to the LICENSE file.