This commit is contained in:
Jasen Qin 2024-04-27 17:27:08 +10:00
parent b799f686d7
commit 4715e323fc
4 changed files with 66 additions and 12 deletions

View File

@ -1,9 +1,22 @@
import '@testing-library/jest-dom' import '@testing-library/jest-dom'
import pl from 'nodejs-polars' import pl from 'nodejs-polars'
import { test_x } from '../app/database' import { test_x, create_csv_database, add_row, print_head, delete_row_by_key, save_db } from '../app/database'
describe('DATABASE', () => { describe('DATABASE', () => {
it('DATA', () => { it('DATA', () => {
test_x() test_x()
}) })
it('create csv', () => {
let res = create_csv_database('csv_db/x.csv')
print_head(res)
add_row(res, { "key": ["x"], "value": ["y"] })
print_head(res)
save_db(res)
delete_row_by_key(res, "x")
print_head(res)
})
}) })

View File

@ -3,4 +3,12 @@ describe('Database 2', () => {
// so do firebase // so do firebase
// and can optionally have a local index if needed // and can optionally have a local index if needed
}) })
it('Hi', () => {
let x = "hi" == "hi"
let y = "hi2" == "hi"
console.log(x)
console.log(y)
})
}) })

View File

@ -1,5 +1,5 @@
import pl, { DataFrame } from 'nodejs-polars' import pl, { DataFrame } from 'nodejs-polars'
import fs from 'fs' import fs, { writeFile } 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])
@ -38,20 +38,12 @@ class CSVDatabase {
} }
read(filter: Record<string, any>) { read(filter: Record<string, any>) {
if (this.df) { return this.df ? this.df.filter(filter) : null
return this.df.filter(filter)
}
return null
} }
update(filter: Record<string, any>, values: Record<string, any>) { update(filter: Record<string, any>, values: Record<string, any>) {
if (this.df) { if (this.df) {
// this.df = this.df.withColumn( // update it
// Object.keys(values)[0],
// this.df
// .filter(filter)
// .withColumn(Object.keys(values)[0], pl.lit(values[Object.keys(values)[0]]))
// )
this.saveCSV() this.saveCSV()
} }
} }
@ -64,3 +56,41 @@ class CSVDatabase {
} }
} }
} }
// A CSV can refer to other CSV's via pointers, a type of metadata
interface CSVDatabase2 {
df: DataFrame
filepath: string
}
export function create_csv_database(filepath: string): CSVDatabase2 {
// let df = DataFrame([], { columns: ["key", "value"] })
let df = DataFrame({ "key": ['header'], "value": ['next'] })
// writeFile(filepath, 'key,value\nheader,next', () => { })
// let df = pl.readCSV(filepath)
df.writeCSV(filepath)
return { df, filepath }
}
export function save_db(db: CSVDatabase2) {
db.df.writeCSV(db.filepath)
}
export function add_row(db: CSVDatabase2, row: Record<string, string>) {
db.df = db.df.vstack(DataFrame(row))
}
export function print_head(db: CSVDatabase2) {
console.log(db.df.head())
}
// can filter everything that is not the row, and do to db
export function delete_row_by_key({df, filepath}: CSVDatabase2, key: string) {
// db.df.drop(pl.col("key").eq(key))
// df = df.select((pl.col("key") != key).alias("nrs > 1"))
// db.df = db.df.filter(pl.col("key").neq(key))
}

View File

@ -0,0 +1,3 @@
key,value
header,next
x,y
1 key value
2 header next
3 x y