use node, esbuild, lowdb, csv-parse
This commit is contained in:
@@ -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();
|
||||
Reference in New Issue
Block a user