Conversation
```
database.query(Entity).withOptions({estimateCount: true}).count();
```
This adds a simple shallow algorithm to detect a union member smarter by scoring its matched properties before taking the serializer. this adds also support for the same in BSON.
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for scored unions to improve serialization behavior when union members have overlapping properties. Previously, the serializer always picked the first matching union member, which could lead to data loss. The new implementation assigns scores to union members and selects the one with the highest score, ensuring more accurate serialization.
- Implements scored union selection for better union member matching
- Updates type guard functions to return scores instead of simple boolean values
- Adds comprehensive test coverage for various union scenarios with optional and additional properties
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/type/src/serializer.ts | Core implementation of scored union logic with type guard scoring system |
| packages/type/src/typeguard.ts | Updates type guard to handle score-based returns for root validation |
| packages/bson/src/bson-deserializer-templates.ts | BSON-specific implementation of scored union deserialization |
| packages/bson/src/bson-serializer.ts | Updates BSON serializer to use new union handling |
| packages/type/tests/serializer.spec.ts | Test cases for union serialization with overlapping members |
| packages/type/tests/type-spec.spec.ts | Test cases for union JSON serialization scenarios |
| packages/bson/tests/bson-parser.spec.ts | BSON-specific union serialization test cases |
| packages/bson/tests/type-spec.spec.ts | Reordered test assertions for null/undefined handling |
| packages/mongo/src/query.resolver.ts | Added empty query handling for deterministic count results |
| packages/mongo/src/client/options.ts | Added estimateCount option to CommandOptions interface |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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.
This adds support for simple scored unions. Previously the serializer picked the first matching union member. Now it takes a score per member and uses the one with highest score.