Fastify API for storing third-party integration credentials (Newbook, Resos, Nextcloud, Azure, SambaPOS) with AES-256-GCM encryption for sensitive fields. Includes Newbook room sync endpoint and global_config store for shared app data. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
33 lines
1 KiB
JavaScript
33 lines
1 KiB
JavaScript
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]
|
|
)
|
|
}
|
|
}
|