1.1.3 • Published 2 months ago

@loxjs/snowflake v1.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
2 months ago

@loxjs/snowflake

@loxjs/snowflake is a Node.js module that generates unique, distributed, time-sorted identifiers using the Twitter Snowflake algorithm. Snowflake IDs are composed of a timestamp, a data center identifier, a worker identifier, and a sequence number.

Features

  • Generates unique IDs in a distributed environment.
  • IDs are time-based and sorted.
  • Can extract timestamp, data center ID, and worker ID from generated IDs.
  • High-performance ID generation (over 1 million IDs per second).

Installation

Install the module with npm:

npm install @loxjs/snowflake

Or with yarn:

yarn add @loxjs/snowflake

Usage

To use @loxjs/snowflake, create a new instance of the Snowflake class with optional configuration parameters.

const Snowflake = require('@loxjs/snowflake');

// Create a Snowflake ID generator with custom settings
const snowflake = new Snowflake({
  epoch: 1546272000000, // Custom epoch start time (default is 2019/1/1)
  dataCenterId: 1, // Data center identifier
  workerId: 1, // Worker identifier
  sequence: 0 // Initial sequence number
});

// Generate a Snowflake ID
const id = snowflake.nextId();
console.log(`Generated Snowflake ID: ${id}`);

Validating Snowflake IDs

The module provides a method to validate if a given value is a valid Snowflake ID.

// Check if a given value is a valid Snowflake ID
const isValid = snowflake.isId(id);
console.log(`Is valid Snowflake ID: ${isValid}`);

Example: Handling ID Generation

try {
  const id = snowflake.nextId();
  console.log(`Generated Snowflake ID: ${id}`);
} catch (error) {
  console.error('Error generating Snowflake ID:', error);
}

Contributing

Contributions to @loxjs/snowflake are welcome! Please ensure that your contributions adhere to the following guidelines:

  • Write clear, readable, and maintainable code.
  • Follow existing coding styles and practices.
  • Write meaningful commit messages.
  • Update the documentation accordingly.

For more detailed information, please read the contributing guide.

Enjoy using @loxjs/snowflake!

1.1.3

2 months ago

1.1.2

2 months ago

1.1.1

2 months ago

1.1.0

2 years ago

1.0.1

5 years ago

1.0.0

5 years ago