Skip to content

Commit

Permalink
fix: lost RxDocument in preSave from v14.0.0 (pubkey#4581)
Browse files Browse the repository at this point in the history
* fix: lost RxDocument in preSave from v14.0.0

https://rxdb.info/middleware.html

* fix: add tests for presave

Signed-off-by: scriptman <scriptmanxx@gmail.com>

---------

Signed-off-by: scriptman <scriptmanxx@gmail.com>
  • Loading branch information
scriptsman authored Mar 30, 2023
1 parent a8776d7 commit 1368d8f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/rx-document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,5 @@ export function beforeDocumentUpdateWrite<RxDocType>(
if (overwritable.isDevMode()) {
collection.schema.validateChange(oldData, newData);
}
return collection._runHooks('pre', 'save', newData);
return collection._runHooks('pre', 'save', newData, oldData);
}
11 changes: 9 additions & 2 deletions test/unit/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,18 @@ config.parallel('hooks.test.js', () => {
await c.insert(human);
const doc = await c.findOne(human.passportId).exec(true);
let count = 0;
c.preSave(function (data) {
c.preSave(function (data, oldData) {
assert.ok(data);
assert.ok(oldData);
if (count === 1) {
assert.equal(oldData.firstName, 'foobar');
}
count++;
}, false);
await doc.incrementalPatch({ firstName: 'foobar' });
assert.strictEqual(count, 1);
await c.upsert(human);
assert.strictEqual(count, 2);
c.database.destroy();
});
it('parallel', async () => {
Expand All @@ -203,8 +209,9 @@ config.parallel('hooks.test.js', () => {
await c.insert(human);
const doc = await c.findOne(human.passportId).exec(true);
let count = 0;
c.preSave(function (data) {
c.preSave(function (data, oldData) {
assert.ok(data);
assert.ok(oldData);
count++;
}, true);
await doc.incrementalPatch({ firstName: 'foobar' });
Expand Down

0 comments on commit 1368d8f

Please sign in to comment.