Skip to content

Commit

Permalink
fix(index.d.ts): make all query methods instead return QueryWithHelpe…
Browse files Browse the repository at this point in the history
…rs so they always have helper methods

Fix #9850
  • Loading branch information
vkarpov15 committed Mar 3, 2021
1 parent 206fc4e commit a5c7f22
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ declare module 'mongoose' {
*/
export function isValidObjectId(v: any): boolean;

export function model<T extends Document, TQueryHelpers = any>(name: string, schema?: Schema<any>, collection?: string, skipInit?: boolean): Model<T, TQueryHelpers>;
export function model<T extends Document, U extends Model<T, TQueryHelpers>, TQueryHelpers = any>(
export function model<T extends Document, TQueryHelpers = {}>(name: string, schema?: Schema<any>, collection?: string, skipInit?: boolean): Model<T, TQueryHelpers>;
export function model<T extends Document, U extends Model<T, TQueryHelpers>, TQueryHelpers = {}>(
name: string,
schema?: Schema<T, U>,
collection?: string,
Expand Down Expand Up @@ -244,7 +244,7 @@ declare module 'mongoose' {

/** Defines or retrieves a model. */
model<T extends Document>(name: string, schema?: Schema<T>, collection?: string): Model<T>;
model<T extends Document, U extends Model<T, TQueryHelpers>, TQueryHelpers = {}>(
model<T extends Document, U extends Model<T, TQueryHelpers>, TQueryHelpers = undefined>(
name: string,
schema?: Schema<T, U, TQueryHelpers>,
collection?: string,
Expand Down Expand Up @@ -366,7 +366,7 @@ declare module 'mongoose' {
getIndexes(): any;
}

class Document<T = any> {
class Document<T = any, TQueryHelpers = {}> {
constructor(doc?: any);

/** This documents _id. */
Expand Down Expand Up @@ -435,11 +435,11 @@ declare module 'mongoose' {
db: Connection;

/** Removes this document from the db. */
delete(options?: QueryOptions): Query<any, this>;
delete(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
delete(options?: QueryOptions, cb?: (err: CallbackError, res: any) => void): void;

/** Removes this document from the db. */
deleteOne(options?: QueryOptions): Query<any, this>;
deleteOne(options?: QueryOptions): QueryWithHelpers<any, this, TQueryHelpers>;
deleteOne(options?: QueryOptions, cb?: (err: CallbackError, res: any) => void): void;

/** Takes a populated field and returns it to its unpopulated state. */
Expand Down Expand Up @@ -594,7 +594,7 @@ declare module 'mongoose' {

export const Model: Model<any>;
// eslint-disable-next-line no-undef
interface Model<T extends Document, TQueryHelpers = any> extends NodeJS.EventEmitter {
interface Model<T extends Document, TQueryHelpers = undefined> extends NodeJS.EventEmitter {
new(doc?: any): T;

aggregate<R = any>(pipeline?: any[]): Aggregate<Array<R>>;
Expand Down Expand Up @@ -624,12 +624,12 @@ declare module 'mongoose' {
collection: Collection;

/** Creates a `count` query: counts the number of documents that match `filter`. */
count(callback?: (err: any, count: number) => void): Query<number, T, TQueryHelpers>;
count(filter: FilterQuery<T>, callback?: (err: any, count: number) => void): Query<number, T, TQueryHelpers>;
count(callback?: (err: any, count: number) => void): QueryWithHelpers<number, T, TQueryHelpers>;
count(filter: FilterQuery<T>, callback?: (err: any, count: number) => void): QueryWithHelpers<number, T, TQueryHelpers>;

/** Creates a `countDocuments` query: counts the number of documents that match `filter`. */
countDocuments(callback?: (err: any, count: number) => void): Query<number, T, TQueryHelpers>;
countDocuments(filter: FilterQuery<T>, callback?: (err: any, count: number) => void): Query<number, T, TQueryHelpers>;
countDocuments(callback?: (err: any, count: number) => void): QueryWithHelpers<number, T, TQueryHelpers>;
countDocuments(filter: FilterQuery<T>, callback?: (err: any, count: number) => void): QueryWithHelpers<number, T, TQueryHelpers>;

/** Creates a new document or documents */
create(doc: T | DocumentDefinition<T>): Promise<T>;
Expand Down Expand Up @@ -665,14 +665,14 @@ declare module 'mongoose' {
* Behaves like `remove()`, but deletes all documents that match `conditions`
* regardless of the `single` option.
*/
deleteMany(filter?: FilterQuery<T>, options?: QueryOptions, callback?: (err: CallbackError) => void): Query<mongodb.DeleteWriteOpResultObject['result'] & { deletedCount?: number }, T, TQueryHelpers>;
deleteMany(filter?: FilterQuery<T>, options?: QueryOptions, callback?: (err: CallbackError) => void): QueryWithHelpers<mongodb.DeleteWriteOpResultObject['result'] & { deletedCount?: number }, T, TQueryHelpers>;

/**
* Deletes the first document that matches `conditions` from the collection.
* Behaves like `remove()`, but deletes at most one document regardless of the
* `single` option.
*/
deleteOne(filter?: FilterQuery<T>, options?: QueryOptions, callback?: (err: CallbackError) => void): Query<mongodb.DeleteWriteOpResultObject['result'] & { deletedCount?: number }, T, TQueryHelpers>;
deleteOne(filter?: FilterQuery<T>, options?: QueryOptions, callback?: (err: CallbackError) => void): QueryWithHelpers<mongodb.DeleteWriteOpResultObject['result'] & { deletedCount?: number }, T, TQueryHelpers>;

/**
* Sends `createIndex` commands to mongo for each index declared in the schema.
Expand All @@ -693,10 +693,10 @@ declare module 'mongoose' {
* equivalent to `findOne({ _id: id })`. If you want to query by a document's
* `_id`, use `findById()` instead of `findOne()`.
*/
findById(id: any, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): Query<T | null, T, TQueryHelpers>;
findById(id: any, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Finds one document. */
findOne(filter?: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): Query<T | null, T, TQueryHelpers>;
findOne(filter?: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: (err: CallbackError, doc: T | null) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/**
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
Expand Down Expand Up @@ -766,7 +766,7 @@ declare module 'mongoose' {

/** Adds a `$where` clause to this query */
// eslint-disable-next-line @typescript-eslint/ban-types
$where(argument: string | Function): Query<Array<T>, T, TQueryHelpers>;
$where(argument: string | Function): QueryWithHelpers<Array<T>, T, TQueryHelpers>;

/** Registered discriminators for this model. */
discriminators: { [name: string]: Model<any> } | undefined;
Expand All @@ -778,10 +778,10 @@ declare module 'mongoose' {
discriminator<D extends Document>(name: string, schema: Schema, value?: string): Model<D>;

/** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
distinct(field: string, filter?: FilterQuery<T>, callback?: (err: any, count: number) => void): Query<Array<any>, T, TQueryHelpers>;
distinct(field: string, filter?: FilterQuery<T>, callback?: (err: any, count: number) => void): QueryWithHelpers<Array<any>, T, TQueryHelpers>;

/** Creates a `estimatedDocumentCount` query: counts the number of documents in the collection. */
estimatedDocumentCount(options?: QueryOptions, callback?: (err: any, count: number) => void): Query<number, T, TQueryHelpers>;
estimatedDocumentCount(options?: QueryOptions, callback?: (err: any, count: number) => void): QueryWithHelpers<number, T, TQueryHelpers>;

/**
* Returns true if at least one document exists in the database that matches
Expand All @@ -791,48 +791,48 @@ declare module 'mongoose' {
exists(filter: FilterQuery<T>, callback: (err: any, res: boolean) => void): void;

/** Creates a `find` query: gets a list of documents that match `filter`. */
find(callback?: (err: any, docs: T[]) => void): Query<Array<T>, T, TQueryHelpers> & TQueryHelpers;
find(filter: FilterQuery<T>, callback?: (err: any, docs: T[]) => void): Query<Array<T>, T, TQueryHelpers> & TQueryHelpers;
find(filter: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: (err: any, docs: T[]) => void): Query<Array<T>, T, TQueryHelpers> & TQueryHelpers;
find(callback?: (err: any, docs: T[]) => void): QueryWithHelpers<Array<T>, T, TQueryHelpers>;
find(filter: FilterQuery<T>, callback?: (err: any, docs: T[]) => void): QueryWithHelpers<Array<T>, T, TQueryHelpers>;
find(filter: FilterQuery<T>, projection?: any | null, options?: QueryOptions | null, callback?: (err: any, docs: T[]) => void): QueryWithHelpers<Array<T>, T, TQueryHelpers>;

/** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findByIdAndDelete(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
findByIdAndRemove(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findByIdAndRemove(id?: mongodb.ObjectId | any, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject<T>, res: any) => void): Query<mongodb.FindAndModifyWriteOpResultObject<T>, T, TQueryHelpers>;
findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query<T, T, TQueryHelpers>;
findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject<T>, res: any) => void): QueryWithHelpers<mongodb.FindAndModifyWriteOpResultObject<T>, T, TQueryHelpers>;
findByIdAndUpdate(id: mongodb.ObjectId | any, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers<T, T, TQueryHelpers>;
findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
findOneAndDelete(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findOneAndDelete(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findOneAndRemove` query: atomically finds the given document and deletes it. */
findOneAndRemove(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findOneAndRemove(filter?: FilterQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
findOneAndReplace(filter: FilterQuery<T>, replacement: DocumentDefinition<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query<T, T, TQueryHelpers>;
findOneAndReplace(filter?: FilterQuery<T>, replacement?: DocumentDefinition<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findOneAndReplace(filter: FilterQuery<T>, replacement: DocumentDefinition<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers<T, T, TQueryHelpers>;
findOneAndReplace(filter?: FilterQuery<T>, replacement?: DocumentDefinition<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

/** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject<T>, res: any) => void): Query<mongodb.FindAndModifyWriteOpResultObject<T>, T, TQueryHelpers>;
findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): Query<T, T, TQueryHelpers>;
findOneAndUpdate(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): Query<T | null, T, TQueryHelpers>;
findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { rawResult: true }, callback?: (err: any, doc: mongodb.FindAndModifyWriteOpResultObject<T>, res: any) => void): QueryWithHelpers<mongodb.FindAndModifyWriteOpResultObject<T>, T, TQueryHelpers>;
findOneAndUpdate(filter: FilterQuery<T>, update: UpdateQuery<T>, options: QueryOptions & { upsert: true } & ReturnsNewDoc, callback?: (err: any, doc: T, res: any) => void): QueryWithHelpers<T, T, TQueryHelpers>;
findOneAndUpdate(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, doc: T | null, res: any) => void): QueryWithHelpers<T | null, T, TQueryHelpers>;

geoSearch(filter?: FilterQuery<T>, options?: GeoSearchOptions, callback?: (err: CallbackError, res: Array<T>) => void): Query<Array<T>, T, TQueryHelpers>;
geoSearch(filter?: FilterQuery<T>, options?: GeoSearchOptions, callback?: (err: CallbackError, res: Array<T>) => void): QueryWithHelpers<Array<T>, T, TQueryHelpers>;

/** Executes a mapReduce command. */
mapReduce<Key, Value>(
o: MapReduceOptions<T, Key, Value>,
callback?: (err: any, res: any) => void
): Promise<any>;

remove(filter?: any, callback?: (err: CallbackError) => void): Query<any, T, TQueryHelpers>;
remove(filter?: any, callback?: (err: CallbackError) => void): QueryWithHelpers<any, T, TQueryHelpers>;

/** Creates a `replaceOne` query: finds the first document that matches `filter` and replaces it with `replacement`. */
replaceOne(filter?: FilterQuery<T>, replacement?: DocumentDefinition<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query<any, T, TQueryHelpers>;
replaceOne(filter?: FilterQuery<T>, replacement?: DocumentDefinition<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers<any, T, TQueryHelpers>;

/** Schema the model uses. */
schema: Schema;
Expand All @@ -841,18 +841,18 @@ declare module 'mongoose' {
* @deprecated use `updateOne` or `updateMany` instead.
* Creates a `update` query: updates one or many documents that match `filter` with `update`, based on the `multi` option.
*/
update(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query<mongodb.WriteOpResult['result'], T, TQueryHelpers>;
update(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers<mongodb.WriteOpResult['result'], T, TQueryHelpers>;

/** Creates a `updateMany` query: updates all documents that match `filter` with `update`. */
updateMany(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query<mongodb.UpdateWriteOpResult['result'], T, TQueryHelpers>;
updateMany(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers<mongodb.UpdateWriteOpResult['result'], T, TQueryHelpers>;

/** Creates a `updateOne` query: updates the first document that matches `filter` with `update`. */
updateOne(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): Query<mongodb.UpdateWriteOpResult['result'], T, TQueryHelpers>;
updateOne(filter?: FilterQuery<T>, update?: UpdateQuery<T>, options?: QueryOptions | null, callback?: (err: any, res: any) => void): QueryWithHelpers<mongodb.UpdateWriteOpResult['result'], T, TQueryHelpers>;

/** Creates a Query, applies the passed conditions, and returns the Query. */
where(path: string, val?: any): Query<Array<T>, T, TQueryHelpers>;
where(obj: object): Query<Array<T>, T, TQueryHelpers>;
where(): Query<Array<T>, T, TQueryHelpers>;
where(path: string, val?: any): QueryWithHelpers<Array<T>, T, TQueryHelpers>;
where(obj: object): QueryWithHelpers<Array<T>, T, TQueryHelpers>;
where(): QueryWithHelpers<Array<T>, T, TQueryHelpers>;
}

interface QueryOptions {
Expand Down Expand Up @@ -1052,7 +1052,7 @@ declare module 'mongoose' {
type SchemaPreOptions = { document?: boolean, query?: boolean };
type SchemaPostOptions = { document?: boolean, query?: boolean };

class Schema<DocType extends Document = Document, M extends Model<DocType> = Model<DocType>, SchemaDefinitionType = undefined> extends events.EventEmitter {
class Schema<DocType extends Document = Document, M extends Model<DocType, any> = Model<DocType>, SchemaDefinitionType = undefined> extends events.EventEmitter {
/**
* Create a new schema
*/
Expand Down Expand Up @@ -1827,7 +1827,9 @@ declare module 'mongoose' {

type ReturnsNewDoc = { new: true } | { returnOriginal: false };

class Query<ResultType, DocType extends Document, THelpers = any> {
type QueryWithHelpers<ResultType, DocType extends Document, THelpers = {}> = Query<ResultType, DocType, THelpers> & THelpers;

class Query<ResultType, DocType extends Document, THelpers = {}> {
_mongooseOptions: MongooseQueryOptions;

/** Executes the query */
Expand Down

0 comments on commit a5c7f22

Please sign in to comment.