-
Notifications
You must be signed in to change notification settings - Fork 0
/
API endpoint.txt
47 lines (38 loc) · 1.14 KB
/
API endpoint.txt
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
const express = require('express');
const bodyParser = require('body-parser');
const DuckDB = require('duckdb');
const app = express();
const db = new DuckDB();
// parse application/json
app.use(bodyParser.json());
// endpoint to execute dynamic SQL query based on input criteria
app.post('/segment', async (req, res) => {
try {
const { criteria } = req.body;
// build dynamic SQL query based on input criteria
let sql = 'SELECT * FROM users';
let whereClause = '';
for (const key in criteria) {
if (criteria.hasOwnProperty(key)) {
if (whereClause === '') {
whereClause += ` WHERE ${key} = '${criteria[key]}'`;
} else {
whereClause += ` AND ${key} = '${criteria[key]}'`;
}
}
}
if (whereClause !== '') {
sql += whereClause;
}
// execute SQL query
const result = await db.query(sql);
// return result as JSON
res.json(result);
} catch (err) {
console.error(err);
res.status(500).send('Error executing query');
}
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});