Skip to content

Commit

Permalink
$modify now supports passing different args to each modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Oct 19, 2020
1 parent 0f4a367 commit 7f2e070
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ Note that all this eager related options are optional.
#### Query Operators

- **`$modify`** - modifiers allow you to easily reuse snippets of query logic. you can pass arguments and use
multiple modifiers. value can be array, a serialized JSON array or a string with modifiers separated by `,`. See
[`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.
multiple modifiers. value can be one of the following:
- String with comma-separated modifier names. e.g. `modifier1,modifier2`
- Array or serialized array with modifier name or array of modifier names as the first item. The rest of the array items would be the modifier/s arguments.
e.g. `['modifier1', arg1, arg2]` or `[['modifier1', 'modifier2'], arg1, arg2]`
- Object or serialized object with modifiers as keys and their arguments as values. Set modifier's value to `true` when it has no arguments.
e.g. `{ modifier1: [arg1, arg2], modifier2: [arg3, arg4], modifier3: true }`

See [`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.

- **`$eager`** - eager load relations defined in models' `relationMappings` getter methods. See
[`withGraphFetched`](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#withgraphfetched) documentation.
Expand Down
20 changes: 18 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,26 @@ class Service extends AdapterService {
}

modifyQuery (query, modify) {
let modifiers = null;

if (typeof modify === 'string') {
if (modify[0] === '[' && modify[modify.length - 1] === ']') { query.modify(...JSON.parse(modify)); } else { query.modify(modify.split(',')); }
} else {
if (modify[0] === '[' && modify[modify.length - 1] === ']') {
query.modify(...JSON.parse(modify));
} else if (modify[0] === '{' && modify[modify.length - 1] === '}') {
modifiers = JSON.parse(modify);
} else {
query.modify(modify.split(','));
}
} else if (Array.isArray(modify)) {
query.modify(...modify);
} else {
modifiers = modify;
}

if (modifiers) {
for (const [modifier, args] of Object.entries(modifiers)) {
if (args === true) { query.modify(modifier); } else { query.modify(modifier, ...args); }
}
}
}

Expand Down
28 changes: 27 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2080,13 +2080,22 @@ describe('Feathers Objection Service', () => {
});
});

it('allow $modify query as string array', () => {
it('allow $modify query with serialized array', () => {
return companies.find({ query: { $modify: JSON.stringify(['google']) } }).then(data => {
expect(data.length).to.be.equal(1);
expect(data[0].name).to.be.equal('Google');
});
});

it('allow $modify query with serialized object', () => {
return companies.find({
query: { $modify: JSON.stringify({ apple: true }) }
}).then(data => {
expect(data.length).to.be.equal(1);
expect(data[0].name).to.be.equal('Apple');
});
});

it('allow $modify query with args', () => {
return companies.find({ query: { $modify: ['large', false] } }).then(data => {
expect(data.length).to.be.equal(1);
Expand All @@ -2106,6 +2115,23 @@ describe('Feathers Objection Service', () => {
expect(data.length).to.be.equal(0);
});
});

it('allow $modify query with multiple modifiers and different args for each', () => {
return companies.find({
query: {
$modify: {
apple: [false],
large: [false],
withRelation: true
}
}
}).then(data => {
expect(data.length).to.be.equal(1);
expect(data[0].name).to.be.equal('Apple');
expect(data[0].employees.length).to.be.equal(1);
expect(data[0].employees[0].name).to.be.equal('Dave');
});
});
});

describe('Create with ID', () => {
Expand Down

0 comments on commit 7f2e070

Please sign in to comment.