-
-
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
added overloads for Schema.pre/post with different values for SchemaPreOptions #12680
Conversation
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.
Couple of fixes. Also, please run npm run lint -- --fix
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.
Thanks 👍
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.
Took a closer look and have a couple of comments
…js test as is checks the tests at runtime.
Completely refactored my changes. Checked the following scripts:
|
Will have a look at the reported issues |
I hopefully fixed the linter issues (I have a different formatter setting...). I also added a new test, generated from the existing one, which actually tests the type annotations (and found some problems, which I fixed). Now we can sure that the type annotations is in sync with the real thing. I assume that the tests which failed on Linux failed due to an older Node version? I removed the optional chaining construct. I'm not quite sure about the ts-benchmark. What is actually measured and what can I do there? How can I check my specific parts? Also I'm not quite sure if the benchmark simply takes longer because there are more lines to parse? |
@jpilgrim ts-benchmark measures the output of |
@vkarpov15 I have (again) merged the latest master. There were conflicts I have solved (by deleting the changed parts since they were exactly the parts I have newly defined. Are there any changes required now from my side? I have no idea what to do about the failing ts benchmark test except increasing the expected time (or whatever is measured). Of course my changes will increase the time, as significantly more overloads are defined now -- which is necessary if you want to have correct definitions. Github says "1 change requested" -- I think that has been resolved. |
@vkarpov15 Any updates? |
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.
Sorry for taking so long to review this, we've been busy working on Mongoose 7. Have a couple of comments, but otherwise LGTM
types/middlewares.d.ts
Outdated
type MongooseDocumentMiddleware = 'validate' | 'save' | 'remove' | 'updateOne' | 'deleteOne' | 'init'; | ||
type MongooseQueryMiddleware = 'count' | 'estimatedDocumentCount' | 'countDocuments' | 'deleteMany' | 'deleteOne' | 'distinct' | 'find' | 'findOne' | 'findOneAndDelete' | 'findOneAndRemove' | 'findOneAndReplace' | 'findOneAndUpdate' | 'remove' | 'replaceOne' | 'update' | 'updateOne' | 'updateMany'; | ||
type DocumentOrQueryMiddleware = 'updateOne' | 'deleteOne' | 'remove'; | ||
type MongooseQueryAndDocumentMiddleware = 'remove' | 'updateOne' | 'deleteOne'; |
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.
validate
is another one that can be both Query and Document middleware
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.
According to https://mongoosejs.com/docs/middleware.html, validate is only a document middleware.
Anyway, how could it be triggered via a query?
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 added a Query.validate()
method. Mongoose calls this method when runValidators
is set on a query, for example:
'use strict';
const mongoose = require('mongoose');
const schema = new mongoose.Schema({ name: String });
schema.pre('validate', () => {
console.log('Regular pre validate');
});
schema.pre('validate', { query: true, document: false }, () => {
console.log('Pre query validate');
});
const Test = mongoose.model('Test', schema);
run().catch(err => console.log(err));
async function run() {
await mongoose.connect('mongodb://localhost:27017/test');
const doc = new Test();
await doc.validate();
console.log('----');
await Test.updateOne({}, { name: 'bar' }).setOptions({ runValidators: true });
}
types/middlewares.d.ts
Outdated
type MongooseQueryAndDocumentMiddleware = 'remove' | 'updateOne' | 'deleteOne'; | ||
|
||
type MongooseDistinctDocumentMiddleware = 'validate' | 'save' | 'init'; | ||
type MongooseDefaultDocumentMiddleware = MongooseDistinctDocumentMiddleware | 'remove'; |
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.
| 'validate'
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 assume this is the same issue again?
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.
Yep same issue
@vkarpov15 I have merged the (your) latest master. There seems to be some methods have been removed:
After removing these calls in my test, I got test failures since all pre/post-middlewares for Does that mean that these middleware-strings ( |
@vkarpov15 I assume update and remove have been removed. Thus I updated the tests and type definitions accordingly. As you might note, I had to fix another test. From my point of view the PR should be ready to be merged. Tests are all passing. However, I cannot run |
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.
LGTM, going to merge this into master to release with 7.1. I will make a couple of related changes in master: 1) bumping up the ts-benchmark limit slightly so ts-benchmark passes, 2) undo adding 'validate' to MongooseQueryAndDocumentMiddleware
, because of the if (!(this instanceof Query))
change in document.test.ts
.
Summary
You can call the
updateOne
method with either the model class or a model instance (i.e. document) as receiver:Depending on how you call it, different pre-hooks are called and
this
is set differently:In mongoose version 6.7.2 a breaking change has been introduced (compared with 6.7.1. !) with commit 74af5f5b34cc7fcf631e1e323f18df36c74f6201 (by Luca Pizzini) in types/index.d.ts, by moving the overloads for this=Query ahead of the overloads for this=Document (HydratedDocument).
Examples
The following code worked in 6.7.1
but it produces an error in 6.7.2: Property 'isModified' does not exist on type 'Query<any, any, {}, any>'.ts(2339)
In order to fix that problem, I would suggest to add more overloads, which do not use the general SchemaPreOptions type alias, but concrete values for document and query.