Skip to content

Commit c1b7b6b

Browse files
committed
perf(types): some more micro-optimizations re: #10349, remove extra type checking on $ne, etc.
1 parent 339adb6 commit c1b7b6b

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

types/index.d.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ declare module 'mongoose' {
6262
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];
6363

6464
/* ! ignore */
65-
export type CompileModelOptions = { overwriteModels?: boolean, connection?: Connection };
65+
export type CompileModelOptions = {
66+
overwriteModels?: boolean,
67+
connection?: Connection
68+
};
6669

6770
export function model<TSchema extends Schema = any>(
6871
name: string,
@@ -113,10 +116,6 @@ declare module 'mongoose' {
113116
? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
114117
: T & { _id: Types.ObjectId };
115118

116-
export type RequireOnlyTypedId<T> = T extends { _id?: infer U; }
117-
? Required<{ _id: U }>
118-
: { _id: Types.ObjectId };
119-
120119
export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);
121120

122121
export interface TagSet {
@@ -516,7 +515,6 @@ declare module 'mongoose' {
516515
T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
517516
LeanDocument<T>; // Documents and everything else
518517

519-
520518
export type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
521519

522520
export type _LeanDocument<T> = {

types/models.d.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,21 @@ declare module 'mongoose' {
260260
init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
261261

262262
/** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
263-
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>): void;
263+
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>): void;
264264
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { rawResult: true; }, callback: Callback<mongodb.InsertManyResult<T>>): void;
265-
insertMany<DocContents = T>(docs: Array<DocContents | T>, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
266-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>): void;
265+
insertMany<DocContents = T>(docs: Array<DocContents | T>, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
266+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }, callback: Callback<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>): void;
267267
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { rawResult: true; }, callback: Callback<mongodb.InsertManyResult<T>>): void;
268-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean?: false | undefined }, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
269-
insertMany<DocContents = T>(doc: DocContents, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
268+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean?: false | undefined }, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
269+
insertMany<DocContents = T>(doc: DocContents, callback: Callback<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>): void;
270270

271-
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
271+
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
272272
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
273-
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
274-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
273+
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
274+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
275275
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
276-
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
277-
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
276+
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
277+
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
278278

279279
/** The name of the model */
280280
modelName: string;

types/query.d.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ declare module 'mongoose' {
271271
distinct<ReturnType = any>(field: string, filter?: FilterQuery<DocType>, callback?: Callback<number>): QueryWithHelpers<Array<ReturnType>, DocType, THelpers, RawDocType>;
272272

273273
/** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
274-
elemMatch<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$elemMatch']): this;
274+
elemMatch<K = string>(path: K, val: any): this;
275275
elemMatch(val: Function | any): this;
276276

277277
/**
@@ -288,7 +288,7 @@ declare module 'mongoose' {
288288
estimatedDocumentCount(options?: QueryOptions<DocType>, callback?: Callback<number>): QueryWithHelpers<number, DocType, THelpers, RawDocType>;
289289

290290
/** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
291-
exists<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$exists']): this;
291+
exists<K = string>(path: K, val: boolean): this;
292292
exists(val: boolean): this;
293293

294294
/**
@@ -403,18 +403,18 @@ declare module 'mongoose' {
403403
getUpdate(): UpdateQuery<DocType> | UpdateWithAggregationPipeline | null;
404404

405405
/** Specifies a `$gt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
406-
gt<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$gt']): this;
406+
gt<K = string>(path: K, val: any): this;
407407
gt(val: number): this;
408408

409409
/** Specifies a `$gte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
410-
gte<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$gte']): this;
410+
gte<K = string>(path: K, val: any): this;
411411
gte(val: number): this;
412412

413413
/** Sets query hints. */
414414
hint(val: any): this;
415415

416416
/** Specifies an `$in` query condition. When called with one argument, the most recent path passed to `where()` is used. */
417-
in<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$in']): this;
417+
in<K = string>(path: K, val: any[]): this;
418418
in(val: Array<any>): this;
419419

420420
/** Declares an intersects query for `geometry()`. */
@@ -430,11 +430,11 @@ declare module 'mongoose' {
430430
limit(val: number): this;
431431

432432
/** Specifies a `$lt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
433-
lt<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$lt']): this;
433+
lt<K = string>(path: K, val: any): this;
434434
lt(val: number): this;
435435

436436
/** Specifies a `$lte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
437-
lte<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$lte']): this;
437+
lte<K = string>(path: K, val: any): this;
438438
lte(val: number): this;
439439

440440
/**
@@ -461,7 +461,7 @@ declare module 'mongoose' {
461461
merge(source: Query<any, any> | FilterQuery<DocType>): this;
462462

463463
/** Specifies a `$mod` condition, filters documents for documents whose `path` property is a number that is equal to `remainder` modulo `divisor`. */
464-
mod<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$mod']): this;
464+
mod<K = string>(path: K, val: number): this;
465465
mod(val: Array<number>): this;
466466

467467
/** The model this query was created from */
@@ -474,15 +474,15 @@ declare module 'mongoose' {
474474
mongooseOptions(val?: MongooseQueryOptions): MongooseQueryOptions;
475475

476476
/** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
477-
ne<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$ne']): this;
477+
ne<K = string>(path: K, val: any): this;
478478
ne(val: any): this;
479479

480480
/** Specifies a `$near` or `$nearSphere` condition */
481-
near<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$near']): this;
481+
near<K = string>(path: K, val: any): this;
482482
near(val: any): this;
483483

484484
/** Specifies an `$nin` query condition. When called with one argument, the most recent path passed to `where()` is used. */
485-
nin<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$nin']): this;
485+
nin<K = string>(path: K, val: any[]): this;
486486
nin(val: Array<any>): this;
487487

488488
/** Specifies arguments for an `$nor` condition. */
@@ -518,7 +518,7 @@ declare module 'mongoose' {
518518
readConcern(level: string): this;
519519

520520
/** Specifies a `$regex` query condition. When called with one argument, the most recent path passed to `where()` is used. */
521-
regex<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$regex']): this;
521+
regex<K = string>(path: K, val: RegExp): this;
522522
regex(val: string | RegExp): this;
523523

524524
/**
@@ -570,7 +570,7 @@ declare module 'mongoose' {
570570
setUpdate(update: UpdateQuery<DocType> | UpdateWithAggregationPipeline): void;
571571

572572
/** Specifies an `$size` query condition. When called with one argument, the most recent path passed to `where()` is used. */
573-
size<K extends keyof FilterQuery<DocType>>(path: K, val: FilterQuery<DocType>[K]['$size']): this;
573+
size<K = string>(path: K, val: number): this;
574574
size(val: number): this;
575575

576576
/** Specifies the number of documents to skip. */
@@ -599,7 +599,7 @@ declare module 'mongoose' {
599599
then: Promise<ResultType>['then'];
600600

601601
/** Converts this query to a customized, reusable query constructor with all arguments and options retained. */
602-
toConstructor(): new (...args: any[]) => QueryWithHelpers<ResultType, DocType, THelpers, RawDocType>;
602+
toConstructor(): typeof this;
603603

604604
/** Declare and/or execute this query as an update() operation. */
605605
update(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;

0 commit comments

Comments
 (0)