feat: implement SQLite persistent storage for analysis results

- Implemented database initialization and connection management in `database.ts` using Bun's SQLite.
- Created `runs` and `results` tables to track historical analysis metadata and detailed product performance.
- Updated `writer.ts` to persist analysis results to the database within a transaction, replacing the previous CSV output logic.
- Updated README and `.gitignore` to reflect the new persistent storage capability.
This commit is contained in:
Victor Noguera
2026-04-12 23:41:40 -04:00
parent 0162e54007
commit 8ffbd48c46
5 changed files with 223 additions and 25 deletions

View File

@@ -3,7 +3,10 @@ import { fetchKeepaDataBatch } from "./keepa.ts";
import { fetchSellabilityBatch, fetchSpApiPricingAndFees } from "./sp-api.ts";
import { connectCache, getCache, setCache, disconnectCache } from "./cache.ts";
import { analyzeProducts } from "./llm.ts";
import { printResults, writeResultsCsv } from "./writer.ts";
import { printResults, writeResultsToDb } from "./writer.ts";
import { initDb, closeDb } from "./database.ts";
const DB_PATH = "./results.db";
import type {
EnrichedProduct,
AnalysisResult,
@@ -35,6 +38,10 @@ async function main() {
console.log("Connecting to Redis...");
await connectCache();
// Initialize SQLite DB
console.log("Initializing SQLite database...");
initDb(DB_PATH);
// Phase 1: Read input file
console.log(`\nReading ${inputFile}...`);
const products = readProducts(inputFile);
@@ -279,11 +286,10 @@ async function main() {
printResults(allResults);
if (outputFile) {
writeResultsCsv(allResults, outputFile);
}
writeResultsToDb(allResults, DB_PATH, inputFile, outputFile);
await disconnectCache();
closeDb();
}
main().catch((err) => {