File
Blob: src/worker/db/d1/client.ts
| 1 | import { drizzle, type DrizzleD1Database } from "drizzle-orm/d1"; |
| 2 | |
| 3 | import * as schema from "@/worker/db/d1/schema"; |
| 4 | |
| 5 | export type D1Executor = D1Database | D1DatabaseSession; |
| 6 | export type D1Db<TClient extends D1Executor = D1Executor> = DrizzleD1Database<typeof schema> & { $client: TClient }; |
| 7 | |
| 8 | export function createD1Db(executor: D1Database): D1Db<D1Database>; |
| 9 | export function createD1Db(executor: D1DatabaseSession): D1Db<D1DatabaseSession>; |
| 10 | export function createD1Db(executor: D1Executor): D1Db { |
| 11 | if ("withSession" in executor) { |
| 12 | return drizzle(executor, { schema }); |
| 13 | } |
| 14 | |
| 15 | return drizzle(executor, { schema }); |
| 16 | } |
| 17 | export type D1DbExecutor = D1Db; |