Skip to content

Commit 3f7d74f

Browse files
authored
feat: Including format and type in tableColumns (#587) Thanks to @danpanaite!
* Including format and type in tableColumns * Updating docs * Updating JSDoc Fixes #585
1 parent ec2750e commit 3f7d74f

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

docs/Cube.js-Frontend/@cubejs-client-core.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ For example
257257

258258
// ResultSet.tableColumns() will return
259259
[
260-
{ key: "Stories.time", title: "Stories Time", shortTitle: "Time" },
261-
{ key: "Stories.count", title: "Stories Count", shortTitle: "Count" },
260+
{ key: "Stories.time", title: "Stories Time", shortTitle: "Time", type: "time", format: undefined },
261+
{ key: "Stories.count", title: "Stories Count", shortTitle: "Count", type: "count", format: undefined },
262262
//...
263263
]
264264
```

packages/cubejs-client-core/src/ResultSet.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ class ResultSet {
361361
*
362362
* // ResultSet.tableColumns() will return
363363
* [
364-
* { key: "Stories.time", title: "Stories Time", shortTitle: "Time" },
365-
* { key: "Stories.count", title: "Stories Count", shortTitle: "Count" },
364+
* { key: "Stories.time", title: "Stories Time", shortTitle: "Time", type: "time", format: undefined },
365+
* { key: "Stories.count", title: "Stories Count", shortTitle: "Count", type: "count", format: undefined },
366366
* //...
367367
* ]
368368
* ```
@@ -377,6 +377,8 @@ class ResultSet {
377377
key: m,
378378
title: this.loadResponse.annotation.measures[m].title,
379379
shortTitle: this.loadResponse.annotation.measures[m].shortTitle,
380+
format: this.loadResponse.annotation.measures[m].format,
381+
type: this.loadResponse.annotation.measures[m].type,
380382
})) :
381383
[{
382384
key: field,
@@ -387,7 +389,15 @@ class ResultSet {
387389
shortTitle: (
388390
this.loadResponse.annotation.dimensions[field] ||
389391
this.loadResponse.annotation.timeDimensions[field]
390-
).shortTitle
392+
).shortTitle,
393+
format: (
394+
this.loadResponse.annotation.dimensions[field] ||
395+
this.loadResponse.annotation.timeDimensions[field]
396+
).format,
397+
type: (
398+
this.loadResponse.annotation.dimensions[field] ||
399+
this.loadResponse.annotation.timeDimensions[field]
400+
).type
391401
}]
392402
);
393403
return normalizedPivotConfig.x.map(column)

packages/cubejs-client-core/src/ResultSet.test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,4 +597,84 @@ describe('ResultSet', () => {
597597
]);
598598
});
599599
});
600+
601+
describe('tableColumns', () => {
602+
test('returns array of column definitions for tablePivot', () => {
603+
const resultSet = new ResultSet({
604+
"query": {
605+
"measures": ["Orders.count", "Orders.totalAmount"],
606+
"timeDimensions": [{
607+
"dimension": "Orders.createdAt",
608+
"granularity": "day",
609+
"dateRange": ["2020-01-08T00:00:00.000", "2020-01-14T23:59:59.999"]
610+
}],
611+
"dimensions": ["Orders.createdAt"],
612+
"filters": [],
613+
"timezone": "UTC"
614+
},
615+
"data": [],
616+
"annotation": {
617+
"measures": {
618+
"Orders.count": {
619+
"title": "Orders Count",
620+
"shortTitle": "Count",
621+
"type": "count",
622+
},
623+
"Orders.totalAmount": {
624+
"title": "Orders Total Amount",
625+
"shortTitle": "Total Amount",
626+
"type": "number",
627+
"format": "currency"
628+
}
629+
},
630+
"dimensions": {
631+
"Orders.createdAt": {
632+
"title": "Orders Created at",
633+
"shortTitle": "Created at",
634+
"type": "time"
635+
}
636+
},
637+
"segments": {},
638+
"timeDimensions": {
639+
"Orders.createdAt.day": {
640+
"title": "Orders Created at",
641+
"shortTitle": "Created at",
642+
"type": "time"
643+
}
644+
}
645+
}
646+
});
647+
648+
expect(resultSet.tableColumns()).toEqual([
649+
{
650+
"format": undefined,
651+
"key": "Orders.createdAt.day",
652+
"shortTitle": "Created at",
653+
"title": "Orders Created at",
654+
"type": "time",
655+
},
656+
{
657+
"format": undefined,
658+
"key": "Orders.createdAt",
659+
"shortTitle": "Created at",
660+
"title": "Orders Created at",
661+
"type": "time",
662+
},
663+
{
664+
"format": undefined,
665+
"key": "Orders.count",
666+
"shortTitle": "Count",
667+
"title": "Orders Count",
668+
"type": "count",
669+
},
670+
{
671+
"format": "currency",
672+
"key": "Orders.totalAmount",
673+
"shortTitle": "Total Amount",
674+
"title": "Orders Total Amount",
675+
"type": "number",
676+
}
677+
]);
678+
});
679+
});
600680
});

0 commit comments

Comments
 (0)