Skip to content

Commit 80e08dd

Browse files
authored
Merge pull request #2785 from qinyang912/master
FIX postInsert hook not working when use bulkInsert to insert doc
2 parents 975aa46 + 1ced28d commit 80e08dd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/rx-collection.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,6 @@ export class RxCollectionBase<
419419
const startTime = now();
420420
return this.pouch.bulkDocs(insertDocs)
421421
.then(results => {
422-
const endTime = now();
423422
const okResults = results.filter(r => r.ok);
424423

425424
// create documents
@@ -430,6 +429,20 @@ export class RxCollectionBase<
430429
return doc;
431430
});
432431

432+
return Promise.all(
433+
rxDocuments.map(doc => {
434+
return this._runHooks(
435+
'post',
436+
'insert',
437+
docsMap.get(doc.primary),
438+
doc
439+
);
440+
})
441+
).then(() => {
442+
return { rxDocuments, errorResults: results.filter(r => !r.ok) };
443+
});
444+
}).then(({ rxDocuments, errorResults }) => {
445+
const endTime = now();
433446
// emit events
434447
rxDocuments.forEach(doc => {
435448
const emitEvent = createInsertEvent(
@@ -444,7 +457,7 @@ export class RxCollectionBase<
444457

445458
return {
446459
success: rxDocuments,
447-
error: results.filter(r => !r.ok)
460+
error: errorResults
448461
};
449462
});
450463
}

test/unit/hooks.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ config.parallel('hooks.test.js', () => {
180180
assert.strictEqual(count, 1);
181181
c.database.destroy();
182182
});
183+
it('should call post insert hook after bulkInsert', async () => {
184+
const c = await humansCollection.create(0);
185+
const human = schemaObjects.human();
186+
let count = 0;
187+
c.postInsert((data, instance) => {
188+
assert.ok(data.age);
189+
assert.ok(isRxDocument(instance));
190+
count++;
191+
}, false);
192+
await c.bulkInsert([human]);
193+
assert.strictEqual(count, 1);
194+
c.database.destroy();
195+
});
183196
});
184197
});
185198
});

0 commit comments

Comments
 (0)