Skip to content

Commit

Permalink
feat: add support of array of tuples order format (#973). Thanks to @…
Browse files Browse the repository at this point in the history
  • Loading branch information
RusovDmitriy authored Aug 10, 2020
1 parent 01759b3 commit 0950b94
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
16 changes: 16 additions & 0 deletions docs/Cube.js-Backend/Query-Format.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ If the `order` property is not specified in the query, Cube.js sorts results by
- The first measure, descending. If no measure exists...
- The first dimension, ascending.

### Аlternative order format

Also you can control the ordering of the `order` specification, Cube.js support alternative order format - array of tuples:

```js
{
...,
order: [
['Stories.time', 'asc'],
['Stories.count', 'asc']
]
},
...
}
```

## Filters Format

A filter is a Javascript object with the following properties:
Expand Down
25 changes: 19 additions & 6 deletions packages/cubejs-api-gateway/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ const querySchema = Joi.object().keys({
Joi.string()
]
})),
order: Joi.object().pattern(id, Joi.valid('asc', 'desc')),
order: Joi.alternatives(
Joi.object().pattern(id, Joi.valid('asc', 'desc')),
Joi.array().items(Joi.array().min(2).ordered(id, Joi.valid('asc', 'desc')))
),
segments: Joi.array().items(id),
timezone: Joi.string(),
limit: Joi.number().integer().min(1).max(50000),
Expand All @@ -155,6 +158,20 @@ const querySchema = Joi.object().keys({
ungrouped: Joi.boolean()
});

const normalizeQueryOrder = order => {
let result = [];
const normalizeOrderItem = (k, direction) => ({
id: k,
desc: direction === 'desc'
});
if (order) {
result = Array.isArray(order) ?
order.map(([k, direction]) => normalizeOrderItem(k, direction)) :
Object.keys(order).map(k => normalizeOrderItem(k, order[k]));
}
return result;
};

const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;

const normalizeQuery = (query) => {
Expand Down Expand Up @@ -209,15 +226,11 @@ const normalizeQuery = (query) => {
granularity: d.split('.')[2]
}));
const timezone = query.timezone || 'UTC';
const order = query.order && Object.keys(query.order).map(k => ({
id: k,
desc: query.order[k] === 'desc'
}));
return {
...query,
rowLimit: query.rowLimit || query.limit,
timezone,
order,
order: normalizeQueryOrder(query.order),
filters: (query.filters || []).map(f => (
{
...f,
Expand Down
31 changes: 31 additions & 0 deletions packages/cubejs-api-gateway/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,35 @@ describe(`API Gateway`, () => {
"2020-01-01T23:59:59.999"
]);
});

test(`order support object format`, async () => {
const query = {
measures: ["Foo.bar"],
order: {
'Foo.bar': 'asc'
},
};
const res = await request(app)
.get(`/cubejs-api/v1/load?query=${JSON.stringify(query)}`)
.set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M')
.expect(200);

expect(res.body.query.order).toStrictEqual([{ id: 'Foo.bar', desc: false }]);
});

test(`order support array of tuples`, async () => {
const query = {
measures: ["Foo.bar"],
order: [
['Foo.bar', 'asc'],
['Foo.foo', 'desc']
],
};
const res = await request(app)
.get(`/cubejs-api/v1/load?query=${JSON.stringify(query)}`)
.set('Authorization', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M')
.expect(200);

expect(res.body.query.order).toStrictEqual([{ id: 'Foo.bar', desc: false }, { id: 'Foo.foo', desc: true }]);
});
});

0 comments on commit 0950b94

Please sign in to comment.