30 lines
827 B
TypeScript
30 lines
827 B
TypeScript
import 'server-only'
|
|
import { Pool } from 'pg'
|
|
import { drizzle } from 'drizzle-orm/node-postgres'
|
|
import * as schema from './schema'
|
|
|
|
if (!process.env.DATABASE_URL) {
|
|
throw new Error('DATABASE_URL not configured')
|
|
}
|
|
if (!process.env.DATABASE_URL_ADMIN) {
|
|
throw new Error('DATABASE_URL_ADMIN not configured')
|
|
}
|
|
|
|
// app_user pool: RLS enforced. Used for all normal user operations.
|
|
const userPool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
max: 10,
|
|
})
|
|
|
|
// app_admin pool: BYPASSRLS. Used for admin user CRUD and service operations.
|
|
const adminPool = new Pool({
|
|
connectionString: process.env.DATABASE_URL_ADMIN,
|
|
max: 5,
|
|
})
|
|
|
|
export const userDb = drizzle(userPool, { schema })
|
|
export const adminDb = drizzle(adminPool, { schema })
|
|
|
|
export type UserDb = typeof userDb
|
|
export type AdminDb = typeof adminDb
|