autosql
Version:
An auto-parser of JSON into SQL.
43 lines (42 loc) • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const worker_threads_1 = require("worker_threads");
const database_1 = require("../db/database");
(async () => {
try {
// ✅ Ensure workerData is received properly
if (!worker_threads_1.workerData || !worker_threads_1.workerData.dbConfig) {
throw new Error("workerData is missing or invalid! Ensure it's passed correctly.");
}
// Extract database config, method, and params
const { dbConfig } = worker_threads_1.workerData;
let db;
db = database_1.Database.create(dbConfig);
const autoSQL = db.autoSQLHandler;
worker_threads_1.parentPort?.on("message", async (task) => {
const { method, params } = task;
const normalizedParams = Array.isArray(params) ? params : [params];
if (method === "test") {
const randomTimeInMs = Math.random() * 500;
const startTime = db.startDate;
const result = `${startTime}, ${params}`;
await new Promise(resolve => setTimeout(resolve, randomTimeInMs)); // Simulate async delay
worker_threads_1.parentPort?.postMessage({ success: true, result: result });
return;
}
else if (typeof autoSQL[method] === "function") {
const result = await autoSQL[method](...normalizedParams);
worker_threads_1.parentPort?.postMessage({ success: true, result });
}
else {
throw new Error(`Invalid method: ${method}`);
}
});
}
catch (error) {
worker_threads_1.parentPort?.postMessage({
success: false,
error: error instanceof Error ? error.message : String(error),
});
}
})();