0.1.3 • Published 5 years ago

@luvaeria/snowflake-js v0.1.3

Weekly downloads
1
License
GPL-3.0-or-later
Repository
github
Last release
5 years ago

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

PropertyDescriptionOptional?Default Value
epochCustom epoch for timestamp generation. By default, the number of milliseconds since the first second of 2020Yes1577833200000
workerIdWorker ID for this generatorYes0
workerIdBitsNumber of usable bits for Worker Id. 5 by default, allows up to 31 WorkersYes5Worker ID for this generator
datacenterIdDatacenter ID for this generatorYes0
datacenterIdBitsNumber of usable bits for Datacenter Id. 5 by default, allows up to 31 DatacentersYes5
sequenceFor every ID that is generated on that process, this number is incrementedYes0
sequenceBitsNumber of usable bits for Sequence Id. 12 by default, allows up to 4095 generations per millisecond per Worker per DatacenterYes12

Useful Links

License

Refer to the LICENSE file.

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago