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: add overwriteMiddlewareResult and skipMiddlewareFunction to types #14328

Merged
merged 7 commits into from Mar 4, 2024
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -20,7 +20,7 @@
"license": "MIT",
"dependencies": {
"bson": "^6.2.0",
"kareem": "2.5.1",
"kareem": "2.6.0",
"mongodb": "6.3.0",
"mpath": "0.9.0",
"mquery": "5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/tsc-diagnostics-check.js
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs');

const stdin = fs.readFileSync(0).toString('utf8');
const maxInstantiations = isNaN(process.argv[2]) ? 120000 : parseInt(process.argv[2], 10);
const maxInstantiations = isNaN(process.argv[2]) ? 125000 : parseInt(process.argv[2], 10);

console.log(stdin);

Expand Down
9 changes: 9 additions & 0 deletions test/types/base.test.ts
Expand Up @@ -8,6 +8,15 @@ Object.values(mongoose.models).forEach(model => {

mongoose.pluralize(null);

mongoose.overwriteMiddlewareResult('foo');
const schema = new mongoose.Schema({ name: String });
schema.pre('save', function() {
return mongoose.skipMiddlewareFunction('foobar');
});
schema.post('save', function() {
return mongoose.overwriteMiddlewareResult('foobar');
});

function gh10746() {
type A = string extends Function ? never : string;

Expand Down
2 changes: 1 addition & 1 deletion test/types/middleware.test.ts
Expand Up @@ -72,7 +72,7 @@ schema.pre<Model<ITest>>('insertMany', function() {
return Promise.resolve();
});

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

Expand Down
36 changes: 5 additions & 31 deletions types/index.d.ts
Expand Up @@ -27,6 +27,7 @@
declare class NativeDate extends global.Date { }

declare module 'mongoose' {
import Kareem = require('kareem');
import events = require('events');
import mongodb = require('mongodb');
import mongoose = require('mongoose');
Expand Down Expand Up @@ -390,7 +391,6 @@ declare module 'mongoose' {
): this;
// this = Document
pre<T = THydratedDocumentType>(method: 'save', fn: PreSaveMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], fn: PreMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(method: MongooseDistinctDocumentMiddleware|MongooseDistinctDocumentMiddleware[], options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
pre<T = THydratedDocumentType>(
Expand All @@ -408,7 +408,6 @@ declare module 'mongoose' {
pre<T = THydratedDocumentType|Query<any, any>>(method: MongooseQueryOrDocumentMiddleware | MongooseQueryOrDocumentMiddleware[] | RegExp, fn: PreMiddlewareFunction<T>): this;
// method aggregate
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,
Expand All @@ -419,16 +418,6 @@ declare module 'mongoose' {
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;
/* method bulkWrite */
pre<T = TModelType>(
method: 'bulkWrite' | RegExp,
Expand All @@ -439,16 +428,6 @@ declare module 'mongoose' {
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
) => void | Promise<void>
): this;
pre<T = TModelType>(
method: 'bulkWrite' | RegExp,
options: SchemaPreOptions,
fn: (
this: T,
next: (err?: CallbackError) => void,
ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
) => void | Promise<void>
): this;
/* method createCollection */
pre<T = TModelType>(
method: 'createCollection' | RegExp,
Expand All @@ -458,15 +437,6 @@ declare module 'mongoose' {
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
) => void | Promise<void>
): this;
pre<T = TModelType>(
method: 'createCollection' | RegExp,
options: SchemaPreOptions,
fn: (
this: T,
next: (err?: CallbackError) => void,
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
) => void | Promise<void>
): this;

/** Object of currently defined query helpers on this schema. */
query: TQueryHelpers;
Expand Down Expand Up @@ -715,5 +685,9 @@ declare module 'mongoose' {
/* for ts-mongoose */
export class mquery { }

export function overwriteMiddlewareResult(val: any): Kareem.OverwriteMiddlewareResult;

export function skipMiddlewareFunction(val: any): Kareem.SkipWrappedFunction;

export default mongoose;
}
9 changes: 5 additions & 4 deletions types/middlewares.d.ts
@@ -1,4 +1,5 @@
declare module 'mongoose' {
import Kareem = require('kareem');

type MongooseQueryAndDocumentMiddleware = 'updateOne' | 'deleteOne';

Expand Down Expand Up @@ -37,13 +38,13 @@ declare module 'mongoose' {
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts?: Record<string, any>
) => void | Promise<void>;
) => void | Promise<void> | Kareem.SkipWrappedFunction;
type PreSaveMiddlewareFunction<ThisType = any> = (
this: ThisType,
next: CallbackWithoutResultAndOptionalError,
opts: SaveOptions
) => void | Promise<void>;
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
) => void | Promise<void> | Kareem.SkipWrappedFunction;
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;
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>;
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void> | Kareem.OverwriteMiddlewareResult;
}