Fix scoped.ts TypeScript error - simplify to avoid transaction type issue
The Drizzle transaction generic type was causing a type mismatch error. Since withFamilyContext and getScopedDb were not used anywhere, simplify the file to just re-export sql and dbUnscoped. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
b3e8b0a75f
commit
f4a1d4544b
1 changed files with 1 additions and 36 deletions
|
|
@ -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<T>(
|
||||
familyId: string,
|
||||
callback: (tx: typeof sql) => Promise<T>
|
||||
): Promise<T> {
|
||||
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: <T>(cb: (tx: typeof sql) => Promise<T>) =>
|
||||
withFamilyContext(familyId!, cb),
|
||||
};
|
||||
}
|
||||
|
||||
// Need to import NextResponse for error throwing
|
||||
import { NextResponse } from "next/server";
|
||||
export { sql, dbUnscoped };
|
||||
Loading…
Add table
Reference in a new issue