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

Commit ace6ee0

Browse files
author
Dekel Barzilay
committed
- Added $modify query operator
- Bumped version to 5.2.0
1 parent d38a63d commit ace6ee0

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Note that all this eager related options are optional.
144144
[`allowGraph`](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#allowgraph)
145145
documentation.
146146

147-
- **`eagerOptions`** - Options object to use with `$eager` and `$joinEager` query operators.
147+
- **`eagerOptions`** - options object to use with `$eager` and `$joinEager` query operators.
148148
See [`GraphOptions`](https://vincit.github.io/objection.js/api/types/#type-graphoptions)
149149
documentation.
150150

@@ -157,6 +157,9 @@ 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
161+
[`modify`](https://vincit.github.io/objection.js/api/query-builder/other-methods.html#modify) documentation.
162+
160163
- **`$eager`** - eager load relations defined in models' `relationMappings` getter methods. See
161164
[`withGraphFetched`](https://vincit.github.io/objection.js/api/query-builder/eager-methods.html#withgraphfetched) documentation.
162165

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-objection",
33
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
4-
"version": "5.1.1",
4+
"version": "5.2.0",
55
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
66
"keywords": [
77
"feathers",

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ class Service extends AdapterService {
162162
if (params.$modifyEager) { delete params.$modifyEager; }
163163
if (params.$mergeEager) { delete params.$mergeEager; }
164164
if (params.$noSelect) { delete params.$noSelect; }
165+
if (params.$modify) { delete params.$modify; }
165166

166167
Object.keys(params || {}).forEach(key => {
167168
let value = params[key];
@@ -303,6 +304,12 @@ class Service extends AdapterService {
303304
delete query.$mergeEager;
304305
}
305306

307+
if (query && query.$modify) {
308+
q.modify(...query.$modify);
309+
310+
delete query.$modify;
311+
}
312+
306313
// apply eager filters if specified
307314
if (this.eagerFilters) {
308315
const eagerFilters = Array.isArray(this.eagerFilters) ? this.eagerFilters : [this.eagerFilters];

test/company.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class Company extends Model {
1212
id: { type: 'integer' },
1313
name: { type: 'string' },
1414
ceo: { type: ['integer', 'null'] },
15+
size: { type: ['string', 'null'], enum: ['small', 'medium', 'large', null] },
1516
jsonObject: {
1617
type: ['object', 'null'],
1718
properties: {
@@ -32,6 +33,24 @@ export default class Company extends Model {
3233
}
3334
}
3435

36+
static modifiers = {
37+
google: (builder, hasCeo) => {
38+
builder.where('name', 'Google');
39+
40+
if (hasCeo) { builder.whereNot('ceo', null); }
41+
},
42+
apple: (builder, hasCeo) => {
43+
builder.where('name', 'Apple');
44+
45+
if (hasCeo) { builder.whereNot('ceo', null); }
46+
},
47+
large: (builder, hasCeo) => {
48+
builder.where('size', 'large');
49+
50+
if (hasCeo) { builder.whereNot('ceo', null); }
51+
}
52+
}
53+
3554
// This object defines the relations to other models.
3655
static relationMappings = {
3756
ceos: {

test/index.test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const app = feathers()
134134
model: Company,
135135
id: 'id',
136136
multi: ['create', 'remove', 'patch'],
137-
whitelist: ['$eager', '$joinRelation', '$modifyEager', '$mergeEager', '$between', '$notBetween', '$containsKey', '$contains', '$contained', '$any', '$all', '$noSelect', '$like', '$null'],
137+
whitelist: ['$eager', '$joinRelation', '$modifyEager', '$mergeEager', '$between', '$notBetween', '$containsKey', '$contains', '$contained', '$any', '$all', '$noSelect', '$like', '$null', '$modify'],
138138
allowedEager: '[ceos, clients, employees]',
139139
eagerFilters: [
140140
{
@@ -233,6 +233,7 @@ function clean (done) {
233233
table.increments('id');
234234
table.string('name');
235235
table.integer('ceo');
236+
table.enum('size', ['small', 'medium', 'large']);
236237
table.json('jsonObject');
237238
table.json('jsonArray');
238239
table.jsonb('jsonbObject');
@@ -1661,4 +1662,52 @@ describe('Feathers Objection Service', () => {
16611662
});
16621663
});
16631664
});
1665+
1666+
describe('$modify', () => {
1667+
before(async () => {
1668+
await companies
1669+
.create([
1670+
{
1671+
name: 'Google',
1672+
ceo: 1
1673+
},
1674+
{
1675+
name: 'Apple',
1676+
ceo: null,
1677+
size: 'large'
1678+
}
1679+
]);
1680+
});
1681+
1682+
after(async () => {
1683+
await companies.remove(null);
1684+
});
1685+
1686+
it('allow $modify query', () => {
1687+
return companies.find({ query: { $modify: ['google'] } }).then(data => {
1688+
expect(data.length).to.be.equal(1);
1689+
expect(data[0].name).to.be.equal('Google');
1690+
});
1691+
});
1692+
1693+
it('allow $modify query with args', () => {
1694+
return companies.find({ query: { $modify: ['large', false] } }).then(data => {
1695+
expect(data.length).to.be.equal(1);
1696+
expect(data[0].name).to.be.equal('Apple');
1697+
});
1698+
});
1699+
1700+
it('allow $modify query 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 with multiple modifiers and args', () => {
1708+
return companies.find({ query: { $modify: [['apple', 'large'], true] } }).then(data => {
1709+
expect(data.length).to.be.equal(0);
1710+
});
1711+
});
1712+
});
16641713
});

0 commit comments

Comments
 (0)