UUIDv7 Generator

1.2.1 · active · verified Sun Apr 19

uuidv7 is a JavaScript library providing a robust implementation of Universally Unique Identifiers (UUID) version 7, as defined by RFC 9562. UUIDv7 introduces a time-ordered component, making the generated IDs monotonically increasing and thus highly suitable for database primary keys or indexed fields where sequential writes and improved locality are beneficial, unlike traditional UUIDv4. The current stable version is 1.2.1. It also includes support for UUIDv4 generation and offers an object representation for UUIDs, allowing programmatic inspection and comparison. The library is designed to handle clock rollbacks and counter overflows gracefully to maintain monotonicity, with a large 42-bit counter and mechanisms to increment the internal timestamp if the counter overflows within a millisecond.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates generating UUIDv7 and UUIDv4 strings, and creating a UUIDv7 object for inspection and comparison.

import { uuidv7, uuidv4, uuidv7obj } from 'uuidv7';

// Generate a UUIDv7 string
const myUuidV7 = uuidv7();
console.log('Generated UUIDv7:', myUuidV7);
// Expected output: e.g., '018f3a2c-e5b0-7d1a-8c9d-1b2c3d4e5f60'

// Generate a UUIDv4 string
const myUuidV4 = uuidv4();
console.log('Generated UUIDv4:', myUuidV4);
// Expected output: e.g., 'a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d'

// Generate a UUIDv7 object for more detailed inspection
const uuidObject = uuidv7obj();
console.log('UUIDv7 Object (string representation):', String(uuidObject));
console.log('UUIDv7 Object (byte array):', uuidObject.bytes);
console.assert(uuidObject.getVersion() === 7);
console.assert(uuidObject.getVariant() === 'VAR_10');

// Example of object comparison
const anotherUuidObject = uuidv7obj();
console.assert(uuidObject.compareTo(anotherUuidObject) < 0); // Should be true as uuidObject was generated earlier

view raw JSON →