forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
36 lines (31 loc) · 1.15 KB
/
Copy pathclient.ts
File metadata and controls
36 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import * as schema from './schema.js'
/**
* The only place in the codebase that holds a database connection.
*
* The service connects as service_role and does its own authorization — the
* browser never gets a Supabase key, so this connection is not a fallback for
* client access, it is the whole path to data.
*
* Long-lived container, so use the direct connection or the session pooler.
* Supabase's transaction pooler (port 6543) does not support prepared
* statements, which postgres.js uses by default; if you point DATABASE_URL
* there, set `prepare: false` or queries will fail in ways that look random.
*/
let client: postgres.Sql | undefined
export function getDb(url: string) {
client ??= postgres(url, {
max: 10,
idle_timeout: 20,
connect_timeout: 10,
})
return drizzle(client, { schema, casing: 'snake_case' })
}
export type Db = ReturnType<typeof getDb>
/** Close the pool on shutdown so in-flight queries finish first. */
export async function closeDb(): Promise<void> {
await client?.end({ timeout: 5 })
client = undefined
}
export { schema }