NodeJS SnowflakeIds
Installation
npm install @whackdevelopment/snowflakeid
generates unique 64-bit snowflake IDs inspired by Twitter and Discord IDs
@whackdevelopment/snowflakeid
NPMJSUsage
Import Snowflake
Common NodeJS
const { Snowflake, SnowflakeGenerator } = require('@whackdevelopment/snowflakeid'); /* nodejs only */
NodeJS ES Module
import { Snowflake, SnowflakeGenerator } from '@whackdevelopment/snowflakeid';
Create a new SnowflakeGenerator
const flakeGenerator = new SnowflakeGenerator({
machineId: 1, // optional, define machine id (defaults to 1)
timeOffset: 0 // optional, define a offset time (defaults to 0)
});
Options
machineId: (Defaults to 1) A machine id or any random id. If you are generating id in distributed system, its highly advised to provide a proper machineId which is unique to different machines.
timeOffset: (Defaults to 0) Time offset will be subtracted from current time to get the first 42 bit of id. This help in generating smaller ids. ( not recommended )
Generate a Snowflake
const id1 = flakeGenerator.generate(); // returns something like 112867124767768576
const id2 = flakeGenerator.generate(); // returns something like 112867124784545792
Create a new Snowflake from a Snowflake String
const id1Clone = new Snowflake(id1.toString()); // or
const id2Clone = new Snowflake(id2);