add
This commit is contained in:
parent
d8a14a7c3b
commit
b799f686d7
|
|
@ -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) {
|
export function test_x(params: any) {
|
||||||
const fooSeries = pl.Series("foo", [1, 2, 3])
|
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")
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue