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
This commit is contained in:
46
src/db/memory-store.ts
Normal file
46
src/db/memory-store.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user