● Zion Boggan
repos/Stepping App/packages/db/src/index.ts
zionboggan.com ↗
24 lines · typescript
History for this file →
1
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
2
import postgres from "postgres";
3
import * as schema from "./schema";
4
 
5
export * from "./schema";
6
export { schema };
7
export type Database = PostgresJsDatabase<typeof schema>;
8
 
9
let _db: Database | undefined;
10
let _client: ReturnType<typeof postgres> | undefined;
11
 
12
export function getDb(): Database {
13
  if (!_db) {
14
    const url = process.env.DATABASE_URL;
15
    if (!url) {
16
      throw new Error(
17
        "DATABASE_URL is not set. Copy .env.example to .env and add your Supabase connection string.",
18
      );
19
    }
20
    _client = postgres(url, { prepare: false });
21
    _db = drizzle(_client, { schema });
22
  }
23
  return _db;
24
}