Skip to content

Use with Drizzle

@lumbung/client/drizzle runs Drizzle on a Lumbung branch through Drizzle’s sqlite-proxy driver. The full example lives in examples/drizzle in the repository, and CI runs it against a real engine.

Terminal window
pnpm add @lumbung/client drizzle-orm
pnpm add -D drizzle-kit
src/schema.ts
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
export const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
email: text("email").notNull().unique(),
name: text("name").notNull(),
})

drizzle-kit only writes the SQL files; lumbung migrate apply runs them against a branch, each file in its own transaction.

drizzle.config.ts
import { defineConfig } from "drizzle-kit"
export default defineConfig({ dialect: "sqlite", schema: "./src/schema.ts", out: "./drizzle" })
Terminal window
pnpm drizzle-kit generate
lumbung migrate apply --db app --dir drizzle

See Run migrations for how applied files are tracked.

import { drizzle } from "@lumbung/client/drizzle"
import { eq } from "drizzle-orm"
import * as schema from "./schema.ts"
const db = drizzle({ url: "http://127.0.0.1:4870", token: process.env.LUMBUNG_TOKEN!, database: "app" }, { schema })
// onConflictDoNothing keeps this safe to run twice.
await db.insert(schema.users).values({ email: "ana@example.com", name: "Ana" }).onConflictDoNothing()
const ana = await db.select().from(schema.users).where(eq(schema.users.email, "ana@example.com")).get()

db.batch([...]) runs as one Lumbung batch: one transaction, all or nothing. db.transaction() isn’t supported: every request is already its own transaction, so it fails with a LumbungError of code unsupported as the cause. Use db.batch for statements that must succeed together.

Drizzle wraps driver errors in its own DrizzleQueryError. The LumbungError, with its stable code, is the cause:

try {
await db.insert(schema.users).values({ email: "ana@example.com", name: "Again" })
} catch (err) {
if ((err as { cause?: { code?: string } }).cause?.code === "constraint_violation") {
// duplicate email
} else {
throw err
}
}

Pass branch to work on a branch, for example to try a migration first:

const preview = drizzle(
{ url: "http://127.0.0.1:4870", token: process.env.LUMBUNG_TOKEN!, database: "app", branch: "pr-42" },
{ schema },
)