Skip to content

Commit

Permalink
Merge pull request #12778 from Automattic/vkarpov15/gh-11257
Browse files Browse the repository at this point in the history
fix(types): correctly infer `this` when using `pre('updateOne')` with `{ document: true, query: false }`
  • Loading branch information
vkarpov15 committed Dec 19, 2022
2 parents 59e0da5 + e125992 commit 5b62093
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Expand Up @@ -37,7 +37,7 @@ jobs:
- run: npm install
working-directory: benchmarks/typescript/simple

- run: npx ts-benchmark -p ./ -f 17/100000 18 29 32 -b master -g -t --colors
- run: npx ts-benchmark -p ./ -f 17/110000 18 29 32 -b master -g -t --colors
working-directory: benchmarks/typescript/simple
env:
DB_URL: ${{ secrets.DB_URL }}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -100,7 +100,7 @@
"test-tsd": "node ./test/types/check-types-filename && tsd",
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
"test-coverage": "nyc --reporter=html --reporter=text npm test",
"ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17/100000 18 29 32",
"ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17/110000 18 29 32",
"ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17/100000 18 29 32 -b master"
},
"main": "./index.js",
Expand Down
32 changes: 26 additions & 6 deletions test/types/middleware.test.ts
Expand Up @@ -117,12 +117,6 @@ schema.post<Query<ITest, ITest>>('findOneAndDelete', function(res, next) {

const Test = model<ITest>('Test', schema);

function gh11257(): void {
schema.pre('save', { document: true }, function() {
expectType<HydratedDocument<ITest>>(this);
});
}

function gh11480(): void {
type IUserSchema = {
name: string;
Expand Down Expand Up @@ -155,3 +149,29 @@ function gh12583() {
console.log(doc.name);
});
}

function gh11257() {
interface User {
name: string;
email: string;
avatar?: string;
}

const schema = new Schema<User>({
name: { type: String, required: true },
email: { type: String, required: true },
avatar: String
});

schema.pre('save', { document: true }, function() {
expectType<HydratedDocument<User>>(this);
});

schema.pre('updateOne', { document: true, query: false }, function() {
this.isNew;
});

schema.pre('updateOne', { document: false, query: true }, function() {
this.find();
});
}
10 changes: 10 additions & 0 deletions types/index.d.ts
Expand Up @@ -305,6 +305,16 @@ declare module 'mongoose' {
post<T = M>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;

/** Defines a pre hook for the model. */
pre<T = HydratedDocument<DocType, TInstanceMethods>>(
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
options: SchemaPreOptions & { document: true; query: false; },
fn: PreMiddlewareFunction<T>
): this;
pre<T = Query<any, any>>(
method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[],
options: SchemaPreOptions & { document: false; query: true; },
fn: PreMiddlewareFunction<T>
): this;
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
pre<T = HydratedDocument<DocType, TInstanceMethods>>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
pre<T = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
Expand Down
1 change: 1 addition & 0 deletions types/middlewares.d.ts
Expand Up @@ -2,6 +2,7 @@ declare module 'mongoose' {

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 MiddlewareOptions = {
/**
Expand Down

0 comments on commit 5b62093

Please sign in to comment.