Skip to content

Commit 0297c6a

Browse files
authored
(#8927) - Set default limit on find queries to 25
1 parent 2e48ed7 commit 0297c6a

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

docs/_includes/api/query_index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
* `$elemMatch` Matches all documents that contain an array field with at least one element that matches all the specified query criteria.
9999
* `fields` (Optional) Defines a list of fields that you want to receive. If omitted, you get the full documents.
100100
* `sort` (Optional) Defines a list of fields defining how you want to sort. Note that sorted fields also have to be selected in the `selector`.
101-
* `limit` (Optional) Maximum number of documents to return.
101+
* `limit` (Optional) Maximum number of documents to return. Default is `25` when connected to a local database.
102102
* `skip` (Optional) Number of docs to skip before returning.
103103
* `use_index` (Optional) Set which index to use for the query. It can be "design-doc-name" or "['design-doc-name', 'name']".
104104

packages/node_modules/pouchdb-find/src/adapters/local/find/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ async function find(db, requestDef, explain) {
9292
requestDef.use_index = massageUseIndex(requestDef.use_index);
9393
}
9494

95+
if (!('limit' in requestDef)) {
96+
// Match the default limit of CouchDB
97+
requestDef.limit = 25;
98+
}
99+
95100
validateFindRequest(requestDef);
96101

97102
const getIndexesRes = await getIndexes(db);
@@ -131,9 +136,7 @@ async function find(db, requestDef, explain) {
131136
if (!queryPlan.inMemoryFields.length) {
132137
// no in-memory filtering necessary, so we can let the
133138
// database do the limit/skip for us
134-
if ('limit' in requestDef) {
135-
opts.limit = requestDef.limit;
136-
}
139+
opts.limit = requestDef.limit;
137140
if ('skip' in requestDef) {
138141
opts.skip = requestDef.skip;
139142
}

tests/find/test-suite-1/test.limit.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,16 @@ describe('test.limit.js', function () {
284284
});
285285
});
286286
});
287+
288+
it('should have default limit of 25', async () => {
289+
const extraDocs = Array
290+
.from({ length: 30 }, (_, i) => ({ name: `Test${i}`, series: 'Test' }));
291+
await context.db.bulkDocs(extraDocs);
292+
293+
const { docs } = await context.db.find({
294+
selector: { series: 'Test' }
295+
});
296+
297+
docs.length.should.equal(25);
298+
});
287299
});

0 commit comments

Comments
 (0)