Files
ims/lib/db/with-user.ts
T
admin 1422d3df47 fix(bulk-import): skip __MACOSX folder when processing zip imports
Build errors fixed:
- Fixed session field access (session.sub instead of session.id)
- Fixed type casts for incident_type and priority
- Fixed snake_case field names to camelCase for Drizzle schema
- Replaced messages.create with chat.completions.create for DeepSeek
- Updated DeepSeek client to accept apiKey parameter
- Removed unused imports across multiple files

Route now accessible at /bulk-import. Redirects unauthenticated users to
/login?redirect=%2Fbulk-import.

Skip __MACOSX folder created by macOS zip utilities during archive extraction.
2026-08-19 18:45:34 +08:00

30 lines
964 B
TypeScript

import 'server-only'
import { sql } from 'drizzle-orm'
import { userDb, adminDb, type AdminDb } from './index'
export type DrizzleTransaction = Parameters<Parameters<typeof userDb.transaction>[0]>[0]
/**
* Runs fn inside a transaction with app.user_id SET LOCAL to userId.
* All RLS policies read this GUC via app_current_user_id().
* The GUC is LOCAL so it is automatically cleared when the transaction ends.
*/
export async function withUser<T>(
userId: string,
fn: (tx: DrizzleTransaction) => Promise<T>
): Promise<T> {
return userDb.transaction(async (tx) => {
await tx.execute(sql`SELECT set_config('app.user_id', ${userId}, true)`)
return fn(tx)
})
}
/**
* Runs fn against the admin (BYPASSRLS) pool.
* Use only for: admin user CRUD, service operations that legitimately bypass RLS.
* Never use for regular user data access.
*/
export async function asAdmin<T>(fn: (db: AdminDb) => Promise<T>): Promise<T> {
return fn(adminDb)
}