snowflake-id1.1.0 • Public • Published
A tiny module to generate time based 64-bit unique id, inspired by Twitter id (snowflake).
npm install snowflake-id
var SnowflakeId = ;// Initialize snowflakevar snowflake =mid : 42offset : 2019-1970*31536000*1000;
Create a instance of snowflake as shown above which will be used to generate snowflake ids afterward.
Snowflake ID generation
var id1 = snowflake; // returns something along the lines of "285124269753503744"var id2 = snowflake; // returns something along the lines of "285124417543999488"
mid : (Default to 1) A machine id or any random number. If you are generating the id in a distributed system, its highly advised to provide a proper mid which is unique to the different machines.
offset : (Defaults to 0) This is a time offset which will be subtracted from current time to get the first 42 bits of the id. This will help in generating smaller ids.
generate : Method to generate id from Snowflake instance.