()
| 63 | let _dbReady: Promise<any> | undefined; |
| 64 | |
| 65 | function startInit(): Promise<any> { |
| 66 | if (_dbReady) return _dbReady; |
| 67 | |
| 68 | const url = getDatabaseUrl("file:./data/app.db"); |
| 69 | const dialect = getDialect(); |
| 70 | |
| 71 | // D1 only if dialect detected it (DATABASE_URL takes priority) |
| 72 | if (dialect === "d1") { |
| 73 | const d1 = globalThis.__cf_env?.DB; |
| 74 | if (d1) { |
| 75 | _db = drizzleD1(d1, { schema }) as unknown as LibSQLDatabase<T>; |
| 76 | _dbReady = Promise.resolve(_db); |
| 77 | return _dbReady; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (dialect === "postgres") { |
| 82 | if (isNeonUrl(url)) { |
| 83 | _dbReady = getNeonServerlessDrizzle().then(({ drizzle, Pool }) => { |
| 84 | const pool = new Pool({ connectionString: url }); |
| 85 | _db = drizzle(pool, { schema }); |
| 86 | }); |
| 87 | } else { |
| 88 | _dbReady = getPgDrizzle().then(({ drizzle, postgres }) => { |
| 89 | const client = postgres(url, { |
| 90 | onnotice: () => {}, |
| 91 | idle_timeout: 240, |
| 92 | max_lifetime: 60 * 30, |
| 93 | connect_timeout: 10, |
| 94 | ...(url.includes("supabase") ? { prepare: false } : {}), |
| 95 | }); |
| 96 | _db = drizzle(client, { schema }); |
| 97 | }); |
| 98 | } |
| 99 | } else { |
| 100 | _dbReady = getLibsqlDrizzle().then(({ drizzle }) => { |
| 101 | _db = drizzle({ |
| 102 | connection: { url, authToken: getDatabaseAuthToken() }, |
| 103 | schema, |
| 104 | }); |
| 105 | }); |
| 106 | } |
| 107 | return _dbReady; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Create a lazy proxy that records property accesses and method calls, |
no test coverage detected