Skip to content

Commit 00f8406

Browse files
alxndrsnjanl
authored andcommitted
adapter-idb: count docs with attachments in total_rows
Docs initially inserted with attachments were not added to the metaDocs docCount.
1 parent e4ea51c commit 00f8406

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

packages/node_modules/pouchdb-adapter-idb/src/bulkDocs.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,20 +236,20 @@ function idbBulkDocs(dbOpts, req, opts, api, idb, callback) {
236236
var hasAttachments = doc._attachments &&
237237
Object.keys(doc._attachments).length;
238238
if (hasAttachments) {
239-
return writeAttachments(docInfo, winningRev, winningRevIsDeleted,
239+
return writeAttachments(docInfo, delta, winningRev, winningRevIsDeleted,
240240
isUpdate, resultsIdx, callback);
241241
}
242242

243-
docCountDelta += delta;
244-
updateDocCountIfReady();
245-
246-
finishDoc(docInfo, winningRev, winningRevIsDeleted,
243+
finishDoc(docInfo, delta, winningRev, winningRevIsDeleted,
247244
isUpdate, resultsIdx, callback);
248245
}
249246

250-
function finishDoc(docInfo, winningRev, winningRevIsDeleted,
247+
function finishDoc(docInfo, delta, winningRev, winningRevIsDeleted,
251248
isUpdate, resultsIdx, callback) {
252249

250+
docCountDelta += delta;
251+
updateDocCountIfReady();
252+
253253
var doc = docInfo.data;
254254
var metadata = docInfo.metadata;
255255

@@ -305,7 +305,7 @@ function idbBulkDocs(dbOpts, req, opts, api, idb, callback) {
305305
putReq.onerror = afterPutDocError;
306306
}
307307

308-
function writeAttachments(docInfo, winningRev, winningRevIsDeleted,
308+
function writeAttachments(docInfo, delta, winningRev, winningRevIsDeleted,
309309
isUpdate, resultsIdx, callback) {
310310

311311

@@ -316,7 +316,7 @@ function idbBulkDocs(dbOpts, req, opts, api, idb, callback) {
316316

317317
function collectResults() {
318318
if (numDone === attachments.length) {
319-
finishDoc(docInfo, winningRev, winningRevIsDeleted,
319+
finishDoc(docInfo, delta, winningRev, winningRevIsDeleted,
320320
isUpdate, resultsIdx, callback);
321321
}
322322
}

tests/integration/test.basics.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,5 +1339,23 @@ adapters.forEach(function (adapter) {
13391339
}).should.throw(Error, 'Invalid plugin: got "pouchdb-adapter-memory", expected an object or a function');
13401340
});
13411341
}
1342+
1343+
it('#9094 should update total_rows when doc is put with an attachment', async () => {
1344+
// given
1345+
const db = new PouchDB(dbs.name);
1346+
await db.put({ _id:'foo', _attachments:{ 'simple.txt':{ content_type:'text/plain', data:'helo' } } });
1347+
const { rows, total_rows } = await db.allDocs();
1348+
rows.length.should.equal(1);
1349+
total_rows.should.equal(1);
1350+
});
1351+
1352+
it('#9094 should update total_rows when doc is posted with an attachment', async () => {
1353+
// given
1354+
const db = new PouchDB(dbs.name);
1355+
await db.post({ _attachments:{ 'simple.txt':{ content_type:'text/plain', data:'helo' } } });
1356+
const { rows, total_rows } = await db.allDocs();
1357+
rows.length.should.equal(1);
1358+
total_rows.should.equal(1);
1359+
});
13421360
});
13431361
});

0 commit comments

Comments
 (0)