UUID v7
Time-ordered UUIDs for primary keys, APIs, and event streams.
UUID v7 keeps the UUID shape while embedding a millisecond timestamp. It is a strong default for database primary keys when you want chronological locality without creating a custom ID column.
import { uuidv7 } from 'uniku/uuid/v7'
const orderId = uuidv7()
// '019f5732-0342-75f9-9efd-fc3c1f8da7fd' (string)
uuidv7.timestamp(orderId) // 1783874323266Generated API reference
Public methods
Generate a UUID v7 string or write the bytes into a buffer. UUID v7 is a time-ordered UUID that embeds a Unix timestamp in milliseconds, making IDs naturally sortable by creation time. Ideal for database primary keys where chronological ordering improves index performance.
- uuidv7
Generate a time-ordered UUID v7 string.
uuidv7(): stringGenerate a UUID v7 with explicit options or write its 16 canonical bytes into a caller-owned buffer.
uuidv7<TBuf extends Uint8Array = Uint8Array>(options: UuidV7Options | undefined, buf: TBuf, offset?: number): TBufGenerate a UUID v7 string with optional timestamp, sequence, or random bytes.
uuidv7(options?: UuidV7Options, buf?: undefined, offset?: number): stringExampleuuidv7()// => '019f5732-0342-75f9-9efd-fc3c1f8da7fd'- toBytes
Convert a UUID v7 string to its canonical 16-byte representation.
toBytes(id: string): Uint8ArrayExampleuuidv7.toBytes('019f5732-0342-75f9-9efd-fc3c1f8da7fd')// => Uint8Array [1, 159, 87, 50, 3, 66, 117, 249, 158, 253, 252, 60, 31, 141, 167, 253]- fromBytes
Convert 16 canonical UUID bytes to a UUID v7 string.
fromBytes(bytes: Uint8Array): stringExampleuuidv7.fromBytes(Uint8Array.from([1, 159, 87, 50, 3, 66, 117, 249, 158, 253, 252, 60, 31, 141, 167, 253]))// => '019f5732-0342-75f9-9efd-fc3c1f8da7fd'- timestamp
Read the embedded Unix timestamp in milliseconds.
timestamp(id: string): numberExampleuuidv7.timestamp('019f5732-0342-75f9-9efd-fc3c1f8da7fd')// => 1783874323266- isValid
Return whether a value is a syntactically valid UUID v7 string.
isValid(id: unknown): id is stringExampleuuidv7.isValid('019f5732-0342-75f9-9efd-fc3c1f8da7fd')// => true- NIL
The nil UUID (all zeros)
NIL: stringExampleuuidv7.NIL// => '00000000-0000-0000-0000-000000000000'- MAX
The max UUID (all ones)
MAX: stringExampleuuidv7.MAX// => 'ffffffff-ffff-ffff-ffff-ffffffffffff'