-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (40 loc) · 1.34 KB
/
app.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 express = require("express");
const app = express();
const { Pool, Client } = require("pg");
let config;
try { config = require("./config.js"); } catch(e) {}
const pool = new Pool(config);
app.use(express.json());
app.post("/", (req, res) => {
let queries = [].concat(req.body);
if(queries.length) {
(async () => {
let client = await pool.connect();
try {
await client.query("BEGIN");
let results = await Promise.all(queries.map(query => {
if(typeof query === "object") {
return client.query(query.query, query.values || []);
}
return client.query(query);
}));
await client.query("COMMIT");
res.json(
results.map(result => ({
rowCount : result.rowCount,
rows : result.rows
}))
);
} catch(e) {
await client.query("ROLLBACK");
throw e;
} finally {
client.release();
}
})().catch(e => {
console.error(e.stack);
res.json({ error : e.message });
})
}
});
app.listen(3000, () => console.log("Listening on port 3000"));