Skip to content

feat(model): support named parameters in Model.findOne#16336

Open
swayamsingh2004 wants to merge 1 commit into
Automattic:masterfrom
swayamsingh2004:feat/named-params-findOne
Open

feat(model): support named parameters in Model.findOne#16336
swayamsingh2004 wants to merge 1 commit into
Automattic:masterfrom
swayamsingh2004:feat/named-params-findOne

Conversation

@swayamsingh2004

Copy link
Copy Markdown

Summary

Adds support for calling Model.findOne with a single named-parameters object, in addition to the existing positional arguments. Addresses #10367, scoped to Model.findOne as a first step (per discussion in the issue).

Approach

Per the discussion in #10367, named parameters are detected using an internal namedParamsSymbol (added to lib/helpers/symbols.js, following the same pattern as existing internal symbols like modelCollectionSymbol). This avoids any ambiguity with real schema field names like filter, projection, or options, since a Symbol key can never collide with a string key.

Example

// existing positional style (unchanged)
User.findOne({ email: 'x@y.com' }, 'name', { lean: true })

// new named-parameters style
User.findOne({
  [namedParamsSymbol]: true,
  filter: { email: 'x@y.com' },
  projection: 'name',
  options: { lean: true }
})

Testing

  • Added a test in test/model.querying.test.js covering both the old positional style and the new named-parameters style.
  • Ran the full test suite locally (npm test): 4378 passing, 0 failures (one unrelated pre-existing flaky timeout in timestamps.test.js that passes in isolation).

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