@funcd-dev/drizzle
v0.24.0
Published
Drizzle over funcd's context.sql: the sqlite-proxy database and drizzle-kit migrations applied in one execute
Readme
@funcd-dev/drizzle
Drizzle over funcd's context.sql (funcd ADR-0258): one package instead of a copied adapter
in each App.
import { applyMigrations, funcdDrizzle } from '@funcd-dev/drizzle';
import type { FunctionContext } from '@funcd-dev/shim';
import * as schema from './db/schema.ts';
export const db = (ctx: FunctionContext) => funcdDrizzle(ctx, { schema });
// the migrate function, or an App's preApply hook: the drizzle-kit folder the bundle carries
const migrations: Record<string, string> = import.meta.glob(['../../drizzle/*.sql', '../../drizzle/meta/_journal.json'], {
query: '?raw',
import: 'default',
eager: true,
});
export const handle = async (ctx: FunctionContext) => ({ applied: await applyMigrations(ctx, migrations) });funcdDrizzle(ctx, { alias = 'app', schema })is Drizzle'ssqlite-proxydatabase. ASELECTorWITHgoes tocontext.sql.queryValues, any other statement toexecute, since Drizzle sendsinsert … returning()as a read. Abatchis oneexecute, all or nothing, and is for writes: a select inside it gets no rows. AWITHthat writes (db.with(cte).insert(…)) takes the read path too, so write without aWITH.applyMigrations(ctx, files, { alias = 'app', table = '_migrations' })applies the drizzle-kit migrations the table lacks, with their rows, in oneexecute, and returns how many it applied. It reads them fromfiles, a path to content map, and keeps the table as Drizzle's ownsqlite-proxymigrator keeps it, so either one can take over from the other.drizzle-ormis a peer dependency: the App picks its version (>=0.45.0 <1.0.0).
