Snowflake
Introduction
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.
Installation
- npm
- yarn
- pnpm
npm install @sapphire/snowflake
yarn add @sapphire/snowflake
pnpm add @sapphire/snowflake
Usage
Constructing a Snowflake
- CommonJS
- ESM
- TypeScript
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
// Import the Snowflake class
import { Snowflake } from '@sapphire/snowflake';
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
// Import the Snowflake class
import { Snowflake } from '@sapphire/snowflake';
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Generate a snowflake with the given epoch
const uniqueId = snowflake.generate();
Constructing a Discord Snowflake
- CommonJS
- ESM
- TypeScript
// Import the Snowflake class
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
// Import the Snowflake class
import { DiscordSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
// Import the Snowflake class
import { DiscordSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.generate();
Constructing a Twitter Snowflake
- CommonJS
- ESM
- TypeScript
// Import the Snowflake class
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
// Import the Snowflake class
import { TwitterSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
// Import the Snowflake class
import { TwitterSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Twitter's epoch
const uniqueId = TwitterSnowflake.generate();
Deconstructing Snowflake
- CommonJS
- ESM
- TypeScript
// Import the Snowflake class
const { Snowflake } = require('@sapphire/snowflake');
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
// Import the Snowflake class
import { Snowflake } from '@sapphire/snowflake';
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
// Import the Snowflake class
import { Snowflake } from '@sapphire/snowflake';
// Define a custom epoch
const epoch = new Date('2000-01-01T00:00:00.000Z');
// Create an instance of Snowflake
const snowflake = new Snowflake(epoch);
// Deconstruct a snowflake with the given epoch
const uniqueId = snowflake.deconstruct('3971046231244935168');
Deconstructing Discord Snowflake
- CommonJS
- ESM
- TypeScript
const { DiscordSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
import { DiscordSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
import { DiscordSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = DiscordSnowflake.deconstruct('3971046231244935168');
Deconstructing Twitter Snowflake
- CommonJS
- ESM
- TypeScript
const { TwitterSnowflake } = require('@sapphire/snowflake');
// Generate a snowflake with Discord's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
import { TwitterSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');
import { TwitterSnowflake } from '@sapphire/snowflake';
// Generate a snowflake with Discord's epoch
const uniqueId = TwitterSnowflake.deconstruct('3971046231244935168');