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.
1. Install
Section titled “1. Install”pnpm add @lumbung/client drizzle-ormpnpm add -D drizzle-kit2. Define the schema
Section titled “2. Define the schema”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(),})3. Generate and apply migrations
Section titled “3. Generate and apply migrations”drizzle-kit only writes the SQL files; lumbung migrate apply runs them against a branch, each file in its own transaction.
import { defineConfig } from "drizzle-kit"
export default defineConfig({ dialect: "sqlite", schema: "./src/schema.ts", out: "./drizzle" })pnpm drizzle-kit generatelumbung migrate apply --db app --dir drizzleSee Run migrations for how applied files are tracked.
4. Query
Section titled “4. Query”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.
Errors
Section titled “Errors”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 }}Branches
Section titled “Branches”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 },)