TanStack / TanStack/tanstack.com

Eager DB client initialization makes the DB required to preview the docs

Open
#590 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
1.1k
Forks
401
Avg merge
18h 14m
Merged PRs (30d)
56

Description

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? 🤔

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/db/client.ts lines 5-21 and reproduce a docs build or preview without DATABASE_URL. Trace which docs paths initialize the client and compare them with paths that actually need database access. Done means documentation-only work can build and preview without a database while database-backed behavior still works when configured.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
build-system, database, documentation
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.