-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
64 lines (64 loc) · 2.14 KB
/
helpers.js
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
59
60
61
62
63
64
/**
*
* @param columnMetadata - Column metadata from a source database.
* @returns Column lists for creating and inserting records.
*/
export function buildColumnLists(columnMetadata) {
const create = [];
const insert = [];
const parameters = [];
for (const [columnIndex, column] of Object.entries(columnMetadata)) {
create.push(columnMetadataToCreateString(column));
insert.push(`[${column.name}]`);
parameters.push(`@${columnIndex}`);
}
return {
create: create.join(', '),
insert: insert.join(', '),
parameters: parameters.join(', ')
};
}
function columnMetadataToCreateString(columnMetadata) {
console.log(columnMetadata);
const createStringPieces = [`[${columnMetadata.name}]`];
const columnType = (typeof columnMetadata.type === 'function'
? columnMetadata.type()
: columnMetadata.type);
const columnTypeName = columnType === undefined
? 'VarChar'
: columnType.type.name;
switch (columnTypeName) {
case 'Char':
case 'NChar':
case 'VarChar':
case 'NVarChar':
case 'VarBinary': {
createStringPieces.push(`${columnTypeName} (${columnMetadata.length === 0 || columnMetadata.length > 8000 ? 'max' : columnMetadata.length})`);
break;
}
case 'Decimal':
case 'Numeric': {
if (columnMetadata.precision !== undefined &&
columnMetadata.scale !== undefined) {
createStringPieces.push(`${columnTypeName} (${columnMetadata.precision}, ${columnMetadata.scale})`);
}
else {
createStringPieces.push(`VarChar (${columnMetadata.length + 1})`);
}
break;
}
case 'Time':
case 'DateTime2':
case 'DateTimeOffset': {
createStringPieces.push(`${columnTypeName} (${columnMetadata.scale})`);
break;
}
default: {
createStringPieces.push(columnTypeName);
}
}
if (!columnMetadata.nullable) {
createStringPieces.push('not null');
}
return createStringPieces.join(' ');
}