sql-agent / src /pages /api /tables.ts
fullstuckdev's picture
Upload 23 files
645ad9d verified
raw
history blame
820 Bytes
import { NextApiRequest, NextApiResponse } from "next";
import { createConnection, getTables } from "@/utils/database";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Method not allowed" });
}
const { dbUri } = req.body;
if (!dbUri) {
return res.status(400).json({
message: "Database URI is required"
});
}
let connection;
try {
connection = await createConnection(dbUri);
const tables = await getTables(connection);
await connection.end();
return res.status(200).json({ tables });
} catch (error: any) {
await connection?.end();
return res.status(500).json({
message: "Failed to fetch tables",
details: error.message
});
}
}