Skip to content

Commit cf5fdf0

Browse files
authored
hydrate inner values (#49)
1 parent 29a6eb3 commit cf5fdf0

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ const statement = await connection.execute("select 1 union all select 2");
562562

563563
const { data } = await statement.streamResult();
564564

565+
565566
data.pipe(serializedStream).pipe(process.stdout);
566567
```
567568

src/statement/dataTypes.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,31 @@ export const getFireboltType = (type: string): string => {
8787
const match = key.match(COMPLEX_TYPE);
8888
if (match) {
8989
const [_, outerType, innerType] = match;
90+
if (innerType.match(COMPLEX_TYPE)) {
91+
return getFireboltType(innerType);
92+
}
9093
const mappedType = getMappedType(innerType);
9194
return mappedType ? `${outerType}(${mappedType})` : key;
9295
}
9396
const mappedType = getMappedType(key);
9497
return mappedType || key;
9598
};
9699

100+
export const getInnerType = (type: string): string => {
101+
const key = type.toLowerCase();
102+
const match = key.match(COMPLEX_TYPE);
103+
if (match) {
104+
const [_, _outerType, innerType] = match;
105+
if (innerType.match(COMPLEX_TYPE)) {
106+
return getInnerType(innerType);
107+
}
108+
const mappedType = getMappedType(innerType);
109+
return mappedType || innerType;
110+
}
111+
const mappedType = getMappedType(key);
112+
return mappedType || key;
113+
};
114+
97115
export const isByteAType = (type: string) => {
98116
return BYTEA_TYPES.indexOf(type) !== -1;
99117
};

src/statement/hydrateResponse.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import BigNumber from "bignumber.js";
22
import { ExecuteQueryOptions, Row } from "../types";
33
import { Meta } from "../meta";
4-
import { isByteAType, isDateType, isNumberType } from "./dataTypes";
4+
import {
5+
isByteAType,
6+
isDateType,
7+
isNumberType,
8+
getInnerType
9+
} from "./dataTypes";
510
import { hydrateDate } from "./hydrateDate";
611

712
const getHydratedValue = (
813
value: unknown,
9-
meta: Meta,
14+
type: string,
1015
executeQueryOptions: ExecuteQueryOptions
11-
) => {
12-
const { type } = meta;
16+
): any => {
17+
if (Array.isArray(value)) {
18+
const innerType = getInnerType(type);
19+
return value.map(element =>
20+
getHydratedValue(element, innerType, executeQueryOptions)
21+
);
22+
}
1323
if (isDateType(type)) {
1424
return value ? hydrateDate(value as string) : value;
1525
}
@@ -44,14 +54,14 @@ export const hydrateRow = (
4454
const key = +index;
4555
(hydratedRow as unknown[])[key] = getHydratedValue(
4656
row[key],
47-
column,
57+
column.type,
4858
executeQueryOptions
4959
);
5060
} else {
5161
const key = column.name;
5262
(hydratedRow as Record<string, unknown>)[key] = getHydratedValue(
5363
row[key],
54-
column,
64+
column.type,
5565
executeQueryOptions
5666
);
5767
}

test/integration/outputFormat.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,26 @@ describe("integration test", () => {
3636
expect(long_type.type).toEqual("long");
3737
expect(array_type.type).toEqual("array(int)");
3838
});
39+
it("hydrates arrays", async () => {
40+
const firebolt = Firebolt({
41+
apiEndpoint: process.env.FIREBOLT_API_ENDPOINT as string
42+
});
43+
44+
const connection = await firebolt.connect(connectionParams);
45+
46+
const statement = await connection.execute(
47+
`select
48+
'2023-06-20 14:50:00'::TIMESTAMPNTZ as ts,
49+
['2023-06-20 14:50:00'::TIMESTAMPNTZ] as ts_array,
50+
['2023-06-20 14:50:00'::TIMESTAMPNTZ, NULL] as nullable_ts_array;`
51+
);
52+
53+
const { data, meta } = await statement.fetchResult();
54+
expect(data.length).toEqual(1);
55+
const row = data[0];
56+
const [ts, ts_array, nullable_ts_array] = row as unknown[];
57+
expect(ts instanceof Date).toEqual(true);
58+
expect((ts_array as unknown[])[0] instanceof Date).toEqual(true);
59+
expect((nullable_ts_array as unknown[])[0] instanceof Date).toEqual(true);
60+
});
3961
});

0 commit comments

Comments
 (0)