sing-ui/app/api/connections/route.ts

40 lines
1.2 KiB
TypeScript

import { createConnection, listConnections, normalizeJsonColumn } from "@/lib/db";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
export async function GET() {
try {
return Response.json(listConnections());
} catch (err) {
return Response.json({ error: (err as Error).message }, { status: 500 });
}
}
export async function POST(request: Request) {
try {
const body = await request.json();
if (!body.name || !body.type || !body.host) {
return Response.json(
{ error: "name, type and host are required" },
{ status: 400 }
);
}
const conn = createConnection({
name: String(body.name),
type: String(body.type),
host: String(body.host),
port: Number(body.port ?? 3306),
user: String(body.user ?? ""),
password: String(body.password ?? ""),
database: String(body.database ?? ""),
params: normalizeJsonColumn(body.params),
});
return Response.json(conn, { status: 201 });
} catch (err) {
const message = (err as Error).message;
const status = message.includes("UNIQUE") ? 409 : 500;
return Response.json({ error: message }, { status });
}
}