feat: add eqOrIn and neOrNin query value utils - #49
Merged
Merged
Conversation
Both collapse a list of values into the narrower query operator when only one
value remains: `eqOrIn` returns the bare value instead of `{ $in: [value] }`,
`neOrNin` returns `{ $ne: value }` instead of `{ $nin: [value] }`.
Values are deduplicated first via a new shared `dedupeValues` helper, which
compares primitives with SameValueZero and non-primitives deep-equal so value
wrappers like `Date` or a mongo `ObjectId` collapse across references.
Primitives never hit the deep comparison, keeping large scalar id lists a
single `Set` pass.
`onDelete` now uses `eqOrIn` instead of hand-rolling the same branch.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Deploying feathers-utils with
|
| Latest commit: |
6a43a4b
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://21026863.feathers-utils.pages.dev |
| Branch Preview URL: | https://feat-eq-or-in-ne-or-nin.feathers-utils.pages.dev |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds two
utilsfor a pattern that recurs whenever you build a query from alist of values: collapse to the narrower operator when only one value remains.
onDeletealready hand-rolled exactly this branch and now useseqOrIninstead.
An empty list yields
{ $in: [] }/{ $nin: [] }— semantically correct(matches nothing / excludes nothing), but documented, since some adapters
dislike an empty
IN ().Other Information
Deduplication. Values are deduplicated before the length check, so
[1, 1]collapses to the single-value form too. This is behaviour-preserving:a deduplicated
$inmatches the same set of records.The dedupe lives in a new shared
src/common/dedupe-values.ts(in-sourcetests) used by both utils. It compares primitives with
SameValueZeroandnon-primitives deep-equal via
dequal— the dependencydedupeBranchesalready uses for the same job on
$and/$orbranches. Deep-equal matters forthe most common real case:
result.map((x) => x[keyHere])over a mongo serviceyields distinct
ObjectIdreferences with equal values, which a plainSetwould not collapse. Same for
Date.Primitives never reach the deep comparison, so a large list of scalar ids stays
a single
Setpass and only the non-primitive elements are compared pairwise —relevant because
$inarrays can hold thousands of ids, unlike the handful ofbranches
dedupeBranchessees.One documented trade-off:
dequalignores key order, so two plain objects withthe same entries in a different order count as one. MongoDB matches document
values in
$infield-order-sensitively and would treat them as distinct. Plaindocuments as
$invalues are vanishingly rare in Feathers, anddedupeBranchesalready makes the same trade-off, so this seemed the rightdefault — happy to restrict deep-equal to non-plain objects if preferred.
Docs pages are auto-discovered from the sibling
.mdfiles;test/index.test.tsexport lists updated.
pnpm testpasses: 0 lint errors, no type errors, bothutils and the helper at full coverage.
🤖 Generated with Claude Code