diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 131200b6b03..589bab7e8a4 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -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 }} diff --git a/package.json b/package.json index 50942427dad..25c2ae169ff 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/types/middleware.test.ts b/test/types/middleware.test.ts index 7f1464375a4..f1234c81b80 100644 --- a/test/types/middleware.test.ts +++ b/test/types/middleware.test.ts @@ -117,12 +117,6 @@ schema.post>('findOneAndDelete', function(res, next) { const Test = model('Test', schema); -function gh11257(): void { - schema.pre('save', { document: true }, function() { - expectType>(this); - }); -} - function gh11480(): void { type IUserSchema = { name: string; @@ -155,3 +149,29 @@ function gh12583() { console.log(doc.name); }); } + +function gh11257() { + interface User { + name: string; + email: string; + avatar?: string; + } + + const schema = new Schema({ + name: { type: String, required: true }, + email: { type: String, required: true }, + avatar: String + }); + + schema.pre('save', { document: true }, function() { + expectType>(this); + }); + + schema.pre('updateOne', { document: true, query: false }, function() { + this.isNew; + }); + + schema.pre('updateOne', { document: false, query: true }, function() { + this.find(); + }); +} diff --git a/types/index.d.ts b/types/index.d.ts index b2973fd8aad..14a0e79feac 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -305,6 +305,16 @@ declare module 'mongoose' { post(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction): this; /** Defines a pre hook for the model. */ + pre>( + method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[], + options: SchemaPreOptions & { document: true; query: false; }, + fn: PreMiddlewareFunction + ): this; + pre>( + method: DocumentOrQueryMiddleware | DocumentOrQueryMiddleware[], + options: SchemaPreOptions & { document: false; query: true; }, + fn: PreMiddlewareFunction + ): this; pre>(method: 'save', fn: PreSaveMiddlewareFunction): this; pre>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction): this; pre>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | RegExp, fn: PreMiddlewareFunction): this; diff --git a/types/middlewares.d.ts b/types/middlewares.d.ts index 7aa28b6753f..270524f5519 100644 --- a/types/middlewares.d.ts +++ b/types/middlewares.d.ts @@ -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 = { /**