function

randomUUIDv7

function randomUUIDv7(
encoding?: 'base64' | 'base64url' | 'hex',
timestamp?: number | Date
): string;

Generate a UUIDv7, a sequential ID based on the current timestamp with a random component.

When the same timestamp is used multiple times, a monotonically increasing counter is appended to allow sorting. The final 8 bytes are cryptographically random. When the timestamp changes, the counter resets to a pseudo-random integer.

@param encoding

Output encoding for the UUID

@param timestamp

Unix timestamp in milliseconds, defaults to Date.now()

import { randomUUIDv7 } from "bun";
const array = [
  randomUUIDv7(),
  randomUUIDv7(),
  randomUUIDv7(),
];
// [
//   "0192ce07-8c4f-7d66-afec-2482b5c9b03c",
//   "0192ce07-8c4f-7d67-805f-0f71581b5622",
//   "0192ce07-8c4f-7d68-8170-6816e4451a58"
// ]
function randomUUIDv7(
encoding: 'buffer',
timestamp?: number | Date
): Buffer;

Generate a UUIDv7 as a Buffer.

@param encoding

Pass "buffer" to get the UUID as bytes instead of a string

@param timestamp

Unix timestamp in milliseconds, defaults to Date.now()