use node, esbuild, lowdb, csv-parse

This commit is contained in:
2023-06-12 00:40:10 -04:00
parent 64fb6cfaf3
commit d6445cd239
6 changed files with 188 additions and 919 deletions
+28
View File
@@ -0,0 +1,28 @@
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs/promises';
import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'
import {parse} from 'csv-parse/sync';
// initialize lowdb:
// db.json file path
const __dirname = dirname(fileURLToPath(import.meta.url))
const file = join(__dirname, 'db.json')
// Configure lowdb to write data to JSON file
const adapter = new JSONFile<{ stocks: Array<any>, options: Array<any> }>(file)
const defaultData = { stocks: [], options: [] }
const db = new Low(adapter, defaultData)
// read each csv, and ingest each row into lowdb
const csvDir = '/home/avraham/programming/calendar-optimizer-csv/';
const csvFiles = await fs.readdir(csvDir);
await Promise.all(csvFiles.filter((csvFile)=>csvFile.substring(10,16)==='stocks').map(async (csvFile)=>{
db.data.stocks.push( parse(await fs.readFile(join(csvDir, csvFile))) );
}));
await db.write();