Skip to content

Commit 7d8e843

Browse files
committed
refactor transformValue and related
1 parent a23988a commit 7d8e843

File tree

5 files changed

+49
-79
lines changed

5 files changed

+49
-79
lines changed

packages/cubejs-api-gateway/src/helpers/transformData.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,13 @@
66
*/
77

88
import R from 'ramda';
9+
import moment, { MomentInput } from 'moment';
910
import { UserError } from '../UserError';
1011
import { ConfigItem } from './prepareAnnotation';
11-
import {
12-
DBResponsePrimitive,
13-
DBResponseValue,
14-
transformValue,
15-
} from './transformValue';
16-
import {
17-
NormalizedQuery,
18-
QueryTimeDimension
19-
} from '../types/query';
20-
import {
21-
ResultType,
22-
QueryType,
23-
} from '../types/strings';
24-
import {
25-
ResultType as ResultTypeEnum,
26-
QueryType as QueryTypeEnum,
27-
} from '../types/enums';
12+
import { NormalizedQuery, QueryTimeDimension } from '../types/query';
13+
import { QueryType, ResultType, } from '../types/strings';
14+
import { QueryType as QueryTypeEnum, ResultType as ResultTypeEnum, } from '../types/enums';
15+
import { DBResponsePrimitive, DBResponseValue } from '../types/responses';
2816

2917
const COMPARE_DATE_RANGE_FIELD = 'compareDateRange';
3018
const COMPARE_DATE_RANGE_SEPARATOR = ' - ';
@@ -175,6 +163,25 @@ function getMembers(
175163
return members;
176164
}
177165

166+
/**
167+
* Transform specified `value` with specified `type` to the network
168+
* protocol type.
169+
*/
170+
function transformValue(
171+
value: DBResponseValue,
172+
type: string
173+
): DBResponsePrimitive {
174+
// TODO: support for max time
175+
if (value && (type === 'time' || value instanceof Date)) {
176+
return (
177+
value instanceof Date
178+
? moment(value)
179+
: moment.utc(value as MomentInput)
180+
).format(moment.HTML5_FMT.DATETIME_LOCAL_MS);
181+
}
182+
return value as DBResponsePrimitive;
183+
}
184+
178185
/**
179186
* Convert DB response object to the compact output format.
180187
* @internal
@@ -370,4 +377,5 @@ export {
370377
getCompactRow,
371378
getVanilaRow,
372379
transformData,
380+
transformValue,
373381
};

packages/cubejs-api-gateway/src/helpers/transformValue.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Query 'or'-filters type definition.
3+
*/
4+
export type DBResponsePrimitive =
5+
null |
6+
boolean |
7+
number |
8+
string;
9+
10+
export type DBResponseValue =
11+
Date |
12+
DBResponsePrimitive |
13+
{ value: DBResponsePrimitive };
14+
15+
export type TransformDataResponse = {
16+
members: string[],
17+
dataset: DBResponsePrimitive[][]
18+
} | {
19+
[member: string]: DBResponsePrimitive
20+
}[];

packages/cubejs-api-gateway/test/helpers/transformData.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import {
1616
QueryType as QueryTypeEnum,
1717
ResultType as ResultTypeEnum,
1818
} from '../../src/types/enums';
19-
import {
20-
DBResponseValue,
21-
} from '../../src/helpers/transformValue';
2219
import {
2320
ConfigItem,
2421
} from '../../src/helpers/prepareAnnotation';
@@ -41,6 +38,7 @@ import {
4138
import {
4239
QueryType,
4340
} from '../../src/types/strings';
41+
import { DBResponseValue } from '../../src/types/responses';
4442

4543
const mockData = {
4644
regular_discount_by_city: {
@@ -792,7 +790,7 @@ describe('transformData helpers', () => {
792790
.timeDimensions
793791
)
794792
) as QueryTimeDimension[];
795-
793+
796794
expect(() => {
797795
getBlendingResponseKey();
798796
}).toThrow(

packages/cubejs-api-gateway/test/helpers/transformValue.test.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
*/
66

77
/* globals describe,test,expect */
8-
/* eslint-disable import/no-duplicates */
9-
/* eslint-disable @typescript-eslint/no-duplicate-imports */
10-
118
import moment from 'moment';
12-
import transformValueDef from '../../src/helpers/transformValue';
13-
import { transformValue } from '../../src/helpers/transformValue';
9+
10+
import { transformValue } from '../../src/helpers/transformData';
1411

1512
describe('transformValue helper', () => {
16-
test('export looks as expected', () => {
17-
expect(transformValueDef).toBeDefined();
18-
expect(transformValue).toBeDefined();
19-
expect(transformValue).toEqual(transformValueDef);
20-
});
2113
test('object with the time value', () => {
2214
const date = Date();
2315
expect(transformValue(date, 'time')).toBe(

0 commit comments

Comments
 (0)