Skip to content

Commit

Permalink
chore(index.d.ts): add query helpers typescript definitions
Browse files Browse the repository at this point in the history
Re: #8108
  • Loading branch information
vkarpov15 committed Oct 10, 2020
1 parent 6cc6769 commit 649172d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 28 deletions.
103 changes: 101 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,19 @@ declare module "mongoose" {

$where(argument: string | Function): Query<Array<DocType>, DocType>;

/** Specifies an `$all` query condition. When called with one argument, the most recent path passed to `where()` is used. */
all(val: Array<any>): this;
all(path: string, val: Array<any>): this;

/** Specifies arguments for an `$and` condition. */
and(array: Array<FilterQuery<DocType>>): this;

/** Specifies the batchSize option. */
batchSize(val: number): this;

/** Specifies the `comment` option. */
comment(val: string): this;

/** Specifies this query as a `count` query. */
count(callback?: (err: any, count: number) => void): Query<number, DocType>;
count(criteria: FilterQuery<DocType>, callback?: (err: any, count: number) => void): Query<number, DocType>;
Expand All @@ -937,9 +950,20 @@ declare module "mongoose" {
/** Creates a `distinct` query: returns the distinct values of the given `field` that match `filter`. */
distinct(field: string, filter?: FilterQuery<DocType>, callback?: (err: any, count: number) => void): Query<Array<any>, DocType>;

/** Specifies a `$elemMatch` query condition. When called with one argument, the most recent path passed to `where()` is used. */
elemMatch(val: Function | any): this;
elemMatch(path: string, val: Function | any): this;

/** Specifies the complementary comparison value for paths specified with `where()` */
equals(val: any): this;

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

/** Specifies a `$exists` query condition. When called with one argument, the most recent path passed to `where()` is used. */
exists(val: boolean): this;
exists(path: string, val: boolean): this;

/** Creates a `find` query: gets a list of documents that match `filter`. */
find(callback?: (err: any, count: number) => void): Query<Array<DocType>, DocType>;
find(filter: FilterQuery<DocType>, callback?: (err: any, count: number) => void): Query<Array<DocType>, DocType>;
Expand All @@ -960,8 +984,83 @@ declare module "mongoose" {
/** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
findByIdAndUpdate(id?: mongodb.ObjectId | any, update?: UpdateQuery<DocType>, options?: QueryOptions | null, callback?: (err: any, doc: DocType | null, res: any) => void): Query<DocType | null, DocType>;

/** Creates a Query, applies the passed conditions, and returns the Query. */
where(path: string, val?: any): Query<Array<DocType>, DocType>;
/** Specifies a `$gt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
gt(val: number): this;
gt(path: string, val: number): this;

/** Specifies a `$gte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
gte(val: number): this;
gte(path: string, 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(val: Array<any>): this;
in(path: string, val: Array<any>): this;

/** Specifies the maximum number of documents the query will return. */
limit(val: number): this;

/** Specifies a `$lt` query condition. When called with one argument, the most recent path passed to `where()` is used. */
lt(val: number): this;
lt(path: string, val: number): this;

/** Specifies a `$lte` query condition. When called with one argument, the most recent path passed to `where()` is used. */
lte(val: number): this;
lte(path: string, val: number): this;

/** Specifies an `$maxDistance` query condition. When called with one argument, the most recent path passed to `where()` is used. */
maxDistance(val: number): this;
maxDistance(path: string, val: number): this;

/** Specifies the maxScan option. */
maxScan(val: number): this;

/** Specifies a `$mod` condition, filters documents for documents whose `path` property is a number that is equal to `remainder` modulo `divisor`. */
mod(val: Array<number>): this;
mod(path: string, val: Array<number>): this;

/** Specifies a `$ne` query condition. When called with one argument, the most recent path passed to `where()` is used. */
ne(val: any): this;
ne(path: string, val: any);

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

/** Specifies arguments for an `$nor` condition. */
nor(array: Array<FilterQuery<DocType>>): this;

/** Specifies arguments for an `$or` condition. */
or(array: Array<FilterQuery<DocType>>): this;

/** Specifies a `$regex` query condition. When called with one argument, the most recent path passed to `where()` is used. */
regex(val: string | RegExp): this;
regex(path: string, val: string | RegExp): this;

/** Specifies an `$size` query condition. When called with one argument, the most recent path passed to `where()` is used. */
size(val: number): this;
size(path: string, val: number): this;

/** Specifies the number of documents to skip. */
skip(val: number): this;

/** Specifies a `$slice` projection for an array. */
slice(val: number | Array<number>): this;
slice(path: string, val: number | Array<number>): this;

/** Specifies this query as a `snapshot` query. */
snapshot(val?: boolean): this;

/** Converts this query to a customized, reusable query constructor with all arguments and options retained. */
toConstructor(): new (filter?: FilterQuery<DocType>, options?: QueryOptions) => Query<ResultType, DocType>;

/** Specifies a path for use with chaining. */
where(path: string, val?: any): this;

/** Defines a `$within` or `$geoWithin` argument for geo-spatial queries. */
within(val?: any): this;
}

export type FilterQuery<T> = {
Expand Down
52 changes: 26 additions & 26 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,28 @@ Query.prototype.toConstructor = function toConstructor() {
* @api public
*/

/**
* Specifies a `$slice` projection for an array.
*
* ####Example
*
* query.slice('comments', 5)
* query.slice('comments', -5)
* query.slice('comments', [10, 5])
* query.where('comments').slice(5)
* query.where('comments').slice([-10, 5])
*
* @method slice
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val number/range of elements to slice
* @return {Query} this
* @see mongodb http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements
* @see $slice http://docs.mongodb.org/manual/reference/projection/slice/#prj._S_slice
* @api public
*/

Query.prototype.slice = function() {
if (arguments.length === 0) {
return this;
Expand Down Expand Up @@ -458,7 +480,7 @@ Query.prototype.slice = function() {
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val
* @param {any} val
* @api public
*/

Expand All @@ -472,7 +494,7 @@ Query.prototype.slice = function() {
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val
* @param {Array} val
* @api public
*/

Expand All @@ -486,7 +508,7 @@ Query.prototype.slice = function() {
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val
* @param {Array} val
* @api public
*/

Expand Down Expand Up @@ -626,7 +648,7 @@ Query.prototype.mod = function() {
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val
* @param {Boolean} val
* @return {Query} this
* @see $exists http://docs.mongodb.org/manual/reference/operator/exists/
* @api public
Expand Down Expand Up @@ -700,28 +722,6 @@ Query.prototype.mod = function() {
* @api public
*/

/**
* Specifies a `$slice` projection for an array.
*
* ####Example
*
* query.slice('comments', 5)
* query.slice('comments', -5)
* query.slice('comments', [10, 5])
* query.where('comments').slice(5)
* query.where('comments').slice([-10, 5])
*
* @method slice
* @memberOf Query
* @instance
* @param {String} [path]
* @param {Number} val number/range of elements to slice
* @return {Query} this
* @see mongodb http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements
* @see $slice http://docs.mongodb.org/manual/reference/projection/slice/#prj._S_slice
* @api public
*/

/**
* Specifies the maximum number of documents the query will return.
*
Expand Down

0 comments on commit 649172d

Please sign in to comment.