Skip to content

A McEvert package for creating Snowflake ID's.

License

Notifications You must be signed in to change notification settings

mcevert/snowflake

Repository files navigation


McEvert Snowflake ☃️



About 📘

This repository households the source code for the McEvert Snowflake package. We utilize Twitter's snowflake ❄️ format for uniquely identifiable descriptors (IDs).

Snowflake architecture ☃️

Snowflake IDs are 64-bit unsigned integers that are roughly sortable by time. Because of their length, snowflakes are always stored as strings. Snowflakes are composed of 4 parts:

  • Timestamp, milliseconds since 1 January 2023 00:00:00 UTC
  • Internal worker ID
  • Internal process ID
  • Increment, for every ID that is generated on that process

Installation 📦

# With npm
npm install @mcevert/snowflake

# With yarn
yarn add @mcevert/snowflake

Usage 🚀

Create new snowflake

It is time to create your first snowflake ID. You can do this by creating a new instance of the Snowflake class and calling the generate method.

import Snowflake from '@mcevert/snowflake';

const snowflake = new Snowflake();

// You can use this ID variable to identify a user, a message, a channel, etc.
const id = snowflake.generate();

Parse existing snowflakes

You can also parse existing snowflakes. This is useful when you want more information about a snowflake ID.

import Snowflake from '@mcevert/snowflake';

const snowflakeInfo = Snowflake.parse('131557566537793536');
// {
//   createdAt: 2023-01-01T00:00:00.000Z,
//   workerId: 0,
//   processId: 0
// }

License 📜

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.