Bun is a JavaScript runtime, package manager, and test runner built for speed. UUID v7 generation is synchronous and adds no overhead to request handling. See UUID in SQLite for the SQLite storage pattern and UUID in PostgreSQL for the PostgreSQL pattern.

Setup

bun init
bun add uuid
bun add --dev @types/uuid

bun:sqlite with UUID v7

import { Database } from 'bun:sqlite';
import { v7 as uuidv7 } from 'uuid';

const db = new Database('orders.db');

db.run(`
  CREATE TABLE IF NOT EXISTS orders (
    id          TEXT PRIMARY KEY,
    customer_id TEXT NOT NULL,
    total       REAL NOT NULL,
    status      TEXT NOT NULL DEFAULT 'pending'
  )
`);

// Insert with UUID v7 primary key
function createOrder(customerId: string, total: number): string {
  const id = uuidv7();
  db.run(
    'INSERT INTO orders (id, customer_id, total) VALUES (?, ?, ?)',
    [id, customerId, total]
  );
  return id;
}

// Cursor pagination — UUID v7 lex sort = chronological
function listOrders(cursor?: string, limit = 20) {
  const stmt = cursor
    ? db.prepare('SELECT id, total, status FROM orders WHERE id > ? ORDER BY id LIMIT ?')
    : db.prepare('SELECT id, total, status FROM orders ORDER BY id LIMIT ?');

  const rows = cursor ? stmt.all(cursor, limit) : stmt.all(limit);
  return {
    items:      rows,
    nextCursor: rows.length === limit ? (rows.at(-1) as any)?.id : null,
  };
}

Bun.sql (native PostgreSQL client)

import { sql } from 'bun';
import { v7 as uuidv7 } from 'uuid';

// Bun.sql uses DATABASE_URL env var automatically
async function createOrderPg(customerId: string, total: number) {
  const id = uuidv7();

  await sql`
    INSERT INTO orders (id, customer_id, total)
    VALUES (${id}::uuid, ${customerId}::uuid, ${total})
  `;
  return id;
}

async function listOrdersPg(cursor?: string, limit = 20) {
  const rows = cursor
    ? await sql`
        SELECT id, total, status FROM orders
        WHERE id > ${cursor}::uuid ORDER BY id LIMIT ${limit}
      `
    : await sql`
        SELECT id, total, status FROM orders
        ORDER BY id LIMIT ${limit}
      `;

  return {
    items:      rows,
    nextCursor: rows.length === limit ? rows.at(-1)?.id : null,
  };
}

Bun HTTP server with UUID v7

import { v7 as uuidv7 } from 'uuid';

Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (req.method === 'POST' && url.pathname === '/orders') {
      const body = await req.json();
      const id   = createOrder(body.customer_id, body.total);
      return Response.json({ id, status: 'pending' }, { status: 201 });
    }

    if (req.method === 'GET' && url.pathname === '/orders') {
      const cursor = url.searchParams.get('cursor') ?? undefined;
      const result = listOrders(cursor);
      return Response.json(result);
    }

    return new Response('Not Found', { status: 404 });
  },
});

Further reading

External references

Frequently asked questions

How do I use UUID v7 with bun:sqlite?

Install the uuid npm package with bun add uuid. In your Bun project, import { v7 as uuidv7 } from 'uuid'. Use const db = new Database('app.db') from bun:sqlite and insert with db.run("INSERT INTO orders (id) VALUES (?)", [uuidv7()]). UUID v7 stores as TEXT in SQLite. See UUID in SQLite.

What is Bun.sql?

Bun.sql is Bun's native PostgreSQL client introduced in Bun 1.1. It provides a template-literal API: await sql\`INSERT INTO orders (id) VALUES (${uuidv7()})\`. It handles connection pooling, parameter binding, and result mapping with no additional dependencies. See UUID in PostgreSQL.

Is Bun compatible with the uuid npm package?

Yes. Bun has full Node.js module compatibility. The uuid npm package works without modification in Bun projects. bun add uuid installs it from the npm registry. UUID v7 generation is synchronous and has no platform-specific dependencies.