Skip to content

Commit

Permalink
fix(types): correctly infer this when using pre('updateOne') with…
Browse files Browse the repository at this point in the history
… `{ document: true, query: false }`

Fix #11257
  • Loading branch information
vkarpov15 committed Dec 8, 2022
1 parent 82943da commit 2a927bd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
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 2a927bd

Please sign in to comment.