-
-
Notifications
You must be signed in to change notification settings - Fork 252
Open
Description
Lines 5 to 21 in b5379db
| // Create the connection string from environment variable | |
| const connectionString = process.env.DATABASE_URL | |
| if (!connectionString) { | |
| throw new Error('DATABASE_URL environment variable is not set') | |
| } | |
| // Create postgres client | |
| // For serverless environments, use connection pooling | |
| const client = postgres(connectionString, { | |
| max: 1, // For serverless, limit connections | |
| idle_timeout: 20, | |
| connect_timeout: 10, | |
| }) | |
| // Create drizzle instance with schema | |
| export const db = drizzle(client, { schema }) |
If there is no DB connection set, the build will fail. On docs only contributions, it is annoying.
As a workaround, I'm doing this:
function lazyObject<T extends object>(factory: () => T): T {
let value: T | undefined = undefined
return new Proxy({} as T, {
get(_, prop) {
if (!value) value = factory()
return value[prop as keyof T]
},
}) as T
}
export const db = lazyObject(() => {
// Create the connection string from environment variable
const connectionString = process.env.DATABASE_URL
if (!connectionString) {
throw new Error('DATABASE_URL environment variable is not set')
}
// Create postgres client
// For serverless environments, use connection pooling
const client = postgres(connectionString, {
max: 1, // For serverless, limit connections
idle_timeout: 20,
connect_timeout: 10,
})
// Create drizzle instance with schema
return drizzle(client, { schema })
})Is that ok for a workaround for a PR, or is there a better pattern or approach? Passing it though the backend context? Or maybe a React's cache like API in start? 🤔
morinokami
Metadata
Metadata
Assignees
Labels
No labels