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

Commit 1b99eea

Browse files
author
Dekel Barzilay
committed
Added $modify query operator
1 parent ace6ee0 commit 1b99eea

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ Note that all this eager related options are optional.
157157

158158
#### Query Operators
159159

160-
- **`$modify`** - modifiers allow you to easily reuse snippets of query logic. See
160+
- **`$modify`** - modifiers allow you to easily reuse snippets of query logic. you can pass arguments and use
161+
multiple modifiers. value can be array, a serialized JSON array or a string with modifiers separated by `,`. See
161162
[`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.
162163

163164
- **`$eager`** - eager load relations defined in models' `relationMappings` getter methods. See

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ class Service extends AdapterService {
305305
}
306306

307307
if (query && query.$modify) {
308-
q.modify(...query.$modify);
308+
if (typeof query.$modify === 'string') {
309+
if (query.$modify[0] === '[' && query.$modify[query.$modify.length - 1] === ']') { q.modify(...JSON.parse(query.$modify)); } else { q.modify(query.$modify.split(',')); }
310+
} else {
311+
q.modify(...query.$modify);
312+
}
309313

310314
delete query.$modify;
311315
}

test/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,27 @@ describe('Feathers Objection Service', () => {
16901690
});
16911691
});
16921692

1693+
it('allow $modify query as string', () => {
1694+
return companies.find({ query: { $modify: 'google' } }).then(data => {
1695+
expect(data.length).to.be.equal(1);
1696+
expect(data[0].name).to.be.equal('Google');
1697+
});
1698+
});
1699+
1700+
it('allow $modify query as string with multiple modifiers', () => {
1701+
return companies.find({ query: { $modify: 'apple,large' } }).then(data => {
1702+
expect(data.length).to.be.equal(1);
1703+
expect(data[0].name).to.be.equal('Apple');
1704+
});
1705+
});
1706+
1707+
it('allow $modify query as string array', () => {
1708+
return companies.find({ query: { $modify: JSON.stringify(['google']) } }).then(data => {
1709+
expect(data.length).to.be.equal(1);
1710+
expect(data[0].name).to.be.equal('Google');
1711+
});
1712+
});
1713+
16931714
it('allow $modify query with args', () => {
16941715
return companies.find({ query: { $modify: ['large', false] } }).then(data => {
16951716
expect(data.length).to.be.equal(1);

0 commit comments

Comments
 (0)