-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertVerseText.ts
49 lines (46 loc) · 1.14 KB
/
insertVerseText.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
import { Database } from "https://deno.land/x/sqlite3@0.9.1/mod.ts";
const BATCH_SIZE = 50_000;
let tableIsReady = false;
const prepareTable = (db: Database) => {
db.exec(`
DROP TABLE IF EXISTS parallel;
CREATE TABLE parallel (
parallel_id INTEGER NOT NULL,
versification_schema_id INTEGER NOT NULL,
module_id INTEGER NOT NULL,
rid INTEGER NOT NULL,
text TEXT
);`);
tableIsReady = true;
};
export default (db: Database, verseTexts: VerseText[]) => {
if (!tableIsReady) {
prepareTable(db);
}
const insertVerseText = db.prepare(`
INSERT INTO parallel (
parallel_id,
versification_schema_id,
module_id,
rid,
text
) VALUES (
:parallel_id,
:versification_schema_id,
:module_id,
:rid,
:text
);
`);
const insertVerseTextBatch = db.transaction((batch) => {
for (const v of batch) {
insertVerseText.run(v);
}
});
let i = 0;
while (verseTexts.length) {
const batch = verseTexts.splice(0, BATCH_SIZE);
insertVerseTextBatch(batch);
console.log(` - Inserted ${i += batch.length} verses`);
}
};