Skip to content

Commit

Permalink
perf(types): some more micro-optimizations re: #10349, remove extra t…
Browse files Browse the repository at this point in the history
…ype checking on $ne, etc.
  • Loading branch information
vkarpov15 committed Aug 12, 2022
1 parent 339adb6 commit c1b7b6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 30 deletions.
10 changes: 4 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ declare module 'mongoose' {
export function get<K extends keyof MongooseOptions>(key: K): MongooseOptions[K];

/* ! ignore */
export type CompileModelOptions = { overwriteModels?: boolean, connection?: Connection };
export type CompileModelOptions = {
overwriteModels?: boolean,
connection?: Connection
};

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

export type RequireOnlyTypedId<T> = T extends { _id?: infer U; }
? Required<{ _id: U }>
: { _id: Types.ObjectId };

export type HydratedDocument<DocType, TMethodsAndOverrides = {}, TVirtuals = {}> = DocType extends Document ? Require_id<DocType> : (Document<unknown, any, DocType> & Require_id<DocType> & TVirtuals & TMethodsAndOverrides);

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


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

export type _LeanDocument<T> = {
Expand Down
20 changes: 10 additions & 10 deletions types/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,21 @@ declare module 'mongoose' {
init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;

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

insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
insertMany<DocContents = T>(docs: Array<DocContents | T>, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>>>;
insertMany<DocContents = T>(docs: Array<DocContents | T>): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { lean: true; }): Promise<Array<MergeType<MergeType<T, DocContents>, Require_id<T>>>>;
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions & { rawResult: true; }): Promise<mongodb.InsertManyResult<T>>;
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, RequireOnlyTypedId<T>>, TMethodsAndOverrides, TVirtuals>>>;
insertMany<DocContents = T>(doc: DocContents, options: InsertManyOptions): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;
insertMany<DocContents = T>(doc: DocContents): Promise<Array<HydratedDocument<MergeType<MergeType<T, DocContents>, Require_id<T>>, TMethodsAndOverrides, TVirtuals>>>;

/** The name of the model */
modelName: string;
Expand Down
28 changes: 14 additions & 14 deletions types/query.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ declare module 'mongoose' {
distinct<ReturnType = any>(field: string, filter?: FilterQuery<DocType>, callback?: Callback<number>): QueryWithHelpers<Array<ReturnType>, DocType, THelpers, RawDocType>;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit c1b7b6b

Please sign in to comment.