Files
portabase/app/api/v1/databases/[id]/route.ts
T

44 lines
1.3 KiB
TypeScript
Raw Normal View History

2026-05-25 15:09:44 +02:00
import { NextResponse } from "next/server";
2026-05-25 18:31:00 +02:00
import { withApiKey } from "@/lib/api-v1/middleware";
2026-05-25 15:09:44 +02:00
import { db } from "@/db";
import * as drizzleDb from "@/db";
2026-05-27 22:02:22 +02:00
import { and, eq, isNull } from "drizzle-orm";
2026-05-25 15:09:44 +02:00
import { logger } from "@/lib/logger";
2026-05-27 22:02:22 +02:00
import { ApiKeyContext } from "@/lib/api-v1/types";
import {requireDatabaseAccess} from "@/lib/api-v1/services/databases";
2026-05-25 15:09:44 +02:00
const log = logger.child({ module: "api/v1/databases/[id]" });
export const GET = withApiKey(
2026-05-27 22:02:22 +02:00
async (_req: Request, ctx: ApiKeyContext, params?: Record<string, string>) => {
try {
const guard = await requireDatabaseAccess(params, ctx.user);
2026-05-25 15:09:44 +02:00
2026-05-27 22:02:22 +02:00
if (!guard.ok) {
return guard.response;
}
const { id } = guard.data;
const database = await db.query.database.findFirst({
where: and(
eq(drizzleDb.schemas.database.id, id),
isNull(drizzleDb.schemas.database.deletedAt)
),
2026-05-25 15:09:44 +02:00
});
2026-05-27 22:02:22 +02:00
if (!database) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json({ data: database });
} catch (error) {
log.error({ error }, "Error in GET /api/v1/databases/[id]");
2026-05-25 15:09:44 +02:00
return NextResponse.json(
2026-05-27 22:02:22 +02:00
{ error: "Internal server error" },
{ status: 500 }
2026-05-25 15:09:44 +02:00
);
}
}
2026-05-27 22:02:22 +02:00
);