KV-store backed Express 5 app for tracking electronic components, their arbitrary fields, and inventory locations (physical, BOM, digital). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
83 lines
1.7 KiB
JavaScript
83 lines
1.7 KiB
JavaScript
import { mkdirSync } from 'node:fs';
|
|
import { Simple_KeyValue_Store } from './kv-store.mjs';
|
|
|
|
mkdirSync('./data', { recursive: true });
|
|
|
|
const store = new Simple_KeyValue_Store('./data/inventory.ndjson', {
|
|
auto_load: true,
|
|
auto_store: true,
|
|
debounce_flush_timeout: 5000,
|
|
});
|
|
|
|
// --- Field definitions ---
|
|
|
|
export function list_fields() {
|
|
const result = [];
|
|
for (const [key] of store.data.entries()) {
|
|
if (key.startsWith('f:')) {
|
|
result.push(store.get(key));
|
|
}
|
|
}
|
|
return result.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
export function get_field(id) {
|
|
return store.get(`f:${id}`) ?? null;
|
|
}
|
|
|
|
export function set_field(field) {
|
|
store.set(`f:${field.id}`, field);
|
|
}
|
|
|
|
export function delete_field(id) {
|
|
return store.delete(`f:${id}`);
|
|
}
|
|
|
|
// --- Components ---
|
|
|
|
export function list_components() {
|
|
const result = [];
|
|
for (const [key] of store.data.entries()) {
|
|
if (key.startsWith('c:')) {
|
|
result.push(store.get(key));
|
|
}
|
|
}
|
|
return result.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
export function get_component(id) {
|
|
return store.get(`c:${id}`) ?? null;
|
|
}
|
|
|
|
export function set_component(component) {
|
|
store.set(`c:${component.id}`, component);
|
|
}
|
|
|
|
export function delete_component(id) {
|
|
return store.delete(`c:${id}`);
|
|
}
|
|
|
|
// --- Inventory entries ---
|
|
|
|
export function list_inventory() {
|
|
const result = [];
|
|
for (const [key] of store.data.entries()) {
|
|
if (key.startsWith('i:')) {
|
|
result.push(store.get(key));
|
|
}
|
|
}
|
|
return result.sort((a, b) => a.created_at - b.created_at);
|
|
}
|
|
|
|
export function get_inventory_entry(id) {
|
|
return store.get(`i:${id}`) ?? null;
|
|
}
|
|
|
|
export function set_inventory_entry(entry) {
|
|
store.set(`i:${entry.id}`, entry);
|
|
}
|
|
|
|
export function delete_inventory_entry(id) {
|
|
return store.delete(`i:${id}`);
|
|
}
|