Skip to content

Commit 5c9dbb3

Browse files
committed
feat: enhance aggregation and find functions with detailed parameter documentation
1 parent 6e7401d commit 5c9dbb3

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/aggregate.ts

+38
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,44 @@ interface AggregateParams extends PaginationParams {
1616
collation?: Record<string, any> | null;
1717
}
1818

19+
/**
20+
* Performs an aggregate() query on a passed-in Mongo collection, using criteria you specify.
21+
* Unlike `find()`, this method requires fine tuning by the user, and must comply with the following
22+
* two criteria so that the pagination magic can work properly.
23+
*
24+
* 1. `aggregate()` will insert a `$sort` and `$limit` clauses in your aggregation pipeline immediately after
25+
* the first $match is found. Consider this while building your pipeline.
26+
*
27+
* 2. The documents resulting from the aggregation _must_ contain the paginated fields so that a
28+
* cursor can be built from the result set.
29+
*
30+
* Additionally, an additional query will be appended to the first `$match` found in order to apply the offset
31+
* required for the cursor.
32+
*
33+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
34+
* or the mongoist package's `db.collection(<collectionName>)` method.
35+
* @param {Object} params
36+
* -aggregation {Object[]} The aggregation query.
37+
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
38+
* -paginatedField {String} The field name to query the range for. The field must be:
39+
* 1. Orderable. We must sort by this value. If duplicate values for paginatedField field
40+
* exist, the results will be secondarily ordered by the _id.
41+
* 2. Immutable. If the value changes between paged queries, it could appear twice.
42+
* 3. Accessible. The field must be present in the aggregation's end result so the
43+
* aggregation steps added at the end of the pipeline to implement the paging can access it.
44+
4. Consistent. All values (except undefined and null values) must be of the same type.
45+
* The default is to use the Mongo built-in '_id' field, which satisfies the above criteria.
46+
* The only reason to NOT use the Mongo _id field is if you chose to implement your own ids.
47+
* -sortAscending {boolean} Whether to sort in ascending order by the `paginatedField`.
48+
* -sortCaseInsensitive {boolean} Whether to ignore case when sorting, in which case `paginatedField`
49+
* must be a string property.
50+
* -next {String} The value to start querying the page.
51+
* -previous {String} The value to start querying previous page.
52+
* -after {String} The _id to start querying the page.
53+
* -before {String} The _id to start querying previous page.
54+
* -options {Object} Aggregation options
55+
* -collation {Object} An optional collation to provide to the mongo query. E.g. { locale: 'en', strength: 2 }. When null, disables the global collation.
56+
*/
1957
export default async function aggregate(
2058
collection: Collection<Document>,
2159
params: AggregateParams

src/find.ts

+29
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,35 @@ import {
1111
} from './utils/query';
1212
import sanitizeParams, { SanitizeParams } from './utils/sanitizeParams';
1313

14+
/**
15+
* Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
16+
* are ordered by the paginatedField.
17+
*
18+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
19+
* or the mongoist package's `db.collection(<collectionName>)` method.
20+
* @param {Object} params
21+
* -query {Object} The find query.
22+
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
23+
* -fields {Object} Fields to query in the Mongo object format, e.g. {_id: 1, timestamp :1}.
24+
* The default is to query all fields.
25+
* -paginatedField {String} The field name to query the range for. The field must be:
26+
* 1. Orderable. We must sort by this value. If duplicate values for paginatedField field
27+
* exist, the results will be secondarily ordered by the _id.
28+
* 2. Indexed. For large collections, this should be indexed for query performance.
29+
* 3. Immutable. If the value changes between paged queries, it could appear twice.
30+
4. Consistent. All values (except undefined and null values) must be of the same type.
31+
* The default is to use the Mongo built-in '_id' field, which satisfies the above criteria.
32+
* The only reason to NOT use the Mongo _id field is if you chose to implement your own ids.
33+
* -sortAscending {boolean} Whether to sort in ascending order by the `paginatedField`.
34+
* -sortCaseInsensitive {boolean} Whether to ignore case when sorting, in which case `paginatedField`
35+
* must be a string property.
36+
* -next {String} The value to start querying the page.
37+
* -previous {String} The value to start querying previous page.
38+
* -after {String} The _id to start querying the page.
39+
* -before {String} The _id to start querying previous page.
40+
* -hint {String} An optional index hint to provide to the mongo query
41+
* -collation {Object} An optional collation to provide to the mongo query. E.g. { locale: 'en', strength: 2 }. When null, disables the global collation.
42+
*/
1443
export interface FindParams extends PaginationParams {
1544
query?: Document;
1645
limit?: number;

src/search.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export interface SearchResponse<T = Document> {
1717
}
1818

1919
/**
20-
* Performs a search query on a Mongo collection and pages the results. The results are ordered
21-
* by their relevancy using MongoDB's $text index.
22-
*
20+
* Performs a search query on a Mongo collection and pages the results. This is different from
21+
* find() in that the results are ordered by their relevancy, and as such, it does not take
22+
* a paginatedField parameter. Note that this is less performant than find() because it must
23+
* perform the full search on each call to this function.
2324
* @param collection - A MongoDB collection object. This MUST have a Mongo $text index.
2425
* @param searchString - The string to search for.
2526
* @param params - Search parameters:

0 commit comments

Comments
 (0)