del db2 with pr

This commit is contained in:
Jasen Qin 2024-05-20 08:07:16 +10:00
parent 6a738af3ae
commit 3d6846b554
8 changed files with 211 additions and 53 deletions

View File

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

View File

@ -0,0 +1,32 @@
import '@testing-library/jest-dom'
import pl from 'nodejs-polars'
import { create_csv_database, add_row, print_head, delete_row_by_key, save_db } from '../app/database'
describe('DATABASE', () => {
it('DATA', () => {
const fooSeries = pl.Series("foo", [1, 2, 3])
let res = fooSeries.sum()
console.log(res)
let df = pl.readCSV("data/iris.csv")
})
it('create csv', () => {
let res = create_csv_database('csv_db/x.csv', { "a_key": [], "a_value": [] })
print_head(res)
add_row(res, { "a_key": ["x"], "a_value": ["y"] })
print_head(res)
save_db(res)
delete_row_by_key(res, "x", "a_key")
print_head(res)
})
it('creates pos model', () => {
})
})

View File

@ -1,57 +1,43 @@
import pl, { DataFrame } from 'nodejs-polars' import pl, { DataFrame, Series } from 'nodejs-polars'
import fs, { writeFile } from 'fs' import fs from 'fs'
export function test_x(params: any) { export interface CSVDatabase {
const fooSeries = pl.Series("foo", [1, 2, 3])
let res = fooSeries.sum()
console.log(res)
let df = pl.readCSV("data/iris.csv")
}
interface CSVDatabase2 {
df: DataFrame df: DataFrame
filepath: string filepath: string
} }
export function create_csv_database(filepath: string): CSVDatabase2 { export function create_csv_database(filepath: string, initialData: any): CSVDatabase {
let df = DataFrame({ 'a_key': ['1'], 'a_value': ['2'] }) let df = DataFrame(initialData)
df.writeCSV(filepath) df.writeCSV(filepath)
return { df, filepath } return { df, filepath }
} }
export function save_db(db: CSVDatabase2) { export function save_db(db: CSVDatabase) {
db.df.writeCSV(db.filepath) db.df.writeCSV(db.filepath)
} }
export function add_row(db: CSVDatabase2, row: any) { export function add_row(db: CSVDatabase, row: any) {
let d = DataFrame(row) let d = DataFrame([row])
db.df = db.df.vstack(d) db.df = db.df.vstack(d)
} }
export function print_head(db: CSVDatabase2) { export function print_head(db: CSVDatabase) {
console.log(db.df.head()) console.log(db.df.head())
} }
export function delete_row_by_key(db: CSVDatabase2, key: string) { export function delete_row_by_key(db: CSVDatabase, key: string, keyColumn: string) {
console.log("NEXT COLUMNS") db.df = db.df.filter(pl.col(keyColumn).neq(pl.lit(key)))
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 { export function select_columns(db: CSVDatabase, columns: string[]): DataFrame {
return db.df.select(...columns) return db.df.select(...columns)
} }
export function join_dataframes(db1: CSVDatabase2, db2: CSVDatabase2, on: string): DataFrame { export function join_dataframes(db1: CSVDatabase, db2: CSVDatabase, on: string): DataFrame {
return db1.df.join(db2.df, { on, how: "inner" }) // or "left", "right", "outer" return db1.df.join(db2.df, { on, how: "inner" })
} }
export function add_simple_index(db: CSVDatabase2) { export function add_simple_index(db: CSVDatabase) {
const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index const indices = Array.from(Array(db.df.height).keys()) // Create 0-N index
db.df = db.df.withColumn(pl.Series("index", indices)) db.df = db.df.withColumn(pl.Series("index", indices))
} }

View File

@ -1,5 +1,8 @@
// point of sales model core // point of sales model core
import { CSVDatabase } from "./database"
import { create_csv_database, print_head } from "./database"
interface Item { interface Item {
id: string id: string
name: string name: string
@ -19,3 +22,48 @@ interface Inventory {
itemId: string itemId: string
quantity: number quantity: number
} }
export function create_item_dataframe(items: Item[]): CSVDatabase {
const filepath = 'data/items.csv'
return create_csv_database(filepath, items)
}
export function create_sale_dataframe(sales: Sale[]): CSVDatabase {
const filepath = 'data/sales.csv'
const data = sales.map(sale => ({
...sale,
timestamp: sale.timestamp.toISOString() // Convert Date to string
}))
return create_csv_database(filepath, data)
}
export function create_inventory_dataframe(inventories: Inventory[]): CSVDatabase {
const filepath = 'data/inventory.csv'
return create_csv_database(filepath, inventories)
}
function create_models() {
const items: Item[] = [
{ id: '1', name: 'Item 1', price: 100 },
{ id: '2', name: 'Item 2', price: 200 }
]
const sales: Sale[] = [
{ id: '1', itemId: '1', quantity: 2, timestamp: new Date() },
{ id: '2', itemId: '2', quantity: 1, timestamp: new Date() }
]
const inventories: Inventory[] = [
{ itemId: '1', quantity: 50 },
{ itemId: '2', quantity: 30 }
]
const itemDB = create_item_dataframe(items)
const saleDB = create_sale_dataframe(sales)
const inventoryDB = create_inventory_dataframe(inventories)
print_head(itemDB)
print_head(saleDB)
print_head(inventoryDB)
}

View File

@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
}

View File

@ -1,3 +1 @@
a_key,a_value a_key,a_value
1,2
x,y

1 a_key a_value
1 2
x y

View File

@ -49,6 +49,7 @@
"rocksdb": "^5.2.1" "rocksdb": "^5.2.1"
}, },
"devDependencies": { "devDependencies": {
"@babel/preset-typescript": "^7.24.1",
"@next/eslint-plugin-next": "^14.2.0", "@next/eslint-plugin-next": "^14.2.0",
"@testing-library/jest-dom": "^6.4.2", "@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^14.2.2", "@testing-library/react": "^14.2.2",

View File

@ -117,6 +117,9 @@ importers:
specifier: ^5.2.1 specifier: ^5.2.1
version: 5.2.1 version: 5.2.1
devDependencies: devDependencies:
'@babel/preset-typescript':
specifier: ^7.24.1
version: 7.24.1(@babel/core@7.24.5)
'@next/eslint-plugin-next': '@next/eslint-plugin-next':
specifier: ^14.2.0 specifier: ^14.2.0
version: 14.2.3 version: 14.2.3
@ -216,10 +219,20 @@ packages:
resolution: {integrity: sha512-x32i4hEXvr+iI0NEoEfDKzlemF8AmtOP8CcrRaEcpzysWuoEb1KknpcvMsHKPONoKZiDuItklgWhB18xEhr9PA==} resolution: {integrity: sha512-x32i4hEXvr+iI0NEoEfDKzlemF8AmtOP8CcrRaEcpzysWuoEb1KknpcvMsHKPONoKZiDuItklgWhB18xEhr9PA==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
'@babel/helper-annotate-as-pure@7.22.5':
resolution: {integrity: sha512-LvBTxu8bQSQkcyKOU+a1btnNFQ1dMAd0R6PyW3arXes06F6QLWLIrd681bxRPIXlrMGR3XYnW9JyML7dP3qgxg==}
engines: {node: '>=6.9.0'}
'@babel/helper-compilation-targets@7.23.6': '@babel/helper-compilation-targets@7.23.6':
resolution: {integrity: sha512-9JB548GZoQVmzrFgp8o7KxdgkTGm6xs9DW0o/Pim72UDjzr5ObUQ6ZzYPqA+g9OTS2bBQoctLJrky0RDCAWRgQ==} resolution: {integrity: sha512-9JB548GZoQVmzrFgp8o7KxdgkTGm6xs9DW0o/Pim72UDjzr5ObUQ6ZzYPqA+g9OTS2bBQoctLJrky0RDCAWRgQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
'@babel/helper-create-class-features-plugin@7.24.5':
resolution: {integrity: sha512-uRc4Cv8UQWnE4NXlYTIIdM7wfFkOqlFztcC/gVXDKohKoVB3OyonfelUBaJzSwpBntZ2KYGF/9S7asCHsXwW6g==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0
'@babel/helper-environment-visitor@7.22.20': '@babel/helper-environment-visitor@7.22.20':
resolution: {integrity: sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA==} resolution: {integrity: sha512-zfedSIzFhat/gFhWfHtgWvlec0nqB9YEIVrpuwjruLlXfUSnA8cJB0miHKwqDnQ7d32aKo2xt88/xZptwxbfhA==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -232,6 +245,10 @@ packages:
resolution: {integrity: sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw==} resolution: {integrity: sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
'@babel/helper-member-expression-to-functions@7.24.5':
resolution: {integrity: sha512-4owRteeihKWKamtqg4JmWSsEZU445xpFRXPEwp44HbgbxdWlUV1b4Agg4lkA806Lil5XM/e+FJyS0vj5T6vmcA==}
engines: {node: '>=6.9.0'}
'@babel/helper-module-imports@7.24.3': '@babel/helper-module-imports@7.24.3':
resolution: {integrity: sha512-viKb0F9f2s0BCS22QSF308z/+1YWKV/76mwt61NBzS5izMzDPwdq1pTrzf+Li3npBWX9KdQbkeCt1jSAM7lZqg==} resolution: {integrity: sha512-viKb0F9f2s0BCS22QSF308z/+1YWKV/76mwt61NBzS5izMzDPwdq1pTrzf+Li3npBWX9KdQbkeCt1jSAM7lZqg==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -242,14 +259,28 @@ packages:
peerDependencies: peerDependencies:
'@babel/core': ^7.0.0 '@babel/core': ^7.0.0
'@babel/helper-optimise-call-expression@7.22.5':
resolution: {integrity: sha512-HBwaojN0xFRx4yIvpwGqxiV2tUfl7401jlok564NgB9EHS1y6QT17FmKWm4ztqjeVdXLuC4fSvHc5ePpQjoTbw==}
engines: {node: '>=6.9.0'}
'@babel/helper-plugin-utils@7.24.5': '@babel/helper-plugin-utils@7.24.5':
resolution: {integrity: sha512-xjNLDopRzW2o6ba0gKbkZq5YWEBaK3PCyTOY1K2P/O07LGMhMqlMXPxwN4S5/RhWuCobT8z0jrlKGlYmeR1OhQ==} resolution: {integrity: sha512-xjNLDopRzW2o6ba0gKbkZq5YWEBaK3PCyTOY1K2P/O07LGMhMqlMXPxwN4S5/RhWuCobT8z0jrlKGlYmeR1OhQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
'@babel/helper-replace-supers@7.24.1':
resolution: {integrity: sha512-QCR1UqC9BzG5vZl8BMicmZ28RuUBnHhAMddD8yHFHDRH9lLTZ9uUPehX8ctVPT8l0TKblJidqcgUUKGVrePleQ==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0
'@babel/helper-simple-access@7.24.5': '@babel/helper-simple-access@7.24.5':
resolution: {integrity: sha512-uH3Hmf5q5n7n8mz7arjUlDOCbttY/DW4DYhE6FUsjKJ/oYC1kQQUvwEQWxRwUpX9qQKRXeqLwWxrqilMrf32sQ==} resolution: {integrity: sha512-uH3Hmf5q5n7n8mz7arjUlDOCbttY/DW4DYhE6FUsjKJ/oYC1kQQUvwEQWxRwUpX9qQKRXeqLwWxrqilMrf32sQ==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
'@babel/helper-skip-transparent-expression-wrappers@7.22.5':
resolution: {integrity: sha512-tK14r66JZKiC43p8Ki33yLBVJKlQDFoA8GYN67lWCDCqoL6EMMSuM9b+Iff2jHaM/RRFYl7K+iiru7hbRqNx8Q==}
engines: {node: '>=6.9.0'}
'@babel/helper-split-export-declaration@7.24.5': '@babel/helper-split-export-declaration@7.24.5':
resolution: {integrity: sha512-5CHncttXohrHk8GWOFCcCl4oRD9fKosWlIRgWm4ql9VYioKm52Mk2xsmoohvm7f3JoiLSM5ZgJuRaf5QZZYd3Q==} resolution: {integrity: sha512-5CHncttXohrHk8GWOFCcCl4oRD9fKosWlIRgWm4ql9VYioKm52Mk2xsmoohvm7f3JoiLSM5ZgJuRaf5QZZYd3Q==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -352,6 +383,24 @@ packages:
peerDependencies: peerDependencies:
'@babel/core': ^7.0.0-0 '@babel/core': ^7.0.0-0
'@babel/plugin-transform-modules-commonjs@7.24.1':
resolution: {integrity: sha512-szog8fFTUxBfw0b98gEWPaEqF42ZUD/T3bkynW/wtgx2p/XCP55WEsb+VosKceRSd6njipdZvNogqdtI4Q0chw==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/plugin-transform-typescript@7.24.5':
resolution: {integrity: sha512-E0VWu/hk83BIFUWnsKZ4D81KXjN5L3MobvevOHErASk9IPwKHOkTgvqzvNo1yP/ePJWqqK2SpUR5z+KQbl6NVw==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/preset-typescript@7.24.1':
resolution: {integrity: sha512-1DBaMmRDpuYQBPWD8Pf/WEwCrtgRHxsZnP4mIy9G/X+hFfbI47Q2G4t1Paakld84+qsk2fSsUPMKg71jkoOOaQ==}
engines: {node: '>=6.9.0'}
peerDependencies:
'@babel/core': ^7.0.0-0
'@babel/runtime@7.24.5': '@babel/runtime@7.24.5':
resolution: {integrity: sha512-Nms86NXrsaeU9vbBJKni6gXiEXZ4CVpYVzEjDH9Sb8vmZ3UljyA1GSOJl/6LGPO8EHLuSF9H+IxNXHPX8QHJ4g==} resolution: {integrity: sha512-Nms86NXrsaeU9vbBJKni6gXiEXZ4CVpYVzEjDH9Sb8vmZ3UljyA1GSOJl/6LGPO8EHLuSF9H+IxNXHPX8QHJ4g==}
engines: {node: '>=6.9.0'} engines: {node: '>=6.9.0'}
@ -5513,6 +5562,10 @@ snapshots:
'@jridgewell/trace-mapping': 0.3.25 '@jridgewell/trace-mapping': 0.3.25
jsesc: 2.5.2 jsesc: 2.5.2
'@babel/helper-annotate-as-pure@7.22.5':
dependencies:
'@babel/types': 7.24.5
'@babel/helper-compilation-targets@7.23.6': '@babel/helper-compilation-targets@7.23.6':
dependencies: dependencies:
'@babel/compat-data': 7.24.4 '@babel/compat-data': 7.24.4
@ -5521,6 +5574,19 @@ snapshots:
lru-cache: 5.1.1 lru-cache: 5.1.1
semver: 6.3.1 semver: 6.3.1
'@babel/helper-create-class-features-plugin@7.24.5(@babel/core@7.24.5)':
dependencies:
'@babel/core': 7.24.5
'@babel/helper-annotate-as-pure': 7.22.5
'@babel/helper-environment-visitor': 7.22.20
'@babel/helper-function-name': 7.23.0
'@babel/helper-member-expression-to-functions': 7.24.5
'@babel/helper-optimise-call-expression': 7.22.5
'@babel/helper-replace-supers': 7.24.1(@babel/core@7.24.5)
'@babel/helper-skip-transparent-expression-wrappers': 7.22.5
'@babel/helper-split-export-declaration': 7.24.5
semver: 6.3.1
'@babel/helper-environment-visitor@7.22.20': {} '@babel/helper-environment-visitor@7.22.20': {}
'@babel/helper-function-name@7.23.0': '@babel/helper-function-name@7.23.0':
@ -5532,6 +5598,10 @@ snapshots:
dependencies: dependencies:
'@babel/types': 7.24.5 '@babel/types': 7.24.5
'@babel/helper-member-expression-to-functions@7.24.5':
dependencies:
'@babel/types': 7.24.5
'@babel/helper-module-imports@7.24.3': '@babel/helper-module-imports@7.24.3':
dependencies: dependencies:
'@babel/types': 7.24.5 '@babel/types': 7.24.5
@ -5545,12 +5615,27 @@ snapshots:
'@babel/helper-split-export-declaration': 7.24.5 '@babel/helper-split-export-declaration': 7.24.5
'@babel/helper-validator-identifier': 7.24.5 '@babel/helper-validator-identifier': 7.24.5
'@babel/helper-optimise-call-expression@7.22.5':
dependencies:
'@babel/types': 7.24.5
'@babel/helper-plugin-utils@7.24.5': {} '@babel/helper-plugin-utils@7.24.5': {}
'@babel/helper-replace-supers@7.24.1(@babel/core@7.24.5)':
dependencies:
'@babel/core': 7.24.5
'@babel/helper-environment-visitor': 7.22.20
'@babel/helper-member-expression-to-functions': 7.24.5
'@babel/helper-optimise-call-expression': 7.22.5
'@babel/helper-simple-access@7.24.5': '@babel/helper-simple-access@7.24.5':
dependencies: dependencies:
'@babel/types': 7.24.5 '@babel/types': 7.24.5
'@babel/helper-skip-transparent-expression-wrappers@7.22.5':
dependencies:
'@babel/types': 7.24.5
'@babel/helper-split-export-declaration@7.24.5': '@babel/helper-split-export-declaration@7.24.5':
dependencies: dependencies:
'@babel/types': 7.24.5 '@babel/types': 7.24.5
@ -5650,6 +5735,30 @@ snapshots:
'@babel/core': 7.24.5 '@babel/core': 7.24.5
'@babel/helper-plugin-utils': 7.24.5 '@babel/helper-plugin-utils': 7.24.5
'@babel/plugin-transform-modules-commonjs@7.24.1(@babel/core@7.24.5)':
dependencies:
'@babel/core': 7.24.5
'@babel/helper-module-transforms': 7.24.5(@babel/core@7.24.5)
'@babel/helper-plugin-utils': 7.24.5
'@babel/helper-simple-access': 7.24.5
'@babel/plugin-transform-typescript@7.24.5(@babel/core@7.24.5)':
dependencies:
'@babel/core': 7.24.5
'@babel/helper-annotate-as-pure': 7.22.5
'@babel/helper-create-class-features-plugin': 7.24.5(@babel/core@7.24.5)
'@babel/helper-plugin-utils': 7.24.5
'@babel/plugin-syntax-typescript': 7.24.1(@babel/core@7.24.5)
'@babel/preset-typescript@7.24.1(@babel/core@7.24.5)':
dependencies:
'@babel/core': 7.24.5
'@babel/helper-plugin-utils': 7.24.5
'@babel/helper-validator-option': 7.23.5
'@babel/plugin-syntax-jsx': 7.24.1(@babel/core@7.24.5)
'@babel/plugin-transform-modules-commonjs': 7.24.1(@babel/core@7.24.5)
'@babel/plugin-transform-typescript': 7.24.5(@babel/core@7.24.5)
'@babel/runtime@7.24.5': '@babel/runtime@7.24.5':
dependencies: dependencies:
regenerator-runtime: 0.14.1 regenerator-runtime: 0.14.1