npm install @whackdevelopment/snowflakeid
const { Snowflake, SnowflakeGenerator } = require('@whackdevelopment/snowflakeid'); /* nodejs only */
import { Snowflake, SnowflakeGenerator } from '@whackdevelopment/snowflakeid';
const flakeGenerator = new SnowflakeGenerator({
machineId: 1, // optional, define machine id (defaults to 1)
timeOffset: 0 // optional, define a offset time (defaults to 0)
});
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 )
const id1 = flakeGenerator.generate(); // returns something like 112867124767768576
const id2 = flakeGenerator.generate(); // returns something like 112867124784545792
const id1Clone = new Snowflake(id1.toString()); // or
const id2Clone = new Snowflake(id2);