-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsertModuleInfo.ts
67 lines (63 loc) · 1.82 KB
/
insertModuleInfo.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
import { Database } from "https://deno.land/x/[email protected]/mod.ts";
const versificationSchemas: { [schema: string]: number } = {};
const getVersificationSchemaId = (versification_schema: string) => {
if (!versificationSchemas[versification_schema]) {
versificationSchemas[versification_schema] =
Object.keys(versificationSchemas).length + 1;
}
return versificationSchemas[versification_schema];
};
// sqlite escape quotes
const sqliteEscape = (str: string) => str.replace(/'/g, "''");
export default (modulePartials: ValidModules[], db: Database) => {
const modules = modulePartials.map((m, i) => ({
...m,
module_id: i + 1,
versification_schema_id: getVersificationSchemaId(m.versification_schema),
}));
db.exec(`
DROP TABLE IF EXISTS module_info;
CREATE TABLE module_info (
module_id SERIAL PRIMARY KEY,
name TEXT,
abbreviation TEXT,
description TEXT,
corpora TEXT,
language TEXT,
versification_schema TEXT,
versification_schema_id INTEGER NOT NULL,
license TEXT,
url TEXT
);
`);
const query = `
INSERT INTO module_info (
module_id,
abbreviation,
name,
description,
corpora,
language,
versification_schema,
versification_schema_id,
license,
url
) VALUES ${
modules.map((m) =>
`(
${m.module_id},
'${sqliteEscape(m.abbreviation)}',
'${sqliteEscape(m.name)}',
'${sqliteEscape(m.description)}',
'${sqliteEscape(m.corpora.join(","))}',
'${sqliteEscape(m.language)}',
'${sqliteEscape(m.versification_schema)}',
${m.versification_schema_id},
'${sqliteEscape(m.license)}',
'${sqliteEscape(m.url)}'
)`
).join(",")
}`;
db.exec(query);
return { modules, versificationSchemas };
};