diff --git a/pos-frontend/app/database.ts b/pos-frontend/app/database.ts index a182d0f..014f54b 100644 --- a/pos-frontend/app/database.ts +++ b/pos-frontend/app/database.ts @@ -1,4 +1,5 @@ -import pl from 'nodejs-polars' +import pl, { DataFrame } from 'nodejs-polars' +import fs from 'fs' export function test_x(params: any) { const fooSeries = pl.Series("foo", [1, 2, 3]) @@ -9,3 +10,57 @@ export function test_x(params: any) { let df = pl.readCSV("data/iris.csv") } + +class CSVDatabase { + private df: DataFrame | null = null + private filePath: string + + constructor(filePath: string) { + this.filePath = filePath + this.loadCSV() + } + + private loadCSV() { + fs.existsSync(this.filePath) ? this.df = pl.readCSV(this.filePath) : this.df = DataFrame([]) + } + + private saveCSV() { + if (this.df) { + fs.writeFileSync(this.filePath, this.df.writeCSV()) + } + } + + create(row: Record) { + if (this.df) { + this.df = this.df.vstack(DataFrame([row])) + this.saveCSV() + } + } + + read(filter: Record) { + if (this.df) { + return this.df.filter(filter) + } + return null + } + + update(filter: Record, values: Record) { + if (this.df) { + // this.df = this.df.withColumn( + // Object.keys(values)[0], + // this.df + // .filter(filter) + // .withColumn(Object.keys(values)[0], pl.lit(values[Object.keys(values)[0]])) + // ) + this.saveCSV() + } + } + + delete(filter: Record) { + if (this.df) { + // should be filter Not + this.df = this.df.filter(filter) + this.saveCSV() + } + } +}