-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexportToCompressedCsv.ts
35 lines (28 loc) · 1.1 KB
/
exportToCompressedCsv.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
import { Database } from "https://deno.land/x/[email protected]/mod.ts";
import { stringify } from "https://deno.land/[email protected]/csv/mod.ts";
import {
compress,
init,
} from "https://deno.land/x/[email protected]/deno/zstd.ts";
await init();
export default function (db: Database) {
const tableQuery = "SELECT name FROM sqlite_master WHERE type='table'";
const tables = db.prepare(tableQuery).all();
// Export each table to a CSV file and compress it
for (const table of tables) {
const tableName = table.name;
// Get headers
const headersQuery = `PRAGMA table_info(${tableName})`;
const headers = db.prepare(headersQuery).all().map((h) => h.name);
const query = `SELECT * FROM ${tableName}`;
const rows = db.prepare(query).all();
const csvBytes = new TextEncoder().encode(stringify(rows, {
headers: true,
columns: headers,
}));
// Compress the CSV file using Zstd and write
const compressedData = compress(csvBytes);
Deno.writeFileSync(`build/${tableName}.csv.zst`, compressedData);
console.log(`Table '${tableName}' exported and compressed.`);
}
}