Files
pmp-tool/src/db/memory-store.ts
xiaohei 15a70e4ecf
Some checks failed
CI / lint-and-typecheck (push) Failing after 16m21s
CI / test (push) Has been skipped
CI / build (push) Has been skipped
feat: database layer - PostgreSQL schema + memory fallback
- drizzle-orm + postgres dependencies
- Full schema: 12 tables covering all modules
- Graceful fallback: no DATABASE_URL → memory mode
- drizzle-kit config for migrations
- Memory store as generic CRUD layer
- dev.ts auto-initializes DB on startup
2026-04-12 19:05:03 +08:00

47 lines
1.2 KiB
TypeScript

/**
* Generic in-memory store (fallback when no PostgreSQL)
*/
type Store = Map<string, any>;
const stores = new Map<string, Store>();
function getStore(table: string): Store {
if (!stores.has(table)) stores.set(table, new Map());
return stores.get(table)!;
}
export const memoryDB = {
create(table: string, id: string, data: any): any {
const store = getStore(table);
const record = { ...data, id };
store.set(id, record);
return record;
},
getById(table: string, id: string): any | undefined {
return getStore(table).get(id);
},
list(table: string, filter?: (item: any) => boolean): any[] {
const items = Array.from(getStore(table).values());
return filter ? items.filter(filter) : items;
},
update(table: string, id: string, data: Partial<any>): any | null {
const store = getStore(table);
const existing = store.get(id);
if (!existing) return null;
const updated = { ...existing, ...data };
store.set(id, updated);
return updated;
},
delete(table: string, id: string): boolean {
return getStore(table).delete(id);
},
count(table: string, filter?: (item: any) => boolean): number {
return this.list(table, filter).length;
},
};