SnowUUID is a distributed UUID generator that implements the Snowflake algorithm, designed to create unique identifiers that can be generated across multiple nodes without collision. Each generated ID contains timestamp, worker ID, datacenter ID, and a sequence number.
- Unique ID Generation: Ensures that IDs are unique across distributed systems.
- Timestamp Included: Each ID contains a timestamp, allowing for chronological sorting.
- Configurable: Custom configurations for epoch, worker ID, and datacenter ID.
- Scalable: Supports multiple workers and datacenters, making it suitable for large applications.
To use SnowUUID in your TypeScript project, follow these steps:
npm install snowuuid
Here's a simple example demonstrating how to use the SnowUUID
class:
import { SnowUUID } from 'snowuuid';
// Create a new SnowUUID instance with default settings
const uuidGenerator = new SnowUUID();
// Generate unique IDs
const id1 = uuidGenerator.nextId();
console.log(`Generated ID 1: ${id1}`);
const id2 = uuidGenerator.nextId();
console.log(`Generated ID 2: ${id2}`);
You can create a SnowUUID
instance with custom settings for epoch, worker ID, and datacenter ID:
const customUUIDGenerator = new SnowUUID({
epoch: 1672531200000n, // Custom epoch (January 1, 2023)
workerId: 1n, // Worker ID
datacenterId: 1n // Datacenter ID
});
const customId = customUUIDGenerator.nextId();
console.log(`Custom Generated ID: ${customId}`);
The SnowUUID
class throws errors in specific scenarios, such as:
- If the system clock moves backwards.
- If the worker ID or datacenter ID exceeds their maximum limits.
You can also include tests using Jest to ensure that your implementation works correctly. Here is a sample test suite:
import { SnowUUID } from '../src/index';
describe('SnowUUID Generator', () => {
let uuidGenerator: SnowUUID;
beforeEach(() => {
uuidGenerator = new SnowUUID();
});
test('generates a unique ID each time', () => {
const id1 = uuidGenerator.nextId();
const id2 = uuidGenerator.nextId();
expect(id1).not.toBe(id2); // IDs should be unique
});
// Additional tests can be added here...
});
Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.
This project is licensed under the MIT License.
- Ravi Kishan - ravikishan63392@gmail.com