Skip to content

Commit

Permalink
feat: add API routes for retrieving DB data
Browse files Browse the repository at this point in the history
  • Loading branch information
mzrtamp committed Mar 6, 2024
1 parent dee2d2f commit e16b208
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/pages/api/columns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import connection from "../../index";
import { NextApiRequest, NextApiResponse } from "next";

export default async function columns(
req: NextApiRequest,
res: NextApiResponse
) {
const table = req.query["table"];
if (!table) {
res.status(400).json({ message: "Ngawur cik, mana nama tablenya???" });
return;
}

const columns = await connection.query({
query: "SHOW COLUMNS FROM ?",
values: [table]
});

res.status(200).json(columns);
}
20 changes: 20 additions & 0 deletions src/pages/api/retrieve-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import connection from "../../index";
import { NextApiRequest, NextApiResponse } from "next";

export default async function retrieveAll(
req: NextApiRequest,
res: NextApiResponse
) {
const table = req.query["table"];
if (!table) {
res.status(400).json({ message: "Ngawur cik, mana nama tablenya???" });
return;
}

const datas = await connection.query({
query: "SELECT * FROM ?",
values: [table]
});

res.status(200).json(datas);
}
10 changes: 10 additions & 0 deletions src/pages/api/tables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import connection from "../../index"; // ts server nya stress
import { NextApiRequest, NextApiResponse } from "next";

export default async function tables(
req: NextApiRequest,
res: NextApiResponse
) {
const query = await connection.query("SHOW TABLES");
res.status(200).json(query);
}

0 comments on commit e16b208

Please sign in to comment.