diff --git a/app.js b/app.js index 01fb5bc..1037298 100644 --- a/app.js +++ b/app.js @@ -1,93 +1,123 @@ +/** @import DBTypes from "./types.d.ts" */ import DB from "./db.js"; -// build parts -const Part = {}; -function CreatePart(id, name) +/** @type {DBTypes.Builder} */ +function DBBuilder(params) { - const part = {name, _:{id, time:0, work:[], need:[], make:[]}}; - Part[id] = part; - return part; -} -await DB(import.meta.resolve('./db_part.csv'), 2, CreatePart); + return { + list:{}, + load(){ + const l = this.list; + const loader =(id, data)=>l[id] = data; + return DB(import.meta.resolve(params.path), params.cols, (...args)=>{ - -// then build work -const Work = []; -function CreateWork(partId, user, time, data) -{ - const part = Part[partId]; - const work = {user: user, time, data} - part._.work.push(work); - if(time > part._.time) - { - part._.time = time; - } - Work.push(work); - return work; -} -await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork); - - -// then build desks -const Desk = {}; -/** @type {(need:string[], make:string[])=>[dirtyNeed:string[], dirtyMake:string[], needMax:number, needMin:number]} */ -const Range =(need, make)=> -{ - const dirtyNeed = []; - const dirtyMake = []; - - let makeMin = Infinity; - let needMax = -Infinity; - need.forEach(partId => { - const part = Part[partId]; - if(part._.time > needMax) needMax = part._.time; - }); - - make.forEach(partId => { - const part = Part[partId]; - if(part._.time < makeMin) makeMin = part._.time; - if(part._.time < needMax) - { - dirtyMake.push(partId); - } - }); - - need.forEach(partId => { - const part = Part[partId]; - if(part._.time > makeMin) - { - dirtyNeed.push(partId); - } - }); - - return [dirtyNeed, dirtyMake, needMax, makeMin]; -}; - -/** @typedef {[need:Record, make:string[], mode:"one"|"all", role:string[]} StoredDesk */ - -/** @type {(id:string, name:string, data:StoredDesk)=>void} */ -function CreateDesk(id, name, data, role, need, time, make, mode="one") -{ - const need = Object.keys(data[0]) - const time = Object.values(data[0]); - const desk = {name, need, time, make, role, _:{id}}; - Desk[id] = desk; - need.forEach(partId => Part[partId]._.need.push(desk)); - make.forEach(partId => Part[partId]._.make.push(desk)); - - const check = Range(need, make); - console.log(check); - - return desk; + params.load(loader, ...args); + }); + }, + save(){}, + }; } -await DB(import.meta.resolve('./db_work.csv'), 3, (id, name, /**@type{StoredDesk}*/data)=>{ - - const need = Object.keys(data[0]) - CreateDesk(id, name, need, data[1], ); +const parts = DBBuilder({ + path:"./db_part.csv", + cols:2, + load:(commit, id, name)=>commit(id, {name}), + save:(commit, params)=> commit("a", params.name) }); +await parts.load(); +console.log(parts.list); -console.log(Part); -console.log(Desk); \ No newline at end of file + +// // build parts +// const Part = {}; +// function CreatePart(id, name) +// { +// const part = {name, _:{id, time:0, work:[], need:[], make:[]}}; +// Part[id] = part; +// return part; +// } +// await DB(import.meta.resolve('./db_part.csv'), 2, CreatePart); +// +// +// // then build work +// const Work = []; +// function CreateWork(partId, user, time, data) +// { +// const part = Part[partId]; +// const work = {user: user, time, data} +// part._.work.push(work); +// if(time > part._.time) +// { +// part._.time = time; +// } +// Work.push(work); +// return work; +// } +// await DB(import.meta.resolve('./db_work.csv'), 3, CreateWork); +// +// +// // then build desks +// const Desk = {}; +// /** @type {(need:string[], make:string[])=>[dirtyNeed:string[], dirtyMake:string[], needMax:number, needMin:number]} */ +// const Range =(need, make)=> +// { +// const dirtyNeed = []; +// const dirtyMake = []; +// +// let makeMin = Infinity; +// let needMax = -Infinity; +// need.forEach(partId => { +// const part = Part[partId]; +// if(part._.time > needMax) needMax = part._.time; +// }); +// +// make.forEach(partId => { +// const part = Part[partId]; +// if(part._.time < makeMin) makeMin = part._.time; +// if(part._.time < needMax) +// { +// dirtyMake.push(partId); +// } +// }); +// +// need.forEach(partId => { +// const part = Part[partId]; +// if(part._.time > makeMin) +// { +// dirtyNeed.push(partId); +// } +// }); +// +// return [dirtyNeed, dirtyMake, needMax, makeMin]; +// }; +// +// /** @typedef {[need:Record, make:string[], mode:"one"|"all", role:string[]} StoredDesk */ +// +// /** @type {(id:string, name:string, data:StoredDesk)=>void} */ +// function CreateDesk(id, name, data, role, need, time, make, mode="one") +// { +// const need = Object.keys(data[0]) +// const time = Object.values(data[0]); +// const desk = {name, need, time, make, role, _:{id}}; +// Desk[id] = desk; +// need.forEach(partId => Part[partId]._.need.push(desk)); +// make.forEach(partId => Part[partId]._.make.push(desk)); +// +// const check = Range(need, make); +// console.log(check); +// +// return desk; +// } +// +// await DB(import.meta.resolve('./db_work.csv'), 3, (id, name, /**@type{StoredDesk}*/data)=>{ +// +// const need = Object.keys(data[0]) +// CreateDesk(id, name, need, data[1], ); +// }); +// +// +// +// console.log(Part); +// console.log(Desk); \ No newline at end of file diff --git a/db.js b/db.js index adf7e3a..57b0227 100644 --- a/db.js +++ b/db.js @@ -1,4 +1,4 @@ -function createCSVScanner(onRow, nonJsonCols) { +function createCSVScanner(onRow, cols) { let buffer = ''; return function scanChunk(chunk) { @@ -6,48 +6,22 @@ function createCSVScanner(onRow, nonJsonCols) { const lines = buffer.split('\n'); buffer = lines.pop(); // Save incomplete line - if(nonJsonCols !== 0) + for (const line of lines) { - for (const line of lines) - { - const fields = []; - let start = 0; - let commaCount = 0; - - for (let i = 0; i < line.length; i++) { - if (line[i] === ',' && commaCount < nonJsonCols) { - fields.push(line.slice(start, i)); - start = i + 1; - commaCount++; - } + const fields = []; + let start = 0; + let col = 0; + for (let i = 0; i < line.length; i++) { + if (line[i] === ',' && col < cols) { + fields.push(line.slice(start, i)); + start = i + 1; + col++; } - - const jsonStr = line.slice(start); - let jsonData = null; - try { - jsonData = JSON.parse(jsonStr); - } catch (_e) { - console.error('Invalid JSON:', jsonStr); - } - onRow(...fields, jsonData); } + fields.push(line.slice(start, line.length-1)); + onRow(...fields); } - else - { - for (const line of lines) - { - const fields = []; - let start = 0; - for (let i = 0; i < line.length; i++) { - if (line[i] === ',') { - fields.push(line.slice(start, i)); - start = i + 1; - } - } - fields.push(line.slice(start, line.length-1)) - onRow(...fields); - } - } + }; } diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..52d32e3 --- /dev/null +++ b/deno.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "checkJs": true + } +} \ No newline at end of file diff --git a/types.d.ts b/types.d.ts index e3853e8..08f199f 100644 --- a/types.d.ts +++ b/types.d.ts @@ -1,17 +1,15 @@ -export {}; - export type Row< L extends number, T extends any[] = [] > = T["length"] extends L ? T : Row; - -declare global { - namespace DB - { - type DB=(path:string, flat:Count, handler:(...args:[...Row, T])=>void)=>Promise - } -} - - - - +export type Builder = >(params: +{ + path:string, + cols:Count, + load:(commitLoad:(id:string, data:Loaded)=>Sized, ...args:Sized)=>[id:string, data:Loaded], + save:(commitSave:(...args:Sized)=>Sized, internal:Loaded) => Sized +}) => { + list:Record, + load:()=>Promise, + save:()=>Promise +} \ No newline at end of file