Skip to content

Commit cf1b666

Browse files
authoredMar 29, 2021
Merge pull request #10078 from pezzu/master
Fixes #10072
2 parents 2aef528 + 452c77c commit cf1b666

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed
 

‎index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,8 @@ declare module 'mongoose' {
11661166
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;
11671167
pre<T extends Aggregate<any> = Aggregate<any>>(method: 'aggregate' | RegExp, fn: (this: T, next: (err: CallbackError) => void) => void): this;
11681168
pre<T extends Aggregate<any> = Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err: CallbackError) => void) => void): this;
1169-
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err: CallbackError) => void) => void): this;
1170-
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err: CallbackError) => void) => void): this;
1169+
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void): this;
1170+
pre<T extends Model<DocType> = M>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void): this;
11711171

11721172
/** Object of currently defined query helpers on this schema. */
11731173
query: { [name: string]: <T extends QueryWithHelpers<any, DocType, ExtractQueryHelpers<M>> = QueryWithHelpers<any, DocType, ExtractQueryHelpers<M>>>(this: T, ...args: any[]) => any };

‎test/typescript/middleware.ts

+23
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,27 @@ schema.post<ITest>('save', function(err: Error, res: ITest, next: Function) {
3939
console.log(this.name, err.stack);
4040
});
4141

42+
schema.pre<Model<ITest>>('insertMany', function() {
43+
return Promise.resolve(this.name);
44+
});
45+
46+
schema.pre<Model<ITest>>('insertMany', { document: false, query: false }, function() {
47+
console.log(this.name);
48+
});
49+
50+
schema.pre<Model<ITest>>('insertMany', function(next) {
51+
console.log(this.name);
52+
next();
53+
});
54+
55+
schema.pre<Model<ITest>>('insertMany', function(next, doc: ITest) {
56+
console.log(this.name, doc);
57+
next();
58+
});
59+
60+
schema.pre<Model<ITest>>('insertMany', function(next, docs: Array<ITest>) {
61+
console.log(this.name, docs);
62+
next();
63+
});
64+
4265
const Test = model<ITest>('Test', schema);

0 commit comments

Comments
 (0)
Please sign in to comment.