-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.js
51 lines (45 loc) · 1.22 KB
/
query.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const createClient = require("./queries/create-client");
const { validateId } = require("./queries/id");
const dbExists = require("./queries/db-exists");
const touchLogin = require("./queries/touch-login");
module.exports = async (req, res) => {
const { id } = req.session;
console.log("Session:", id);
if (!id) {
return res.status(400).json({ error: "Session ID is missing" });
}
validateId(id);
if (!(await dbExists(id))) {
throw new Error("No database found. Reload this page.");
}
await touchLogin(id);
const client = await createClient(id);
try {
console.log("Query:", req.body.query);
const result = await client.query(req.body.query);
if (Array.isArray(result)) {
res.send(
result.map(({ rows, fields, command, rowCount }) => ({
command,
rows,
fields,
rowCount,
}))
);
} else {
res.send([
{
command: result.command,
rows: result.rows,
fields: result.fields,
rowCount: result.rowCount,
},
]);
}
} catch (err) {
console.error("Query Execution Error:", err);
res.status(500).json({ error: err.message });
} finally {
await client.end();
}
};