HB_DB_DRIVER=mongo|postgres, versionierte Migrationen beim Start, Mongo-Backend ruft unverändert die Upstream-Modelle. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
import { createDb, checkServerVersion, migrateToLatest } from './db.js';
|
|
import { createBrewRepository } from './brews.js';
|
|
import { createNotificationRepository } from './notifications.js';
|
|
import { createUserRepository } from './users.js';
|
|
|
|
// PostgreSQL-Implementierung der Storage-Schicht (ADR 0002). Kysely + pg, versionierte Migrationen
|
|
// laufen beim Start (migrateToLatest). Konfiguration: HB_POSTGRES_URL, HB_POSTGRES_POOL_MAX.
|
|
export const createPostgresStorage = (config, overrides = {})=>{
|
|
let handle;
|
|
let ready = false;
|
|
const repos = {};
|
|
|
|
const build = (db)=>{
|
|
repos.brews = createBrewRepository(db);
|
|
repos.notifications = createNotificationRepository(db);
|
|
repos.users = createUserRepository(db);
|
|
};
|
|
|
|
return {
|
|
driver : 'postgres',
|
|
get brews() { return repos.brews; },
|
|
get notifications() { return repos.notifications; },
|
|
get users() { return repos.users; },
|
|
async connect() {
|
|
const url = overrides.url ?? config?.get('hb_postgres_url');
|
|
if(!url) throw new Error('HB_DB_DRIVER=postgres, aber HB_POSTGRES_URL ist nicht gesetzt (z. B. postgres://hb:…@postgres:5432/homebrewery)');
|
|
const poolMax = parseInt(overrides.poolMax ?? config?.get('hb_postgres_pool_max'), 10) || 10;
|
|
handle = createDb({ url, poolMax, searchPath: overrides.searchPath });
|
|
handle.pool.on('error', (err)=>{ ready = false; console.error('PostgreSQL-Pool-Fehler:', err.message); });
|
|
await checkServerVersion(handle.pool);
|
|
await migrateToLatest(handle.db);
|
|
build(handle.db);
|
|
ready = true;
|
|
},
|
|
async disconnect() {
|
|
ready = false;
|
|
await handle?.db.destroy();
|
|
handle = undefined;
|
|
},
|
|
isReady() {
|
|
return ready;
|
|
}
|
|
};
|
|
};
|