What is @sapphire/snowflake?
@sapphire/snowflake is a utility library for generating and parsing unique snowflake IDs, which are often used in distributed systems to ensure unique identifiers. It is particularly useful in applications that require high-scale, unique ID generation.
What are @sapphire/snowflake's main functionalities?
Generate Snowflake ID
This feature allows you to generate a unique snowflake ID. The generated ID is a 64-bit integer that is unique and can be used as an identifier in distributed systems.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
console.log(id);
Parse Snowflake ID
This feature allows you to parse a snowflake ID to extract its components, such as the timestamp, worker ID, and process ID. This can be useful for debugging or analyzing the IDs.
const { Snowflake } = require('@sapphire/snowflake');
const snowflake = new Snowflake();
const id = snowflake.generate();
const parsed = snowflake.deconstruct(id);
console.log(parsed);
Custom Epoch
This feature allows you to set a custom epoch for the snowflake ID generation. The epoch is the starting point for the timestamp component of the ID, and setting a custom epoch can be useful for aligning IDs with specific events or timeframes.
const { Snowflake } = require('@sapphire/snowflake');
const customEpoch = 1609459200000; // Custom epoch (e.g., January 1, 2021)
const snowflake = new Snowflake(customEpoch);
const id = snowflake.generate();
console.log(id);
Other packages similar to @sapphire/snowflake
snowflake-id
The 'snowflake-id' package is another library for generating unique snowflake IDs. It offers similar functionality to @sapphire/snowflake, including ID generation and parsing. However, it may have different performance characteristics and configuration options.
flake-idgen
The 'flake-idgen' package is a lightweight library for generating unique IDs using the snowflake algorithm. It is designed to be fast and efficient, making it a good alternative to @sapphire/snowflake for applications that require high-throughput ID generation.
node-snowflake
The 'node-snowflake' package is a simple implementation of the snowflake ID generation algorithm. It provides basic functionality for generating and parsing snowflake IDs, similar to @sapphire/snowflake, but may lack some of the advanced features and customization options.
Table of Contents
Description
There is often a need to get a unique ID for entities, be that for Discord messages/channels/servers, keys in a database or many other similar examples. There are many ways to get such a unique ID, and one of those is using a so-called "snowflake". You can read more about snowflake IDs in this Medium article.
Features
- Written in TypeScript
- Bundled with esbuild so it can be used in NodeJS and browsers
- Offers CommonJS, ESM and UMD bundles
- Offers predefined epochs for Discord and Twitter
- Fully tested
Installation
You can use the following command to install this package, or replace npm install
with your package manager of choice.
npm install @sapphire/snowflake
Usage
Note: While this section uses require
, the imports match 1:1 with ESM imports. For example const { Snowflake } = require('@sapphire/snowflake')
equals import { Snowflake } from '@sapphire/snowflake'
.
Constructing snowflakes
Snowflakes with custom epoch
const { Snowflake } = require('@sapphire/snowflake');
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch);
const uniqueId = snowflake.generate();
Snowflake with Discord epoch constant
const { DiscordSnowflake } = require('@sapphire/snowflake');
const uniqueId = DiscordSnowflake.generate();
const uniqueId = DiscordSnowflake.generate();
const { TwitterSnowflake } = require('@sapphire/snowflake');
const uniqueId = TwitterSnowflake.generate();
const uniqueId = TwitterSnowflake.generate();
Deconstructing snowflakes
Snowflakes with custom epoch
const { Snowflake } = require('@sapphire/snowflake');
const epoch = new Date('2000-01-01T00:00:00.000Z');
const snowflake = new Snowflake(epoch);
const uniqueId = snowflake.deconstruct('3971046231244935168');
Snowflake with Discord epoch constant
const { DiscordSnowflake } = require('@sapphire/snowflake');
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
const { TwitterSnowflake } = require('@sapphire/snowflake');
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
Buy us some doughnuts
Sapphire Community is and always will be open source, even if we don't get donations. That being said, we know there are amazing people who may still want to donate just to show their appreciation. Thank you very much in advance!
We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub Sponsorships. You can use the buttons below to donate through your method of choice.
Contributors
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to Sapphire!