No description
Find a file
2024-02-06 07:26:25 +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 typescript 2024-02-06 07:26:25 +01:00
README.md typescript 2024-02-06 07:26:25 +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

snowflakeid

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 snowflakeid
or
npm i snowflakeid

Usage

import { Snowflake, decodeSnowflake } from "snowflakeid";

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

  const id1 = await snowflake.generate();
  console.log("encodeID", id1);

  const decoded = decodeSnowflake(id1);
  console.log("decodeID", decoded);
})();
};