Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: allow accessing options from pre middleware #13708

Merged
merged 1 commit into from Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion test/types/schema.test.ts
Expand Up @@ -12,7 +12,8 @@ import {
HydratedDocument,
ResolveSchemaOptions,
ObtainDocumentType,
ObtainSchemaGeneric
ObtainSchemaGeneric,
InsertManyOptions
} from 'mongoose';
import { expectType, expectError, expectAssignable } from 'tsd';
import { ObtainDocumentPathType, ResolvePathType } from '../../types/inferschematype';
Expand Down Expand Up @@ -1150,3 +1151,22 @@ function gh13514() {
const doc = new Test({ email: 'bar' });
const str: string = doc.email;
}

function gh13633() {
const schema = new Schema({ name: String });

schema.pre('updateOne', { document: true, query: false }, function(next) {
});

schema.pre('updateOne', { document: true, query: false }, function(next, options) {
expectType<Record<string, any> | undefined>(options);
});

schema.post('save', function(res, next) {
});
schema.pre('insertMany', function(next, docs) {
});
schema.pre('insertMany', function(next, docs, options) {
expectType<(InsertManyOptions & { lean?: boolean }) | undefined>(options);
});
}
21 changes: 19 additions & 2 deletions types/index.d.ts
Expand Up @@ -406,8 +406,25 @@ declare module 'mongoose' {
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
/* method insertMany */
pre<T = TModelType>(method: 'insertMany' | RegExp, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
pre<T = TModelType>(method: 'insertMany' | RegExp, options: SchemaPreOptions, fn: (this: T, next: (err?: CallbackError) => void, docs: any | Array<any>) => void | Promise<void>): this;
pre<T = TModelType>(
method: 'insertMany' | RegExp,
fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
options?: InsertManyOptions & { lean?: boolean }
) => void | Promise<void>
): this;
pre<T = TModelType>(
method: 'insertMany' | RegExp,
options: SchemaPreOptions,
fn: (
this: T,
next: (err?: CallbackError) => void,
docs: any | Array<any>,
options?: InsertManyOptions & { lean?: boolean }
) => void | Promise<void>
): this;

/** Object of currently defined query helpers on this schema. */
query: TQueryHelpers;
Expand Down
12 changes: 10 additions & 2 deletions types/middlewares.d.ts
Expand Up @@ -31,8 +31,16 @@ declare module 'mongoose' {
type SchemaPreOptions = MiddlewareOptions;
type SchemaPostOptions = MiddlewareOptions;

type PreMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
type PreSaveMiddlewareFunction<ThisType = any> = (this: ThisType, next: CallbackWithoutResultAndOptionalError, opts: SaveOptions) => void | Promise<void>;
type PreMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts?: Record<string, any>
) => void | Promise<void>;
type PreSaveMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts: SaveOptions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason opts is not optional here, and is optional in PreMiddlewareFunction?
Does it make any difference when defining a last parameter for a function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For pre('save'), Mongoose defaults to passing empty options, so for schema.pre('save', function(next, opts) { opts; }) opts will always be defined. Even if you call save() with no args.

Not true for all middleware functions.

) => void | Promise<void>;
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
Expand Down