import pg from 'pg' import { INTEGRATIONS } from './integrations/schema.js' const { Pool } = pg export const pool = new Pool({ connectionString: process.env.DATABASE_URL }) export async function initDb() { await pool.query(` CREATE TABLE IF NOT EXISTS integrations ( slug TEXT PRIMARY KEY, name TEXT NOT NULL, config JSONB NOT NULL DEFAULT '{}', secrets TEXT, enabled BOOLEAN NOT NULL DEFAULT true, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); CREATE TABLE IF NOT EXISTS global_config ( key TEXT PRIMARY KEY, value JSONB, updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() ); `) // Seed known integrations (idempotent — never overwrites existing config/secrets) const slugs = Object.entries(INTEGRATIONS).map(([slug, { name }]) => ({ slug, name })) for (const { slug, name } of slugs) { await pool.query( `INSERT INTO integrations (slug, name) VALUES ($1, $2) ON CONFLICT (slug) DO NOTHING`, [slug, name] ) } }