Skip to content

Commit 2b5a5ee

Browse files
Merge pull request #96 from balena-io-modules/orderby-reverse-navigation-resource
Support ordering by a Collection Navigation property using a Key notation
2 parents 6ff32a4 + 460e4f3 commit 2b5a5ee

2 files changed

Lines changed: 154 additions & 12 deletions

File tree

odata-parser.pegjs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -443,24 +443,38 @@ PropertyPath =
443443
name:ResourceName
444444
{ return { name } }
445445
)
446-
(
446+
( // singleNavigationExpr
447447
'/'
448448
property:PropertyPath
449449
{ result.property = property }
450-
)?
451-
(
452-
'/$count'
453-
optionsObj:(
454-
'('
455-
@( Dollar
456-
option:FilterByOption
457-
{ return CollapseObjectArray([option]) }
450+
/ ( // collectionNavigationExpr
451+
( // collectionPathExpr
452+
'/$count'
453+
optionsObj:(
454+
'('
455+
@( Dollar
456+
option:FilterByOption
457+
{ return CollapseObjectArray([option]) }
458+
)
459+
')'
460+
)?
461+
{ result.count = true; result.options = optionsObj }
458462
)
459-
')'
460-
)?
461-
{ result.count = true; result.options = optionsObj }
463+
/ ( // keyPredicate [ singleNavigationExpr ]
464+
(
465+
key:Key
466+
{ result.key = key }
467+
)
468+
( // singleNavigationExpr
469+
'/'
470+
property:PropertyPath
471+
{ result.property = property }
472+
)?
473+
)
474+
)
462475
)?
463476
{ return result }
477+
464478
ExpandPropertyPathList =
465479
ExpandPropertyPath|1..,','|
466480
ExpandPropertyPath =

test/orderby.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,133 @@ export default (test) => {
134134
nestedFilterTest.only = testFilterOption.bind(null, test.only);
135135

136136
filterby(nestedFilterTest);
137+
138+
test('$orderby=Products(1) asc', [1], function (result) {
139+
it('sort options are present on the result', () => {
140+
assert.notEqual(result.options.$orderby, null);
141+
});
142+
it('sort options have a single property specified', () => {
143+
assert.equal(result.options.$orderby.properties.length, 1);
144+
});
145+
it('sort options property should be as expected', () => {
146+
assert.deepStrictEqual(result.options.$orderby.properties, [
147+
{
148+
name: 'Products',
149+
key: { bind: 0 },
150+
order: 'asc',
151+
},
152+
]);
153+
});
154+
});
155+
156+
test('$orderby=Products(1)/Quantity asc', [1], function (result) {
157+
it('sort options are present on the result', () => {
158+
assert.notEqual(result.options.$orderby, null);
159+
});
160+
it('sort options have a single property specified', () => {
161+
assert.equal(result.options.$orderby.properties.length, 1);
162+
});
163+
it('sort options property should be as expected', () => {
164+
assert.deepStrictEqual(result.options.$orderby.properties, [
165+
{
166+
name: 'Products',
167+
key: { bind: 0 },
168+
property: { name: 'Quantity' },
169+
order: 'asc',
170+
},
171+
]);
172+
});
173+
});
174+
175+
test(
176+
`$orderby=Products(partialkey='a')/Quantity asc`,
177+
['a'],
178+
function (result) {
179+
it('sort options are present on the result', () => {
180+
assert.notEqual(result.options.$orderby, null);
181+
});
182+
it('sort options have a single property specified', () => {
183+
assert.equal(result.options.$orderby.properties.length, 1);
184+
});
185+
it('sort options property should be as expected', () => {
186+
assert.deepStrictEqual(result.options.$orderby.properties, [
187+
{
188+
name: 'Products',
189+
key: {
190+
partialkey: { bind: 0 },
191+
},
192+
property: { name: 'Quantity' },
193+
order: 'asc',
194+
},
195+
]);
196+
});
197+
},
198+
);
199+
200+
test('$orderby=Products(a=1,b=2)/Quantity asc', [1, 2], function (result) {
201+
it('sort options are present on the result', () => {
202+
assert.notEqual(result.options.$orderby, null);
203+
});
204+
it('sort options have a single property specified', () => {
205+
assert.equal(result.options.$orderby.properties.length, 1);
206+
});
207+
it('sort options property should be as expected', () => {
208+
assert.deepStrictEqual(result.options.$orderby.properties, [
209+
{
210+
name: 'Products',
211+
key: {
212+
a: { bind: 0 },
213+
b: { bind: 1 },
214+
},
215+
property: { name: 'Quantity' },
216+
order: 'asc',
217+
},
218+
]);
219+
});
220+
});
221+
222+
test(`$orderby=Products(1)/Orders/$count asc`, [1], function (result) {
223+
it('sort options are present on the result', () => {
224+
assert.notEqual(result.options.$orderby, null);
225+
});
226+
it('sort options have a single property specified', () => {
227+
assert.equal(result.options.$orderby.properties.length, 1);
228+
});
229+
it('sort options property should be as expected', () => {
230+
assert.deepStrictEqual(result.options.$orderby.properties, [
231+
{
232+
name: 'Products',
233+
key: { bind: 0 },
234+
property: { name: 'Orders', count: true, options: null },
235+
order: 'asc',
236+
},
237+
]);
238+
});
239+
});
240+
241+
test(
242+
`$orderby=Products(partialkey='a')/Orders/$count asc`,
243+
['a'],
244+
function (result) {
245+
it('sort options are present on the result', () => {
246+
assert.notEqual(result.options.$orderby, null);
247+
});
248+
it('sort options have a single property specified', () => {
249+
assert.equal(result.options.$orderby.properties.length, 1);
250+
});
251+
it('sort options property should be as expected', () => {
252+
assert.deepStrictEqual(result.options.$orderby.properties, [
253+
{
254+
name: 'Products',
255+
key: {
256+
partialkey: { bind: 0 },
257+
},
258+
property: { name: 'Orders', count: true, options: null },
259+
order: 'asc',
260+
},
261+
]);
262+
});
263+
},
264+
);
137265
});
138266
};

0 commit comments

Comments
 (0)