-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.ts
53 lines (49 loc) · 1.26 KB
/
export.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
import { Database } from "sqlite3";
import { resolve } from "path";
import { writeFileSync } from "fs";
function connectToSqlite() {
const pth = resolve("database", "zvms.db");
return new Database(pth);
}
const tables = [
"volunteer",
"user",
"class",
"class_vol",
"user_vol",
"picture",
"issue",
"notice",
"user_notice",
"class_notice",
];
function exportTables(
db: Database
): Promise<{ key: string; value: unknown[] }[]> {
const promises = tables.map((table: string) => {
return new Promise((resolve, reject) => {
db.all(`SELECT * FROM ${table}`, (err: Error, rows: unknown[]) => {
if (err) {
reject(err);
}
resolve({
key: table,
value: rows,
} as { key: string; value: unknown[] });
});
});
}) as Promise<{ key: string; value: unknown[] }>[];
return Promise.all(promises);
}
export async function exportToJSON() {
const db = connectToSqlite();
const folder = resolve("data", "export");
const tables = await exportTables(db);
tables.forEach((table: { key: string; value: unknown[] }) => {
console.log("Exporting table", table.key);
writeFileSync(
resolve(folder, table.key + ".json"),
JSON.stringify(table.value, null, 2)
);
});
}