67 lines
1.4 KiB
TypeScript
67 lines
1.4 KiB
TypeScript
import pl, { DataFrame } from 'nodejs-polars'
|
|
import fs from 'fs'
|
|
|
|
export function test_x(params: any) {
|
|
const fooSeries = pl.Series("foo", [1, 2, 3])
|
|
|
|
let res = fooSeries.sum()
|
|
|
|
console.log(res)
|
|
|
|
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<string, any>) {
|
|
if (this.df) {
|
|
this.df = this.df.vstack(DataFrame([row]))
|
|
this.saveCSV()
|
|
}
|
|
}
|
|
|
|
read(filter: Record<string, any>) {
|
|
if (this.df) {
|
|
return this.df.filter(filter)
|
|
}
|
|
return null
|
|
}
|
|
|
|
update(filter: Record<string, any>, values: Record<string, any>) {
|
|
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<string, any>) {
|
|
if (this.df) {
|
|
// should be filter Not
|
|
this.df = this.df.filter(filter)
|
|
this.saveCSV()
|
|
}
|
|
}
|
|
}
|