Skip to content

Commit

Permalink
Add optional parameter modifierFiltersResults for disabling count of …
Browse files Browse the repository at this point in the history
…$modify query results (#136)

* Add optional parameter modifierFiltersResults for disabling count of $modify query results

* Improve test case for modifierFiltersResults parameter
  • Loading branch information
alex-all3dp committed Dec 15, 2020
1 parent 85cf888 commit 149e1d0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,9 @@ Note that all this eager related options are optional.
See [`withSchema`](https://vincit.github.io/objection.js/api/query-builder/find-methods.html#withschema)
documentation.

- **`modifierFiltersResults`** - when `false` the `total` count of a `find()` query is calculated from the original result set, ignoring the `count` of any `$modify` query.
The default behaviour is to apply the count of the modifier to the result total, assuming that the modifier may influence the result total by filtering the result set. This can be used to workaround issues with `groupBy` and the result count. See [this issue](https://github.com/feathersjs-ecosystem/feathers-objection/issues/102) for a detailed explanation.

### Composite primary keys

Composite primary keys can be passed as the `id` argument using the following
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ class Service extends AdapterService {
countQuery.count({ total: countColumns });
}

if (query && query.$modify) {
if (query && query.$modify && params.modifierFiltersResults !== false) {
this.modifyQuery(countQuery, query.$modify);
}

Expand Down
3 changes: 3 additions & 0 deletions test/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default class Company extends Model {
},
withRelation: builder => {
builder.withGraphFetched('employees');
},
withRelationAndGroupBy: builder => {
builder.withGraphFetched('employees').groupBy('id');
}
}

Expand Down
43 changes: 43 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,49 @@ describe('Feathers Objection Service', () => {
});
});

it('params.modifierFiltersResults=false does not apply count from modify query', () => {
companies.options.paginate = {
default: 2,
max: 2
};

return companies.find({
query: { $modify: ['withRelationAndGroupBy'] }, modifierFiltersResults: false
}).then(data => {
expect(data.total).to.be.equal(2);
expect(data.data.length).to.be.equal(2);
expect(data.data[0].name).to.be.equal('Google');
});
});

it('params.modifierFiltersResults=true applies count from modify query', () => {
companies.options.paginate = {
default: 2,
max: 2
};

return companies.find({
query: { $modify: ['withRelationAndGroupBy'] }, modifierFiltersResults: true
}).then(data => {
expect(data.total).to.be.equal(1); // count result from GROUP BY
expect(data.data.length).to.be.equal(2);
expect(data.data[0].name).to.be.equal('Google');
});
});

it('params.modifierFiltersResults=undefined applies count from modify query', () => {
companies.options.paginate = {
default: 2,
max: 2
};

return companies.find({ query: { $modify: ['withRelationAndGroupBy'] } }).then(data => {
expect(data.total).to.be.equal(1); // count result from GROUP BY
expect(data.data.length).to.be.equal(2);
expect(data.data[0].name).to.be.equal('Google');
});
});

it('allow $modify query with paginate, groupBy and joinRelation', () => {
companies.options.paginate = {
default: 1,
Expand Down

0 comments on commit 149e1d0

Please sign in to comment.