Skip to content

Commit

Permalink
Added $mergeEager to merge an eager expression to $eager
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Dec 6, 2019
1 parent b79ac49 commit 946bc83
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ Note that all this eager related options are optional.
`JoinEagerAlgorithm`. See
[`$joinEager`](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#joineager)
documentation.
- **`$modifyEager`** - filter relation based on a relation's field.
- **`$modifyEager`** - filter relation based on a relation's field,
e.g. `companies.find({ query: { $eager: 'employees', $modifyEager: { employees: { name: 'John' } } } })`
- **`$mergeEager`** - merge an eager expression to `$eager`,
e.g. `ccompanies.find({ query: { $eager: 'employees', $mergeEager: 'ceos' } })`
- **`$pick`** - pick properties from result models. See
[`pick`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#pick) documentation.

Expand Down
23 changes: 15 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-objection",
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
"version": "4.6.1",
"version": "4.6.2",
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
"keywords": [
"feathers",
Expand Down Expand Up @@ -49,7 +49,7 @@
"compile": "rimraf lib/ && babel -d lib/ src/",
"watch": "babel --watch -d lib/ src/",
"lint": "semistandard --fix src/**/*.js test/**/*.js --config",
"dtslint": "dtslint types",
"dtslint": "dtslint --localTs node_modules/typescript/lib types",
"mocha": "nyc mocha --opts mocha.opts",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "npm run compile && npm run lint && npm run dtslint && npm run mocha",
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class Service extends AdapterService {
if (params.$joinEager) { delete params.$joinEager; }
if (params.$joinRelation) { delete params.$joinRelation; }
if (params.$modifyEager) { delete params.$modifyEager; }
if (params.$mergeEager) { delete params.$mergeEager; }
if (params.$pick) { delete params.$pick; }
if (params.$noSelect) { delete params.$noSelect; }

Expand Down Expand Up @@ -288,6 +289,12 @@ class Service extends AdapterService {
delete query.$joinRelation;
}

if (query && query.$mergeEager) {
q.mergeEager(query.$mergeEager);

delete query.$mergeEager;
}

// apply eager filters if specified
if (this.eagerFilters) {
const eagerFilters = Array.isArray(this.eagerFilters) ? this.eagerFilters : [this.eagerFilters];
Expand Down
9 changes: 8 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const app = feathers()
model: Company,
id: 'id',
multi: ['create', 'remove', 'patch'],
whitelist: ['$eager', '$modifyEager', '$pick', '$between', '$notBetween', '$containsKey', '$contains', '$contained', '$any', '$all', '$noSelect', '$like'],
whitelist: ['$eager', '$modifyEager', '$mergeEager', '$pick', '$between', '$notBetween', '$containsKey', '$contains', '$contained', '$any', '$all', '$noSelect', '$like'],
allowedEager: '[ceos, clients]',
namedEagerFilters: {
notSnoop (builder) {
Expand Down Expand Up @@ -779,6 +779,13 @@ describe('Feathers Objection Service', () => {
});
});

it('allows mergeEager queries', () => {
return companies.find({ query: { $eager: 'employees', $mergeEager: 'ceos' }, mergeAllowEager: '[employees, ceos]' }).then(data => {
expect(data[0].employees).to.be.ok;
expect(data[0].ceos).to.be.ok;
});
});

it('allows modifyEager queries', () => {
return companies.find({ query: { $eager: 'employees', $modifyEager: { employees: { name: 'John' } } }, mergeAllowEager: 'employees' }).then(data => {
expect(data[0].employees.length).to.equal(2);
Expand Down

0 comments on commit 946bc83

Please sign in to comment.