waa
This commit is contained in:
parent
06a4d2990a
commit
fcecee03a4
|
|
@ -11,7 +11,7 @@ describe('DATABASE', () => {
|
|||
let res = create_csv_database('csv_db/x.csv')
|
||||
print_head(res)
|
||||
|
||||
add_row(res, { "key": ["x"], "value": ["y"] })
|
||||
add_row(res, { "a_key": ["x"], "a_value": ["y"] })
|
||||
print_head(res)
|
||||
|
||||
save_db(res)
|
||||
|
|
|
|||
|
|
@ -11,54 +11,6 @@ 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<string, any>) {
|
||||
if (this.df) {
|
||||
this.df = this.df.vstack(DataFrame([row]))
|
||||
this.saveCSV()
|
||||
}
|
||||
}
|
||||
|
||||
read(filter: Record<string, any>) {
|
||||
return this.df ? this.df.filter(filter) : null
|
||||
}
|
||||
|
||||
update(filter: Record<string, any>, values: Record<string, any>) {
|
||||
if (this.df) {
|
||||
// update it
|
||||
this.saveCSV()
|
||||
}
|
||||
}
|
||||
|
||||
delete(filter: Record<string, any>) {
|
||||
if (this.df) {
|
||||
// should be filter Not
|
||||
this.df = this.df.filter(filter)
|
||||
this.saveCSV()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A CSV can refer to other CSV's via pointers, a type of metadata
|
||||
|
||||
interface CSVDatabase2 {
|
||||
df: DataFrame
|
||||
filepath: string
|
||||
|
|
@ -66,7 +18,7 @@ interface CSVDatabase2 {
|
|||
|
||||
export function create_csv_database(filepath: string): CSVDatabase2 {
|
||||
// let df = DataFrame([], { columns: ["key", "value"] })
|
||||
let df = DataFrame({ "key": ['header'], "value": ['next'] })
|
||||
let df = DataFrame({ 'a_key': ['1'], 'a_value': ['2'] })
|
||||
|
||||
// writeFile(filepath, 'key,value\nheader,next', () => { })
|
||||
// let df = pl.readCSV(filepath)
|
||||
|
|
@ -79,8 +31,9 @@ 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 add_row(db: CSVDatabase2, row: any) {
|
||||
let d = DataFrame(row)
|
||||
db.df = db.df.vstack(d)
|
||||
}
|
||||
|
||||
export function print_head(db: CSVDatabase2) {
|
||||
|
|
@ -89,8 +42,27 @@ export function print_head(db: CSVDatabase2) {
|
|||
|
||||
// can filter everything that is not the row, and do to db
|
||||
|
||||
export function delete_row_by_key({df, filepath}: CSVDatabase2, key: string) {
|
||||
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))
|
||||
}
|
||||
|
||||
export function delete_row_by_key(db: CSVDatabase2, key: string) {
|
||||
console.log("NEXT COLUMNS")
|
||||
console.log(db.df.columns)
|
||||
db.df = db.df.filter(pl.col('a_key').neq(pl.lit(key)))
|
||||
}
|
||||
|
||||
export function select_columns(db: CSVDatabase2, columns: string[]): DataFrame {
|
||||
return db.df.select(...columns)
|
||||
}
|
||||
|
||||
export function join_dataframes(db1: CSVDatabase2, db2: CSVDatabase2, on: string): DataFrame {
|
||||
return db1.df.join(db2.df, { on, how: "inner" }) // or "left", "right", "outer"
|
||||
}
|
||||
|
||||
export function add_simple_index(db: CSVDatabase2) {
|
||||
const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index
|
||||
db.df = db.df.withColumn(pl.Series("index", indices))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
key,value
|
||||
header,next
|
||||
a_key,a_value
|
||||
1,2
|
||||
x,y
|
||||
|
|
|
|||
|
Loading…
Reference in New Issue