-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
types: Fixes and improves Query.projection
method signature
#11210
types: Fixes and improves Query.projection
method signature
#11210
Conversation
@vkarpov15 please, take a quick look once you have a chance 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The general idea looks good. I double checked the performance against #10349 and it doesn't look like this causes any major performance issues. Only thing to fix up is removing the restriction that RHS of projection properties should be numbers, that isn't entirely correct.
index.d.ts
Outdated
@@ -2117,6 +2117,8 @@ declare module 'mongoose' { | |||
|
|||
type UnpackedIntersection<T, U> = T extends (infer V)[] ? (V & U)[] : T & U; | |||
|
|||
type ProjectionFields<DocType> = {[Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: number} & Record<string, number>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like restricting projections to numbers. There are plenty of codebases out there that use true/false instead of 0/1. And MongoDB does also allow using strings in select()
, although that has different behavior than using true/false or 0/1.
Ideally I'd say this should be [Key in keyof Omit<LeanDocument<DocType>, '__v'>]?: any
, but if you want to be more strict than that, we need to support:
- Boolean
- String
- Projection operators
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review. I'll follow your version with any
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging this into 6.2 branch, thanks 👍
Summary
Fixes type mismatch introduced here #11176
Auto completion for
Query.projection
method should be more efficient and also allows dot notation for nested fields, etc.