make async database integrity check

This commit is contained in:
Zimeng Xiong
2025-11-22 21:59:18 -08:00
parent ef412a3887
commit 9055661b51
2 changed files with 45 additions and 16 deletions
+18
View File
@@ -0,0 +1,18 @@
const { parentPort, workerData } = require('worker_threads');
const Database = require('better-sqlite3');
if (!parentPort) throw new Error("Must be run in a worker thread");
try {
const { filePath } = workerData;
const db = new Database(filePath, { readonly: true, fileMustExist: true });
// This is the CPU-heavy operation
const result = db.prepare("PRAGMA integrity_check;").get();
db.close();
parentPort.postMessage(result.integrity_check === "ok");
} catch (error) {
// Any error means invalid or corrupt DB
parentPort.postMessage(false);
}