-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: Update Expression Typings #11956
Conversation
436775a
to
189e61c
Compare
189e61c
to
b4ad355
Compare
Let me see |
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.
We have to think about this more
} | ||
|
||
type Path = string; | ||
type Path = string | ObjectIdLike; |
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.
No. Path is just a Path. Not ObjectIdLike.
type Path = string | ObjectIdLike; | |
type Path = string; |
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 created a separate type, so that one day if we have good way to generate valid paths by mongoose Schema we would already have it properly encapsulated it.
@@ -2594,7 +2608,8 @@ declare module 'mongoose' { | |||
export type ConditionalExpressionOperator = | |||
Expression.Cond | | |||
Expression.IfNull | | |||
Expression.Switch; | |||
Expression.Switch | | |||
Expression.Exists; |
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.
No this is not a Conditional Expression Operator
see https://www.mongodb.com/docs/manual/reference/operator/aggregation/#conditional-expression-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.
Expression.Exists; |
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 am not aware of all changes but it seems that $exists
is not accepted anymore by the typings.
Wherever you put it, this should work:
const result = await Model.aggregate([{
$match: { optional: { $exists: true } }
}]);
mongoose@6.4.0:
Type '{ $exists: true; }' is not assignable to type 'Expression'.
@@ -2594,7 +2608,8 @@ declare module 'mongoose' { | |||
export type ConditionalExpressionOperator = | |||
Expression.Cond | | |||
Expression.IfNull | | |||
Expression.Switch; | |||
Expression.Switch | |
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.
Expression.Switch | | |
Expression.Switch; |
@@ -2428,7 +2441,8 @@ declare module 'mongoose' { | |||
DateExpression | | |||
BinaryExpression | | |||
FunctionExpression | | |||
ObjectIdExpression; | |||
ObjectIdExpression | | |||
ConditionalExpressionOperator; |
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 think this is kind of wrong.
@Uzlopak just wanted to get the ball rolling. If you want to open your own PR (please!), feel free to. You can copy the tests from mine. |
I think there is a mixup. What you want to add is a Query Operator, but the expressions are for aggregations. I have to think how we can solve this without mixing it up. |
There's also a problem with booleans, like
|
Well it is wrong because in an aggregation pipeline $ne is an array with two values, which are compared. |
Both are valid in different contexts, see: https://stackoverflow.com/a/15330673/114732 |
From mongodb docs at https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/#mongodb-pipeline-pipe.-match:
|
I think export interface Match {
/** [`$match` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/match/) */
$match: FilterQuery<any>;
} It would be great if instead of |
I think we have to rename expressions.d.ts to aggregationpipelineexpressions.d.ts and create also corresponding types for the other operations and then decide how and which expressions are allowed when and where |
I think the main issue is with There's a special case for that one. I had to step away for a bit so I can't verify, but simply fixing that might fix most of the issues in one shot. |
Actually |
Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
@@ -1034,7 +1036,7 @@ declare module 'mongoose' { | |||
* | |||
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/ne/#mongodb-expression-exp.-ne | |||
*/ | |||
$ne: Expression | [Expression, Expression]; | |||
$ne: Expression | [Expression, Expression] | null; |
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.
Would just adding | null
to Expression
work better here? So you can use null
in other places as well.
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'll merge this for now to unblock @andreialecu .
However, there are still some issues with the expression typings. For one thing @Uzlopak , I think we'll have to add AnyObject
, because the following code will still fail:
{
$match: {
_id: new ObjectId('stringObjecId'),
a: { $exists: true },
b: null,
c: 'test',
d: { foo: 42 } // <-- valid, but current typings will fail
}
}
I'll be honest: |
Indeed, this wasn't totally ready to merge - I forgot to set it to draft. I thought @Uzlopak would work on his PR. I also wanted to suggest reverting the expressions PR, but it might not be necessary with this change: export interface Match {
/** [`$match` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/match/) */
$match: FilterQuery<any>;
}
I opened #11969 with a better fix |
Does $nin operator still exists? For the moment I have to workaround with
|
I ask the same question for the |
@lucamodica yes |
Nope, or at least both |
#11969 should fix this. |
Summary
Fixes #11952