Skip to content

fix(adapters): defer config validation to call-time (#64)#65

Merged
techiejd merged 3 commits into
mainfrom
fix/defer-adapter-validation-issue-64
Jun 3, 2026
Merged

fix(adapters): defer config validation to call-time (#64)#65
techiejd merged 3 commits into
mainfrom
fix/defer-adapter-validation-issue-64

Conversation

@techiejd
Copy link
Copy Markdown
Owner

@techiejd techiejd commented Jun 2, 2026

Closes #64.

Problem

The mongodb and cf adapter factories threw on missing config at construction time. Because the adapter is instantiated as a top-level statement in payload.config.ts — before Payload (and its logger) exist, and before the plugin's disabled flag is ever consulted — this breaks pnpm generate:types and pnpm generate:importmap in any environment without the runtime variables present (e.g. CI that builds payload-types to publish as a separate package).

Notably, disabled can't work around this: the throw happens in the adapter factory, upstream of the plugin.

Fix

Move validation to call-time (every call, not memoized):

  • mongodb (adapters/mongodb/src/index.ts): uri / dbName / pools / dimensions checks now run inside each data method via a getCtx() helper. getConfigExtension (which runs at config-build time) builds the resolved pools defensively and never throws.
  • cf (adapters/cf/src/index.ts): removed the factory-time binding is required throw. The existing runtime getVectorizeBinding() check already enforces the binding at call-time for the methods that need it (storeChunk / search / deleteChunks).
  • pg: unaffected — its factory takes no env-derived args and never threw at build time.

Tests (TDD)

  • New adapters/mongodb/dev/specs/validation.spec.ts (10 tests): construction with missing uri/dbName/pools/dimensions does not throw, getConfigExtension does not throw with fully-missing config, and each data method still throws the right error at call-time.
  • Updated the cf validation test from "throws at construction" to "does not throw at construction" + a new "throws at call-time" test (strengthened, not weakened).

All green: mongodb 106/106 (incl. DB-backed compliance), cf adapter 16/16; both adapters typecheck clean.

techiejd added 3 commits June 2, 2026 19:02
The mongodb and cf adapter factories threw on missing config at
construction time, which runs during config build. That breaks
`generate:types` / `generate:importmap` in environments without
runtime variables (e.g. CI publishing payload-types as a separate
package), since the adapter is instantiated before Payload exists.

Move validation to call-time:
- mongodb: uri/dbName/pools/dimensions checks run inside each data
  method via getCtx(); getConfigExtension builds resolved pools
  defensively and never throws at build time.
- cf: drop the factory-time binding throw; the existing runtime
  getVectorizeBinding() check already enforces it at call-time.
- pg: unaffected (no env-derived args at construction).

Closes #64
@techiejd techiejd merged commit d8fea7a into main Jun 3, 2026
8 checks passed
@github-actions github-actions Bot mentioned this pull request Jun 3, 2026
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.

Avoid throwing Errors on missing variables

1 participant