No description
Find a file
2024-02-06 07:36:38 +01:00
src typescript 2024-02-06 07:26:25 +01:00
.gitignore typescript 2024-02-06 07:26:25 +01:00
.npmignore typescript 2024-02-06 07:26:25 +01:00
babel.config.js typescript 2024-02-06 07:26:25 +01:00
jest.config.js typescript 2024-02-06 07:26:25 +01:00
package.json i need to change name becouse: Package name too similar to existing package 2024-02-06 07:30:10 +01:00
README.md add example output to readme 2024-02-06 07:36:38 +01:00
rollup.config.js typescript 2024-02-06 07:26:25 +01:00
tsconfig.json typescript 2024-02-06 07:26:25 +01:00
yarn.lock typescript 2024-02-06 07:26:25 +01:00

@skorotkiewicz/snowflake-id

Snowflake ID is a unique identifier commonly used in distributed systems to generate unique IDs with a timestamp component. It is designed to ensure uniqueness, even in distributed and highly concurrent environments.

The Snowflake ID typically consists of three components:

  1. Timestamp: Representing the time when the ID was generated.
  2. Machine ID: Identifying the machine or node that generated the ID.
  3. Sequence Number: Ensuring uniqueness in cases of multiple IDs generated within the same millisecond.

By combining these components, Snowflake IDs provide a reliable way to generate globally unique identifiers, making them valuable for applications such as distributed databases, messaging systems, and more.

Install

yarn add @skorotkiewicz/snowflake-id
or
npm i @skorotkiewicz/snowflake-id

Usage

import { Snowflake, decodeSnowflake } from "@skorotkiewicz/snowflake-id";

(async () => {
  const machineId = 1; // machine ID (0-1023)
  const snowflake = new Snowflake(machineId);

  const id1 = await snowflake.generate();
  console.log("encodeID", id1);
  // output: 7160521316708126720

  const decoded = decodeSnowflake(id1);
  console.log("decodeID", decoded);
  // output: { timestamp: '2024-02-06T05:12:47.730Z', machineId: '1', sequence: '0' }
})();
};