forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
34 lines (30 loc) · 1.13 KB
/
Copy pathindex.ts
File metadata and controls
34 lines (30 loc) · 1.13 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
// src/lib/db/index.ts
import { drizzle } from "drizzle-orm/postgres-js"
import postgres from "postgres"
import * as schema from "./schema"
function buildConnectionString(): string {
if (process.env.DATABASE_URL) return process.env.DATABASE_URL
const password = process.env.POSTGRES_PASSWORD
if (!password) {
throw new Error("Set either DATABASE_URL or POSTGRES_PASSWORD")
}
const user = process.env.POSTGRES_USER ?? "postgres"
const host = process.env.POSTGRES_HOST ?? "localhost"
const port = process.env.POSTGRES_PORT ?? "5432"
const name = process.env.POSTGRES_DB ?? "tracker_tracker"
return `postgresql://${user}:${encodeURIComponent(password)}@${host}:${port}/${name}`
}
const connectionString = buildConnectionString()
// Reuse the Postgres connection across HMR reloads in development
const g = globalThis as typeof globalThis & { __dbClient?: ReturnType<typeof postgres> }
const client =
g.__dbClient ??
postgres(connectionString, {
max: 5,
idle_timeout: 30,
connect_timeout: 10,
})
if (process.env.NODE_ENV !== "production") {
g.__dbClient = client
}
export const db = drizzle(client, { schema })