Skip to content

refactor: memoize the Couchbase connection as one shared promise - #237

Open
dex-the-ai wants to merge 1 commit into
mainfrom
architecture-audit/nextjs-capella-quickstart-2026w47
Open

dex-the-ai wants to merge 1 commit into
mainfrom
architecture-audit/nextjs-capella-quickstart-2026w47

Conversation

@dex-the-ai

Copy link
Copy Markdown
Contributor

Summary

getDatabase() in lib/couchbase-connection.ts is how every API route reaches Couchbase. Its caching didn't work the way its comment says:

  • The hot-reload cache didn't work. The code read global.couchbase but never wrote to it. So in next dev, every module re-evaluation opened a new cluster connection, which is exactly what the comment said it prevents.
  • Concurrent first requests each connected separately. The connection was cached only after couchbase.connect() resolved. The import-time insertFTSIndexes() call and any requests that arrived during the connect each opened their own Cluster.

This PR caches the in-flight connection promise on globalThis and clears it if connecting fails, so a later request can retry. Callers don't change: getDatabase() keeps the same signature and return shape. Two shallow layers are gone, createCouchbaseCluster() and the unused module-level cachedDbConnection, which leaves a single, deeper connection module.

Tests

lib/couchbase-connection.test.ts is new. It mocks the couchbase SDK, so no cluster is needed, and covers:

  • three concurrent callers → one couchbase.connect call, same connection object
  • module re-evaluation (simulated hot reload) → the connection is reused
  • the first connect fails → the error reaches the caller, and the next call retries and succeeds

All 3 tests fail on main and pass on this branch.

Validation

  • npx vitest run against local Couchbase Server 8.0.3 with travel-sample: 47/47 pass (44 existing + 3 new). On main the same setup gives 44/44.
  • npx tsc --noEmit: pass
  • npx eslint lib/: pass
  • npx next build: pass
  • prettier --check: the new test file passes. couchbase-connection.ts already failed formatting on main, and I left it unformatted to keep this diff small.

🤖 Generated with Claude Code

getDatabase() is the single seam every API route uses to reach Couchbase,
but its caching did not do what it claimed:

- the "hot reload" cache read global.couchbase but never wrote it back,
  so every module re-evaluation in dev opened a fresh cluster connection;
- the connection was cached only after `couchbase.connect` resolved, so
  concurrent first requests (and the import-time insertFTSIndexes call)
  each opened their own cluster.

Memoize the in-flight connection promise on globalThis instead, and clear
it on failure so a later request can retry. Callers are unchanged.

Add lib/couchbase-connection.test.ts, which mocks the couchbase SDK and
covers concurrent callers, module re-evaluation, and retry after failure.
All three tests fail on main and pass here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nextjs-capella-quickstart Ready Ready Preview Nov 22, 2026 3:59am UTC

This branch was successfully deployed

1 active deployment
Preview — b2dddaea Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant