Skip to content

Commit

Permalink
Fixes #10072
Browse files Browse the repository at this point in the history
  • Loading branch information
pezzu committed Mar 29, 2021
1 parent 9e98cd8 commit 452c77c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Expand Up @@ -1166,8 +1166,8 @@ declare module 'mongoose' {
pre<T extends Query<any, any> = Query<any, any>>(method: MongooseQueryMiddleware | MongooseQueryMiddleware[] | string | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err: CallbackError) => void) => void): this;
pre<T extends Aggregate<any> = Aggregate<any>>(method: 'aggregate' | RegExp, fn: (this: T, next: (err: CallbackError) => void) => void): this;
pre<T extends Aggregate<any> = Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err: CallbackError) => void) => void): this;
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err: CallbackError) => void) => void): this;
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err: CallbackError) => void) => void): this;
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void): this;
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void): this;

/** Object of currently defined query helpers on this schema. */
query: { [name: string]: <T extends QueryWithHelpers<any, DocType, ExtractQueryHelpers<M>> = QueryWithHelpers<any, DocType, ExtractQueryHelpers<M>>>(this: T, ...args: any[]) => any };
Expand Down
23 changes: 23 additions & 0 deletions test/typescript/middleware.ts
Expand Up @@ -39,4 +39,27 @@ schema.post<ITest>('save', function(err: Error, res: ITest, next: Function) {
console.log(this.name, err.stack);
});

schema.pre<Model<ITest>>('insertMany', function() {
return Promise.resolve(this.name);
});

schema.pre<Model<ITest>>('insertMany', { document: false, query: false }, function() {
console.log(this.name);
});

schema.pre<Model<ITest>>('insertMany', function(next) {
console.log(this.name);
next();
});

schema.pre<Model<ITest>>('insertMany', function(next, doc: ITest) {
console.log(this.name, doc);
next();
});

schema.pre<Model<ITest>>('insertMany', function(next, docs: Array<ITest>) {
console.log(this.name, docs);
next();
});

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

0 comments on commit 452c77c

Please sign in to comment.