Skip to content

Commit

Permalink
Added $modify query operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Mar 13, 2020
1 parent ace6ee0 commit 1b99eea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1b99eea

Please sign in to comment.