-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSVHelper.ts
69 lines (60 loc) · 2.23 KB
/
CSVHelper.ts
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
65
66
67
68
69
import { ParseResult, parse } from "papaparse";
import VMD, { Table } from "../virtualmodel/VMD";
export function validateCSV(
csvObject: ParseResult<unknown>,
table: Table
): void {
const csvHeaders = csvObject.meta.fields;
// Reject if the csv file doesn't have columns (headers)
if (!csvHeaders) {
throw new Error("Could not parse the headers of the CSV file.");
}
const serialColumns = table.columns
.filter(column => column.is_serial);
// Rejects if the csv file has a number of headers less than the number of columns in the table minus the number of serial columns
if (csvHeaders.length > table.columns.length || csvHeaders.length < (table.columns.length - serialColumns.length)) {
throw new Error(
"The number of headers in the CSV file is not valid."
);
}
// Rejects if the csv file has duplicate headers
if (new Set(csvHeaders).size !== csvHeaders.length) {
throw new Error("The CSV file has duplicate headers.");
}
// Rejects if not all the table columns are included in the csv headers
table.columns.every((column) => {
if (!csvHeaders.includes(column.column_name) && !column.is_serial) {
throw new Error(
`Column ${column.column_name} could not be found in the headers of the CSV file.`
);
}
return true;
});
}
export async function loadCSVToDB(
csvObject: ParseResult<unknown>,
table: Table
): Promise<void> {
const schema = VMD.getTableSchema(table.table_name);
if (!schema) {
throw new Error(`Could not find the schema for table ${table.table_name}.`);
}
// Filtering SERIAL typed columns from the CSV file
const serialColumns = table.columns
.filter(column => column.is_serial)
.map(column => column.column_name);
const filteredCSVObject = csvObject.data.map((row: any) => {
let filteredRow = { ...row };
serialColumns.forEach((serialColumn) => {
delete filteredRow[serialColumn]
})
return filteredRow;
})
const dataAccessor = VMD.getAddRowDataAccessor(
schema.schema_name,
table.table_name,
filteredCSVObject,
true
);
await dataAccessor.addRow();
}