Files
source-gamepanel/apps/api/src/plugins/db.ts
T
hibna c1adb94abb Format the repository with Prettier
CI has been running `prettier --check` against a tree that was never
formatted, so the check reported 63 files and failed every run. Nothing
here is a behaviour change: `pnpm lint` and the four typecheck builds
pass exactly as before.

conduit-bringup-artifacts is added to .prettierignore instead. Those
files are captured bring-up reports, not maintained sources; reflowing
them would only churn a record of what happened.
2026-08-02 21:08:12 +03:00

46 lines
1.3 KiB
TypeScript

import fp from 'fastify-plugin';
import type { FastifyInstance } from 'fastify';
import { createDb, type Database } from '@source/database';
import { sql } from 'drizzle-orm';
declare module 'fastify' {
interface FastifyInstance {
db: Database;
}
}
export default fp(async (app: FastifyInstance) => {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error('DATABASE_URL environment variable is required');
}
const db = createDb(databaseUrl);
app.decorate('db', db);
await db.execute(
sql.raw(`
CREATE TABLE IF NOT EXISTS server_databases (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
server_id uuid NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
name varchar(255) NOT NULL,
database_name varchar(255) NOT NULL UNIQUE,
username varchar(64) NOT NULL UNIQUE,
password text NOT NULL,
host varchar(255) NOT NULL,
port integer NOT NULL,
phpmyadmin_url text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
)
`),
);
await db.execute(
sql.raw(
'CREATE INDEX IF NOT EXISTS server_databases_server_id_idx ON server_databases(server_id)',
),
);
app.log.info('Database connected');
});