tanstack.com
tanstack.com copied to clipboard
Eager DB client initialization makes the DB required to preview the docs
https://github.com/TanStack/tanstack.com/blob/b5379db43a8112a5e929260e992099b58f61734a/src/db/client.ts#L5-L21
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? 🤔