Bun 1.x ships a full Web Crypto implementation including crypto.randomUUID() — UUID v4 generation with no dependencies. The uuid npm package installs and runs in Bun without modification, giving UUID v7 with a monotonic counter. This article covers both paths plus Bun-specific patterns for bun:sqlite and the Elysia framework.
UUID v4 with built-in crypto
// No imports — crypto.randomUUID() is global in Bun
const id = crypto.randomUUID();
// → "550e8400-e29b-41d4-a716-446655440000"
Bun’s crypto.randomUUID() uses the OS CSPRNG — identical entropy to Node.js and browser implementations. See the full runtime support matrix for version requirements.
UUID v7 with the uuid package
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7();
// → "018fbe3a-4c5d-7b12-8abc-0123456789ab"
Install once: bun add uuid. The uuid package uses a per-process monotonic counter for within-millisecond ordering — see Monotonic Counter for why this matters.
bun:sqlite with UUID v7 primary keys
import { Database } from 'bun:sqlite';
import { v7 as uuidv7 } from 'uuid';
const db = new Database('app.db');
db.run(`CREATE TABLE IF NOT EXISTS orders (
id TEXT PRIMARY KEY,
status TEXT NOT NULL
)`);
const insert = db.prepare('INSERT INTO orders (id, status) VALUES (?, ?)');
// UUID v7 — time-sortable, works as TEXT in SQLite
insert.run(uuidv7(), 'pending');
For BLOB storage (16 bytes, faster range scans), convert with the hex pattern from UUID in SQLite — TEXT vs BLOB. UUID v7 in TEXT format still sorts chronologically because the timestamp prefix is at position 0.
Elysia framework
import { Elysia } from 'elysia';
import { v7 as uuidv7 } from 'uuid';
const app = new Elysia()
.post('/orders', ({ body }) => {
const orderId = uuidv7(); // time-sortable primary key
// persist to DB...
return { id: orderId };
})
.listen(3000);
Elysia runs on Bun natively. Assign UUID v7 at the handler layer before any database insert — this gives you the ID for logging and response before the DB round-trip completes. See UUID in JavaScript for the broader Node.js/Bun pattern.
Bun test runner
import { expect, test } from 'bun:test';
import { validate, version } from 'uuid';
test('order ID is a valid UUID v7', () => {
const id = uuidv7();
expect(validate(id)).toBe(true);
expect(version(id)).toBe(7);
});
The uuid package’s validate() and version() helpers work in Bun’s test runner without config. See UUID Testing Strategies for fixture and mock patterns.
Further reading
- crypto.randomUUID() runtime support
- UUID in SQLite — TEXT vs BLOB
- UUID in JavaScript — full guide
- UUID in Databases — storage and indexing
- Monotonic Counter — within-millisecond ordering
External references
Frequently asked questions
Does Bun support crypto.randomUUID() natively?
Yes. Bun implements the Web Crypto API including crypto.randomUUID() — no package needed for UUID v4. Bun also supports the uuid npm package without modification, so import { v7 as uuidv7 } from 'uuid' works identically to Node.js. See crypto.randomUUID() runtime support matrix.
How do I use UUID v7 as a primary key in Bun's built-in SQLite?
Bun ships bun:sqlite — a native SQLite binding. Store UUID v7 as TEXT (36 chars) or BLOB (16 bytes). TEXT is simpler; BLOB is 56% smaller and faster for index scans. Generate the UUID v7 in application code using the uuid package and pass it as a parameter. See UUID in SQLite for the BLOB conversion pattern.