diff options
| author | Maze Winther <mazewinther@gmail.com> | 2025-06-27 16:56:59 +0200 |
|---|---|---|
| committer | Maze Winther <mazewinther@gmail.com> | 2025-06-27 16:56:59 +0200 |
| commit | 8fe09c83b1abd67161c880bf630d0e6d46c70cf4 (patch) | |
| tree | ae86de2e02b1f4695fe9a8880facf9f2d4ce75ce /packages/db | |
| parent | cdfb5ea7b07bf150eefcfcfa7c9890a5e1f41218 (diff) | |
| download | opencut-8fe09c83b1abd67161c880bf630d0e6d46c70cf4.tar.gz | |
fix: DATABASE_URL error on setup
Diffstat (limited to 'packages/db')
| -rw-r--r-- | packages/db/src/index.ts | 41 |
1 files changed, 33 insertions, 8 deletions
diff --git a/packages/db/src/index.ts b/packages/db/src/index.ts index 91fc841d..9ebf611e 100644 --- a/packages/db/src/index.ts +++ b/packages/db/src/index.ts @@ -2,18 +2,43 @@ import { drizzle } from "drizzle-orm/postgres-js"; import postgres from "postgres"; import * as schema from "./schema"; -if (!process.env.DATABASE_URL) { - throw new Error("DATABASE_URL is not set"); -} +// Create a lazy database instance that only initializes when accessed +let _db: ReturnType<typeof drizzle> | null = null; + +function getDb() { + if (!process.env.DATABASE_URL) { + throw new Error("DATABASE_URL is not set"); + } -// Create the postgres client -const client = postgres(process.env.DATABASE_URL); + if (!_db) { + const client = postgres(process.env.DATABASE_URL); + _db = drizzle(client, { schema }); + } + + return _db; +} -// Create the drizzle instance -export const db = drizzle(client, { schema }); +// Export a proxy that forwards all calls to the actual db instance +export const db = new Proxy({} as ReturnType<typeof drizzle>, { + get(target, prop) { + return getDb()[prop as keyof typeof _db]; + }, +}); // Re-export schema for convenience export * from "./schema"; // Re-export drizzle-orm functions to ensure version consistency -export { eq, and, or, not, isNull, isNotNull, inArray, notInArray, exists, notExists, sql } from "drizzle-orm";
\ No newline at end of file +export { + eq, + and, + or, + not, + isNull, + isNotNull, + inArray, + notInArray, + exists, + notExists, + sql, +} from "drizzle-orm"; |
