fix build env
This commit is contained in:
@@ -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
|
||||
_client = postgres(postgresUrl)
|
||||
|
||||
// Create drizzle instance
|
||||
_db = drizzle(_client, { schema })
|
||||
}
|
||||
return _db
|
||||
}
|
||||
|
||||
// Create postgres connection
|
||||
const client = postgres(process.env.POSTGRES_URL)
|
||||
|
||||
// Create drizzle instance
|
||||
export const db = drizzle(client, { schema })
|
||||
/**
|
||||
* 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
|
||||
|
||||
Reference in New Issue
Block a user