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

Commit dd854e1

Browse files
author
Dekel Barzilay
committed
Added support for ref in $select & $sort
1 parent 9e49727 commit dd854e1

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

src/index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,16 @@ class Service extends AdapterService {
274274

275275
// $select uses a specific find syntax, so it has to come first.
276276
if (filters.$select) {
277-
q.select(...filters.$select.concat(`${this.Model.tableName}.${this.id}`));
277+
const items = filters.$select.concat(`${this.Model.tableName}.${this.id}`);
278+
279+
for (const [key, item] of Object.entries(items)) {
280+
const matches = item.match(/^ref\((.+)\)( as (.+))?$/);
281+
if (matches) {
282+
items[key] = ref(matches[1]).as(matches[3] || matches[1]);
283+
}
284+
}
285+
286+
q.select(...items);
278287
}
279288

280289
// $eager for Objection eager queries
@@ -339,8 +348,11 @@ class Service extends AdapterService {
339348
this.objectify(q, query);
340349

341350
if (filters.$sort) {
342-
Object.keys(filters.$sort).forEach(key => {
343-
q.orderBy(key, filters.$sort[key] === 1 ? 'asc' : 'desc');
351+
Object.keys(filters.$sort).forEach(item => {
352+
const matches = item.match(/^ref\((.+)\)$/);
353+
const key = matches ? ref(matches[1]) : item;
354+
355+
q.orderBy(key, filters.$sort[item] === 1 ? 'asc' : 'desc');
344356
});
345357
}
346358

test/index.test.js

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ describe('Feathers Objection Service', () => {
13411341
.create([
13421342
{
13431343
name: 'Google',
1344-
jsonObject: {
1344+
jsonbObject: {
13451345
stringField: 'string',
13461346
numberField: 1.5,
13471347
objectField: {
@@ -1356,6 +1356,24 @@ describe('Feathers Objection Service', () => {
13561356
}
13571357
}
13581358
]
1359+
},
1360+
{
1361+
name: 'Apple',
1362+
jsonbObject: {
1363+
stringField: 'string2',
1364+
numberField: 1,
1365+
objectField: {
1366+
object: 'string in jsonObject.objectField.object2'
1367+
},
1368+
'first.founder': 'Dave'
1369+
},
1370+
jsonArray: [
1371+
{
1372+
objectField: {
1373+
object: 'I\'m string in jsonArray[0].objectField.object2'
1374+
}
1375+
}
1376+
]
13591377
}
13601378
]);
13611379
});
@@ -1365,43 +1383,43 @@ describe('Feathers Objection Service', () => {
13651383
});
13661384

13671385
it('object', () => {
1368-
return companies.find({ query: { jsonObject: { $ne: null } } }).then(data => {
1369-
expect(data[0].jsonObject.stringField).to.equal('string');
1386+
return companies.find({ query: { jsonbObject: { $ne: null } } }).then(data => {
1387+
expect(data[0].jsonbObject.stringField).to.equal('string');
13701388
});
13711389
});
13721390

13731391
it('object stringField', () => {
1374-
return companies.find({ query: { jsonObject: { stringField: 'string' } } }).then(data => {
1375-
expect(data[0].jsonObject.stringField).to.equal('string');
1392+
return companies.find({ query: { jsonbObject: { stringField: 'string' } } }).then(data => {
1393+
expect(data[0].jsonbObject.stringField).to.equal('string');
13761394
});
13771395
});
13781396

13791397
it('object stringField $like', () => {
1380-
return companies.find({ query: { jsonObject: { stringField: { $like: 'str%' } } } }).then(data => {
1381-
expect(data[0].jsonObject.stringField).to.equal('string');
1398+
return companies.find({ query: { jsonbObject: { stringField: { $like: 'str%' } } } }).then(data => {
1399+
expect(data[0].jsonbObject.stringField).to.equal('string');
13821400
});
13831401
});
13841402

13851403
it('object numberField $between', () => {
1386-
return companies.find({ query: { jsonObject: { numberField: { $between: [1, 2] } } } }).then(data => {
1387-
expect(data[0].jsonObject.stringField).to.equal('string');
1404+
return companies.find({ query: { jsonbObject: { numberField: { $between: [1, 2] } } } }).then(data => {
1405+
expect(data[0].jsonbObject.stringField).to.equal('string');
13881406
});
13891407
});
13901408

13911409
it('object numberField', () => {
1392-
return companies.find({ query: { jsonObject: { numberField: 1.5 } } }).then(data => {
1393-
expect(data[0].jsonObject.numberField).to.equal(1.5);
1410+
return companies.find({ query: { jsonbObject: { numberField: 1.5 } } }).then(data => {
1411+
expect(data[0].jsonbObject.numberField).to.equal(1.5);
13941412
});
13951413
});
13961414

13971415
it('object numberField $gt', () => {
1398-
return companies.find({ query: { jsonObject: { numberField: { $gt: 1.4 } } } }).then(data => {
1416+
return companies.find({ query: { jsonbObject: { numberField: { $gt: 1.4 } } } }).then(data => {
13991417
expect(data[0].jsonArray[0].objectField.object).to.equal('I\'m string in jsonArray[0].objectField.object');
14001418
});
14011419
});
14021420

14031421
it('object nested object', () => {
1404-
return companies.find({ query: { jsonObject: { 'objectField.object': 'string in jsonObject.objectField.object' } } }).then(data => {
1422+
return companies.find({ query: { jsonbObject: { 'objectField.object': 'string in jsonObject.objectField.object' } } }).then(data => {
14051423
expect(data[0].jsonArray[0].objectField.object).to.equal('I\'m string in jsonArray[0].objectField.object');
14061424
});
14071425
});
@@ -1419,14 +1437,28 @@ describe('Feathers Objection Service', () => {
14191437
});
14201438

14211439
it('dot in property name', () => {
1422-
return companies.find({ query: { jsonObject: { '(first.founder)': 'John' } } }).then(data => {
1423-
expect(data[0].jsonObject['first.founder']).to.equal('John');
1440+
return companies.find({ query: { jsonbObject: { '(first.founder)': 'John' } } }).then(data => {
1441+
expect(data[0].jsonbObject['first.founder']).to.equal('John');
14241442
});
14251443
});
14261444

14271445
it('dot in property name with brackets', () => {
1428-
return companies.find({ query: { jsonObject: { '[first.founder]': 'John' } } }).then(data => {
1429-
expect(data[0].jsonObject['first.founder']).to.equal('John');
1446+
return companies.find({ query: { jsonbObject: { '[first.founder]': 'John' } } }).then(data => {
1447+
expect(data[0].jsonbObject['first.founder']).to.equal('John');
1448+
});
1449+
});
1450+
1451+
it('select & sort with ref', () => {
1452+
return companies.find({
1453+
query: {
1454+
$select: ['name', 'ref(jsonbObject:numberField)', 'ref(jsonbObject:objectField.object) as object'],
1455+
$sort: { 'ref(jsonbObject:numberField)': 1 }
1456+
}
1457+
}).then(data => {
1458+
expect(data.length).to.equal(2);
1459+
expect(data[0].name).to.equal('Apple');
1460+
expect(data[0]['jsonbObject:numberField']).to.equal(1);
1461+
expect(data[0].object).to.equal('string in jsonObject.objectField.object2');
14301462
});
14311463
});
14321464
});

0 commit comments

Comments
 (0)