-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhydrateRecords.ts
More file actions
58 lines (49 loc) · 1.39 KB
/
hydrateRecords.ts
File metadata and controls
58 lines (49 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
ColumnMetadata,
Field
} from '@aws-sdk/client-rds-data';
import {
ColumnValue,
Row
} from './types';
function getAuroraDataValue (value: Field): ColumnValue {
if ('blobValue' in value) {
return value.blobValue;
} else if ('doubleValue' in value) {
return value.doubleValue;
} else if ('isNull' in value) {
return null;
} else if ('longValue' in value) {
return value.longValue;
} else if ('stringValue' in value) {
return value.stringValue;
} else /* istanbul ignore else */ if ('booleanValue' in value) {
return value.booleanValue;
} else {
const type = Object.keys(value)[0];
throw new Error(`Unknown value type '${type}' from row`);
}
}
export default function hydrateRecords (records: Field[][], fields: ColumnMetadata[]): Row[] {
return records.map(record => record.reduce((row, value, index): Row => {
const field = fields[index];
let hydratedValue = getAuroraDataValue(value);
if (hydratedValue !== null) {
switch (field.typeName) {
case 'DECIMAL':
hydratedValue = Number(hydratedValue);
break;
case 'DATE':
case 'DATETIME':
case 'TIMESTAMP':
case 'YEAR':
hydratedValue = new Date(hydratedValue + 'Z');
break;
default:
break;
}
}
row[field.label as string] = hydratedValue;
return row;
}, {} as Row));
}