feat(db): Drizzle DAL with withUser/asAdmin GUC wrapper (Phase 2)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-23 15:50:57 +08:00
co-authored by Claude Sonnet 4.6
parent e5fd2436fa
commit a95273b182
9 changed files with 1640 additions and 2 deletions
+28
View File
@@ -0,0 +1,28 @@
import { sql } from 'drizzle-orm'
import type { DrizzleTransaction } from './with-user'
export type { DrizzleTransaction }
/**
* Writes to audit_log via the write_audit_log() SECURITY DEFINER function.
* Must be called inside a withUser() transaction — the function reads app_current_user_id().
* Signature mirrors the DB function exactly.
*/
export async function writeAuditLog(
tx: DrizzleTransaction,
tableName: string,
recordId: string,
action: string,
newValue?: Record<string, unknown> | null,
oldValue?: Record<string, unknown> | null
): Promise<void> {
await tx.execute(
sql`SELECT write_audit_log(
${tableName},
${recordId}::uuid,
${action},
${newValue ? JSON.stringify(newValue) : null}::jsonb,
${oldValue ? JSON.stringify(oldValue) : null}::jsonb
)`
)
}