@troplo/snowflake
v1.1.0
Published
Troplo standardized snowflake ID generator library
Readme
Troplo Simple Snowflake Generator Library
This library is used for generating Snowflake IDs in Troplo & Flowinity projects.
ID format
- timestamp:
- 41 bits
- serverId:
- 10 bits
- sequence:
- 12 bits
It is up to the program implementation to parse and implement the serverId. It will default to process.env.SERVER_ID or 1 unless configured with configureID.
If the maximum sequence is reached, the generator will wait for the next millisecond and start from 0.
Usage/Methods
configureID
Run configureID before using the generator.
import { configureID } from '@troplo/snowflake';
configureID({
serverId: 1
});generateID
import { generateID } from '@troplo/snowflake';
const id = generateID(); // Returns a 64-bit integer (SnowflakeID)parseID
import { parseID } from '@troplo/snowflake';
const id = generateID();
const parsed = parseID(id); // Returns a DecodedSnowflakeID with the following properties:
// {
// timestamp: bigint
// serverId: bigint
// sequence: bigint
// }