From 23361b3499e92e67f5bb9a9cec9f541fdbfbcbdc Mon Sep 17 00:00:00 2001 From: Sergei Samsonov Date: Thu, 5 Apr 2018 14:16:29 +0300 Subject: [PATCH] Update pre hooks type definitions --- types/mongoose/index.d.ts | 78 +++++++++++++++++++-- types/mongoose/mongoose-tests.ts | 112 +++++++++++++++++++++++++++++++ 2 files changed, 184 insertions(+), 6 deletions(-) diff --git a/types/mongoose/index.d.ts b/types/mongoose/index.d.ts index e9576b5098..35763d2131 100644 --- a/types/mongoose/index.d.ts +++ b/types/mongoose/index.d.ts @@ -618,8 +618,74 @@ declare module "mongoose" { /** * Defines a pre hook for the document. */ - pre(method: string, parallel: boolean, fn: HookAsyncCallback, errorCb?: HookErrorCallback): this; - pre(method: string, fn: HookSyncCallback, errorCb?: HookErrorCallback): this; + pre( + method: "init" | "validate" | "save" | "remove", + fn: HookSyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Query>( + method: + | "count" + | "find" + | "findOne" + | "findOneAndRemove" + | "findOneAndUpdate" + | "update", + fn: HookSyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Aggregate>( + method: "aggregate", + fn: HookSyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Model>( + method: "insertMany", + fn: HookSyncCallback, + errorCb?: HookErrorCallback + ): this; + pre | Query | Aggregate>( + method: string, + fn: HookSyncCallback, + errorCb?: HookErrorCallback + ): this; + + pre( + method: "init" | "validate" | "save" | "remove", + parallel: boolean, + fn: HookAsyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Query>( + method: + | "count" + | "find" + | "findOne" + | "findOneAndRemove" + | "findOneAndUpdate" + | "update", + parallel: boolean, + fn: HookAsyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Aggregate>( + method: "aggregate", + parallel: boolean, + fn: HookAsyncCallback, + errorCb?: HookErrorCallback + ): this; + pre = Model>( + method: "insertMany", + parallel: boolean, + fn: HookAsyncCallback, + errorCb?: HookErrorCallback + ): this; + pre | Query | Aggregate>( + method: string, + parallel: boolean, + fn: HookAsyncCallback, + errorCb?: HookErrorCallback + ): this; /** * Adds a method call to the queue. @@ -679,12 +745,12 @@ declare module "mongoose" { } // Hook functions: https://github.com/vkarpov15/hooks-fixed - interface HookSyncCallback { - (next: HookNextFunction): any; + interface HookSyncCallback { + (this: T, next: HookNextFunction): Promise | void; } - interface HookAsyncCallback { - (next: HookNextFunction, done: HookDoneFunction): any; + interface HookAsyncCallback { + (this: T, next: HookNextFunction, done: HookDoneFunction): Promise | void; } interface HookErrorCallback { diff --git a/types/mongoose/mongoose-tests.ts b/types/mongoose/mongoose-tests.ts index bf2f61f4a3..963de3eb0e 100644 --- a/types/mongoose/mongoose-tests.ts +++ b/types/mongoose/mongoose-tests.ts @@ -260,6 +260,118 @@ schema.plugin(function (schema, opts) { } }).plugin(cb, {opts: true}); +/* `.pre` hook tests */ + +interface PreHookTestDocumentInterface extends mongoose.Document {} +interface PreHookTestQueryInterface extends mongoose.Query {} +interface PreHookTestAggregateInterface extends mongoose.Aggregate {} +interface PreHookTestModelInterface extends mongoose.Model {} + +// it is used to ensure that all testing cases return a value of mongoose.Schema type +const preHookTestSchemaArr: mongoose.Schema[] = []; + +// testing order: +// serial with default value and returning void +// serial with a type argument and returning a promise +// parallel with default value and returning void +// parallel with a type argument and returning a promise + +// Document +preHookTestSchemaArr.push( + schema.pre("init", function (next) { + const isDefaultType: mongoose.Document = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("init", function (next) { + const isSpecificType: PreHookTestDocumentInterface = this; + return Promise.resolve(""); + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("init", true, function (next, done) { + const isDefaultType: mongoose.Document = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("init", true, function (next, done) { + const isSpecificType: PreHookTestDocumentInterface = this; + return Promise.resolve(""); + }, err => {}) +); + +// Query +preHookTestSchemaArr.push( + schema.pre("count", function (next) { + const isDefaultType: mongoose.Query = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("count", function (next) { + const isSpecificType: PreHookTestQueryInterface = this; + return Promise.resolve(""); + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("count", true, function (next, done) { + const isDefaultType: mongoose.Query = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("count", true, function (next, done) { + const isSpecificType: PreHookTestQueryInterface = this; + return Promise.resolve(""); + }, err => {}) +); + +// Aggregate +preHookTestSchemaArr.push( + schema.pre("aggregate", function(next) { + const isDefaultType: mongoose.Aggregate = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("aggregate", function(next) { + const isSpecificType: PreHookTestAggregateInterface = this; + return Promise.resolve("") + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("aggregate", true, function(next, done) { + const isDefaultType: mongoose.Aggregate = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("aggregate", true, function(next, done) { + const isSpecificType: PreHookTestAggregateInterface = this; + return Promise.resolve("") + }, err => {}) +); + +// Model +preHookTestSchemaArr.push( + schema.pre("insertMany", function(next) { + const isDefaultType: mongoose.Model = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("insertMany", function(next) { + const isSpecificType: PreHookTestModelInterface = this; + return Promise.resolve("") + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre("insertMany", true, function(next, done) { + const isDefaultType: mongoose.Model = this; + }, err => {}) +); +preHookTestSchemaArr.push( + schema.pre>("insertMany", true, function(next, done) { + const isSpecificType: PreHookTestModelInterface = this; + return Promise.resolve("") + }, err => {}) +); + schema .post('save', function (error, doc, next) { error.stack;