diff --git a/README.md b/README.md index 2acd756..a2f946e 100644 --- a/README.md +++ b/README.md @@ -157,7 +157,8 @@ Note that all this eager related options are optional. #### Query Operators -- **`$modify`** - modifiers allow you to easily reuse snippets of query logic. See +- **`$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. - **`$eager`** - eager load relations defined in models' `relationMappings` getter methods. See diff --git a/src/index.js b/src/index.js index be24723..ae2da84 100644 --- a/src/index.js +++ b/src/index.js @@ -305,7 +305,11 @@ class Service extends AdapterService { } if (query && query.$modify) { - q.modify(...query.$modify); + if (typeof query.$modify === 'string') { + if (query.$modify[0] === '[' && query.$modify[query.$modify.length - 1] === ']') { q.modify(...JSON.parse(query.$modify)); } else { q.modify(query.$modify.split(',')); } + } else { + q.modify(...query.$modify); + } delete query.$modify; } diff --git a/test/index.test.js b/test/index.test.js index 743ada9..9636566 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1690,6 +1690,27 @@ describe('Feathers Objection Service', () => { }); }); + it('allow $modify query as string', () => { + return companies.find({ query: { $modify: 'google' } }).then(data => { + expect(data.length).to.be.equal(1); + expect(data[0].name).to.be.equal('Google'); + }); + }); + + it('allow $modify query as string with multiple modifiers', () => { + return companies.find({ query: { $modify: 'apple,large' } }).then(data => { + expect(data.length).to.be.equal(1); + expect(data[0].name).to.be.equal('Apple'); + }); + }); + + it('allow $modify query as string 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 args', () => { return companies.find({ query: { $modify: ['large', false] } }).then(data => { expect(data.length).to.be.equal(1);