Skip to content

Commit

Permalink
fix(rx-query) findByIds not working with modify and patch (#6592)
Browse files Browse the repository at this point in the history
## Content
* Unit tests that shows the bug
* Fix for the bug
## Description
When you chain "findByIds" with "patch" or "modify" it throws an error like "there is not method named modify on doc". The issue is caused by "findByIds" returning a result in the form of a map witch is not handled in "runQueryUpdateFunction"
  • Loading branch information
HRM authored Nov 17, 2024
1 parent 7dce3d9 commit cf5ce9a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/rx-query-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ export async function runQueryUpdateFunction<RxDocType, RxQueryResult>(
return Promise.all(
docs.map(doc => fn(doc))
) as any;
}else if(docs instanceof Map){
return Promise.all(
[...docs.values()].map((doc) => fn(doc))
) as any;
} else {
// via findOne()
const result = await fn(docs as any);
Expand Down
29 changes: 29 additions & 0 deletions test/unit/rx-collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,35 @@ describe('rx-collection.test.ts', () => {
assert.strictEqual(res.size, 5);
c.database.destroy();
});
it('we should be able to modify the documents', async () => {
const c = await humansCollection.create(5);
const docs = await c.find().exec();
const ids = docs.map((d) => d.primary);

await c.findByIds(ids).modify((doc) => {
doc.firstName = 'abcdefghi';
return doc;
});

const res = await c
.count({ selector: { firstName: 'abcdefghi' } })
.exec();
assert.strictEqual(res, 5);
c.database.destroy();
});
it('we should be able to patch the documents', async () => {
const c = await humansCollection.create(5);
const docs = await c.find().exec();
const ids = docs.map((d) => d.primary);

await c.findByIds(ids).patch({ firstName: 'abcdefghi' });

const res = await c
.count({ selector: { firstName: 'abcdefghi' } })
.exec();
assert.strictEqual(res, 5);
c.database.destroy();
});
/**
* @link https://github.com/pubkey/rxdb/issues/6148
*/
Expand Down

0 comments on commit cf5ce9a

Please sign in to comment.