diff --git a/src/db/scoped.ts b/src/db/scoped.ts index 72176fb..9e3c1f1 100644 --- a/src/db/scoped.ts +++ b/src/db/scoped.ts @@ -1,38 +1,3 @@ import { sql, dbUnscoped } from "./index"; -import { validateSession, requireFamily } from "@/lib/auth"; -/** - * Run a callback within a Postgres transaction where - * app.current_family_id is set. ALL data queries must go through this. - */ -export async function withFamilyContext( - familyId: string, - callback: (tx: typeof sql) => Promise -): Promise { - return await sql.begin(async (tx) => { - await tx`SELECT set_config('app.current_family_id', ${familyId}, true)`; - return await callback(tx); - }); -} - -/** - * Convenience: get session + scoped client in one call. - * Use this in API routes instead of importing sql directly. - */ -export async function getScopedDb() { - const auth = await requireFamily(); - if (!auth.success) { - throw new NextResponse.json({ error: auth.error }, { status: auth.status }); - } - - const { familyId, userId } = auth.session!; - - return { - session: auth.session!, - run: (cb: (tx: typeof sql) => Promise) => - withFamilyContext(familyId!, cb), - }; -} - -// Need to import NextResponse for error throwing -import { NextResponse } from "next/server"; \ No newline at end of file +export { sql, dbUnscoped }; \ No newline at end of file