The functions a feature calls for its rows, written for the chosen ORM. A feature router calls these instead of the ORM, so a change of ORM leaves the router as it is.
123456789101112131415161718192021222324252627282930313233import { eq } from "drizzle-orm";import { note, todo } from "./schema";import type { db as client } from "./client";
type Db = typeof client;
/** The row a write returned; a write that returns none did not happen. */function first<T>(rows: readonly T[]): T { const [row] = rows; if (row === undefined) throw new Error("The write returned no row"); return row;}
const todoRepo = (db: Db) => ({ list: async () => db.select().from(todo), add: async (text: string) => first(await db.insert(todo).values({ text }).returning()), toggle: async (id: number, done: boolean) => first(await db.update(todo).set({ done }).where(eq(todo.id, id)).returning()),});
const notesRepo = (db: Db) => ({ list: async (userId: string) => db.select().from(note).where(eq(note.userId, userId)), add: async (userId: string, body: string) => first(await db.insert(note).values({ body, userId }).returning()),});
/** The registry: wires each port to the adapter the chosen ORM writes for it. */export function createRepos(db: Db) { return { todo: todoRepo(db), notes: notesRepo(db), };}