fix build env

This commit is contained in:
wass08
2026-02-11 16:08:11 +09:00
parent a6df6e8c44
commit d6b8fb2cf2
+25 -4
View File
@@ -1,17 +1,38 @@
import { drizzle } from 'drizzle-orm/postgres-js'
import type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import * as schema from './schema'
if (!process.env.POSTGRES_URL) {
let _db: PostgresJsDatabase<typeof schema> | null = null
let _client: ReturnType<typeof postgres> | null = null
function getDb() {
if (!_db) {
const postgresUrl = process.env.POSTGRES_URL
if (!postgresUrl) {
throw new Error(
'Missing POSTGRES_URL environment variable. Add your Supabase database connection string.',
'Missing POSTGRES_URL environment variable. Please configure it in your deployment settings or .env.local file.',
)
}
// Create postgres connection
const client = postgres(process.env.POSTGRES_URL)
_client = postgres(postgresUrl)
// Create drizzle instance
export const db = drizzle(client, { schema })
_db = drizzle(_client, { schema })
}
return _db
}
/**
* Drizzle database instance
* Initialized lazily to avoid requiring env vars at build time
*/
export const db = new Proxy({} as PostgresJsDatabase<typeof schema>, {
get(_target, prop) {
return Reflect.get(getDb(), prop)
},
})
export type Database = typeof db