Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 8753332

Browse files
author
Dekel Barzilay
committed
Fixed count for queries with $modify query operator
1 parent 68475b4 commit 8753332

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ class Service extends AdapterService {
266266
return optionRelations.merge(paramRelations);
267267
}
268268

269+
modifyQuery (query, modify) {
270+
if (typeof modify === 'string') {
271+
if (modify[0] === '[' && modify[modify.length - 1] === ']') { query.modify(...JSON.parse(modify)); } else { query.modify(modify.split(',')); }
272+
} else {
273+
query.modify(...modify);
274+
}
275+
}
276+
269277
_createQuery (params = {}) {
270278
const trx = params.transaction ? params.transaction.trx : null;
271279
return this.Model.query(trx);
@@ -322,11 +330,7 @@ class Service extends AdapterService {
322330
}
323331

324332
if (query && query.$modify) {
325-
if (typeof query.$modify === 'string') {
326-
if (query.$modify[0] === '[' && query.$modify[query.$modify.length - 1] === ']') { q.modify(...JSON.parse(query.$modify)); } else { q.modify(query.$modify.split(',')); }
327-
} else {
328-
q.modify(...query.$modify);
329-
}
333+
this.modifyQuery(q, query.$modify);
330334

331335
delete query.$modify;
332336
}
@@ -422,6 +426,10 @@ class Service extends AdapterService {
422426
countQuery.count({ total: idColumns });
423427
}
424428

429+
if (query && query.$modify) {
430+
this.modifyQuery(countQuery, query.$modify);
431+
}
432+
425433
this.objectify(countQuery, query, null, null, query.$allowRefs);
426434

427435
return countQuery

test/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,10 @@ describe('Feathers Objection Service', () => {
17161716
]);
17171717
});
17181718

1719+
afterEach(async () => {
1720+
companies.options.paginate = {};
1721+
});
1722+
17191723
after(async () => {
17201724
await companies.remove(null);
17211725
});
@@ -1727,6 +1731,19 @@ describe('Feathers Objection Service', () => {
17271731
});
17281732
});
17291733

1734+
it('allow $modify query with paginate', () => {
1735+
companies.options.paginate = {
1736+
default: 1,
1737+
max: 2
1738+
};
1739+
1740+
return companies.find({ query: { $modify: ['google'] } }).then(data => {
1741+
expect(data.total).to.be.equal(1);
1742+
expect(data.data.length).to.be.equal(1);
1743+
expect(data.data[0].name).to.be.equal('Google');
1744+
});
1745+
});
1746+
17301747
it('allow $modify query as string', () => {
17311748
return companies.find({ query: { $modify: 'google' } }).then(data => {
17321749
expect(data.length).to.be.equal(1);

0 commit comments

Comments
 (0)