implement batching

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2024-09-23 20:42:29 +02:00
parent 0a541a91e8
commit bffb802ba2
No known key found for this signature in database

View file

@ -38,12 +38,19 @@ export class ExportAllCommand extends BaseCommand {
await fs.promises.mkdir(backupPath, { recursive: true });
for (const { name: tableName, columns } of tables) {
// TODO: implement batching
const rows = await connection.query(`SELECT * from ${tableName}`);
const totalRowsCount = await connection
.query(`SELECT COUNT(*) AS count FROM ${tableName}`)
.then((rows: Array<{ count: number }>) => rows[0].count);
const stream = fs.createWriteStream(join(backupPath, `${tableName}.jsonl`));
let cursor = 0;
const batchSize = 10;
while (cursor < totalRowsCount) {
const rows = await connection.query(
`SELECT * from ${tableName} LIMIT ${cursor}, ${batchSize}`,
);
for (const row of rows) {
// Our sqlite setup has some quirks. The following code normalizes the exported data so that it can be imported into a new postgres or sqlite database.
if (this.globalConfig.database.type === 'sqlite') {
@ -66,6 +73,9 @@ export class ExportAllCommand extends BaseCommand {
stream.write('\n');
}
cursor += batchSize;
}
stream.end();
}