Open History to see the last commit
1import { drizzle } from 'drizzle-orm/d1'
2
3import * as schema from './schema.ts'
4
5export interface DbEnv {
6 DB: D1Database
7}
8
9// Workers have no module-scope env: the D1 binding only exists on the env
10// object passed to fetch/scheduled handlers, so the db client is built per
11// request rather than exported as a singleton.
12export function getDb(env: DbEnv) {
13 return drizzle(env.DB, { schema })
14}
15
16export type Db = ReturnType<typeof getDb>