db
The Drizzle client for your app’s own SQLite database, imported from app/db together with table builders and query operators.
Usage
app/orders/repository.ts
import { db, eq, desc } from "app/db";
import { orders } from "./orders.table";
export async function recentOrders(customerId: number) {
return db.select().from(orders)
.where(eq(orders.customerId, customerId))
.orderBy(desc(orders.createdAt))
.limit(10);
}
export async function markPaid(id: number) {
const [row] = await db.update(orders).set({ status: "paid" }).where(eq(orders.id, id)).returning();
return row;
}Keep queries in repository.ts; handlers call the repository functions.
Signature
app/db.ts
export const db; // Drizzle client, created on first use
// Table builders (used in *.table.ts)
export { sqliteTable, integer, text, real, blob, index, uniqueIndex, primaryKey, foreignKey, unique, alias };
// Operators and aggregates
export { eq, ne, lt, lte, gt, gte, like, ilike, notLike, notIlike, and, or, not, isNull, isNotNull,
inArray, notInArray, between, notBetween, exists, notExists, asc, desc, sql,
count, countDistinct, sum, sumDistinct, avg, avgDistinct, min, max };
// Manh helpers
export function schemaOf(table);
export function listSchema(opts?);
export function listOf(table, query, opts?);
export function insertMany(table, rows);
export function scopeBy(user, rules);manh.run generates app/db.ts on every deploy. Don’t write it. Importing any other name is a check error, since the build would fail; write anything else with the sql template.
Parameters
None. db is an object, not a function.
Returns
db has Drizzle’s full query API: select, insert, update, delete, transaction. Results are table rows, typed from the declarations in *.table.ts.
Examples
Write and get the written row back with .returning():
app/orders/repository.ts
const [created] = await db.insert(orders).values({ customerName, total }).returning();When several writes must all succeed or none: a synchronous callback, no async, no await, each statement ending in .run(), .get() or .all().
app/orders/repository.ts
let order!: typeof orders.$inferSelect;
db.transaction((tx) => {
order = tx.insert(orders).values(row).returning().get();
tx.insert(items).values(lines.map((l) => ({ ...l, orderId: order.id }))).run();
});
return order;- Never write
db.transaction(async (tx) => …): statements after the firstawaitrun outside the transaction, and an error doesn’t undo the writes already made. db.transaction(...)always returns{}. Pass results out through a variable, as above.- An app’s requests run one at a time. Read, check and write back to back and you need no transaction. But while a handler waits on a call that leaves the app (
fetch, email, AI, file storage), other requests run in between: send mail and call AI after the writes. - One statement takes at most 100 bound values (
?). A multi-rowdb.insert(t).values([...])is split by manh.run into several statements in one transaction, so you don’t batch by hand.insertMany(table, rows)does the same and returns every inserted row. - SQLite has no boolean type: use
integer0/1.
Next steps
- Database: declaring tables, schema changes on deploy.
- schemaOf: zod schemas for create and update bodies, from the table.
- listSchema, listOf: a list endpoint with paging, search and filters in one call.
Last updated