an app to share curated trails
sidetrail.app
atproto
nextjs
react
rsc
1import "server-only";
2import { drizzle } from "drizzle-orm/node-postgres";
3import pg from "pg";
4import * as schema from "@sidetrail/db";
5
6const { Pool } = pg;
7
8// Connection pool singleton
9let pool: pg.Pool | null = null;
10
11function getPool(): pg.Pool {
12 if (!pool) {
13 const connectionString = process.env.DATABASE_URL;
14 if (!connectionString) {
15 throw new Error("DATABASE_URL environment variable is required");
16 }
17 pool = new Pool({ connectionString });
18 }
19 return pool;
20}
21
22// Drizzle instance singleton
23let drizzleInstance: ReturnType<typeof drizzle<typeof schema>> | null = null;
24
25export function getDb() {
26 if (!drizzleInstance) {
27 drizzleInstance = drizzle(getPool(), { schema });
28 }
29 return drizzleInstance;
30}
31
32// Re-export schema for convenience
33export * from "@sidetrail/db";