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

Commit 7f2e070

Browse files
author
Dekel Barzilay
committed
$modify now supports passing different args to each modifier
1 parent 0f4a367 commit 7f2e070

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,14 @@ Note that all this eager related options are optional.
167167
#### Query Operators
168168

169169
- **`$modify`** - modifiers allow you to easily reuse snippets of query logic. you can pass arguments and use
170-
multiple modifiers. value can be array, a serialized JSON array or a string with modifiers separated by `,`. See
171-
[`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.
170+
multiple modifiers. value can be one of the following:
171+
- String with comma-separated modifier names. e.g. `modifier1,modifier2`
172+
- 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.
173+
e.g. `['modifier1', arg1, arg2]` or `[['modifier1', 'modifier2'], arg1, arg2]`
174+
- Object or serialized object with modifiers as keys and their arguments as values. Set modifier's value to `true` when it has no arguments.
175+
e.g. `{ modifier1: [arg1, arg2], modifier2: [arg3, arg4], modifier3: true }`
176+
177+
See [`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.
172178

173179
- **`$eager`** - eager load relations defined in models' `relationMappings` getter methods. See
174180
[`withGraphFetched`](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#withgraphfetched) documentation.

src/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,26 @@ class Service extends AdapterService {
279279
}
280280

281281
modifyQuery (query, modify) {
282+
let modifiers = null;
283+
282284
if (typeof modify === 'string') {
283-
if (modify[0] === '[' && modify[modify.length - 1] === ']') { query.modify(...JSON.parse(modify)); } else { query.modify(modify.split(',')); }
284-
} else {
285+
if (modify[0] === '[' && modify[modify.length - 1] === ']') {
286+
query.modify(...JSON.parse(modify));
287+
} else if (modify[0] === '{' && modify[modify.length - 1] === '}') {
288+
modifiers = JSON.parse(modify);
289+
} else {
290+
query.modify(modify.split(','));
291+
}
292+
} else if (Array.isArray(modify)) {
285293
query.modify(...modify);
294+
} else {
295+
modifiers = modify;
296+
}
297+
298+
if (modifiers) {
299+
for (const [modifier, args] of Object.entries(modifiers)) {
300+
if (args === true) { query.modify(modifier); } else { query.modify(modifier, ...args); }
301+
}
286302
}
287303
}
288304

test/index.test.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2080,13 +2080,22 @@ describe('Feathers Objection Service', () => {
20802080
});
20812081
});
20822082

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

2090+
it('allow $modify query with serialized object', () => {
2091+
return companies.find({
2092+
query: { $modify: JSON.stringify({ apple: true }) }
2093+
}).then(data => {
2094+
expect(data.length).to.be.equal(1);
2095+
expect(data[0].name).to.be.equal('Apple');
2096+
});
2097+
});
2098+
20902099
it('allow $modify query with args', () => {
20912100
return companies.find({ query: { $modify: ['large', false] } }).then(data => {
20922101
expect(data.length).to.be.equal(1);
@@ -2106,6 +2115,23 @@ describe('Feathers Objection Service', () => {
21062115
expect(data.length).to.be.equal(0);
21072116
});
21082117
});
2118+
2119+
it('allow $modify query with multiple modifiers and different args for each', () => {
2120+
return companies.find({
2121+
query: {
2122+
$modify: {
2123+
apple: [false],
2124+
large: [false],
2125+
withRelation: true
2126+
}
2127+
}
2128+
}).then(data => {
2129+
expect(data.length).to.be.equal(1);
2130+
expect(data[0].name).to.be.equal('Apple');
2131+
expect(data[0].employees.length).to.be.equal(1);
2132+
expect(data[0].employees[0].name).to.be.equal('Dave');
2133+
});
2134+
});
21092135
});
21102136

21112137
describe('Create with ID', () => {

0 commit comments

Comments
 (0)