Skip to content

Commit 44c061d

Browse files
committed
feat: add get_list verb to support GET with a list of objects
See also #7
1 parent dea26f5 commit 44c061d

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

examples/js/app.js

+12
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ app.get('/v1/collections/:collectionId/categories/count', (req, res) => {
5959
)
6060
})
6161

62+
app.get('/v1/categories', (req, res) => {
63+
pool.query(
64+
'SELECT id , name , name_ru , slug FROM categories',
65+
(err, rows, fields) => {
66+
if (err) {
67+
throw err
68+
}
69+
res.json(rows)
70+
}
71+
)
72+
})
73+
6274
app.post('/v1/categories', (req, res) => {
6375
pool.query(
6476
'INSERT INTO categories ( name , name_ru , slug , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :nameRu , :slug , NOW() , :userId , NOW() , :userId )',

examples/js/endpoints.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
WHERE cs.collection_id = :collectionId
1111
1212
- path: /v1/categories
13+
get_list: >-
14+
SELECT id
15+
, name
16+
, name_ru
17+
, slug
18+
FROM categories
1319
post: >-
1420
INSERT
1521
INTO categories

src/cli.js

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const createEndpoints = async (destDir, fileName, config) => {
3131
for (let endpoint of config) {
3232
if (endpoint.hasOwnProperty('get')) {
3333
console.log('GET', endpoint.path, '=>', flattenQuery(endpoint.get));
34+
} else if (endpoint.hasOwnProperty('get_list')) {
35+
console.log('GET', endpoint.path, '=>', flattenQuery(endpoint.get_list));
3436
}
3537
if (endpoint.hasOwnProperty('post')) {
3638
console.log('POST', endpoint.path, '=>', flattenQuery(endpoint.post));

src/templates/app.js.ejs

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,28 @@ const pool = mysql.createPool({
2727

2828
<%
2929
endpoints.forEach(function(endpoint) {
30-
if (endpoint.hasOwnProperty('get')) {
31-
const params = extractParams(endpoint.get);
30+
const hasGetOne = endpoint.hasOwnProperty('get');
31+
const hasGetMany = endpoint.hasOwnProperty('get_list');
32+
if (hasGetOne || hasGetMany) {
33+
const sql = hasGetOne ? endpoint.get : endpoint.get_list;
34+
const params = extractParams(sql);
3235
%>
3336
app.get('<%- endpoint.path %>', (req, res) => {
3437
pool.query(
35-
<%- formatQuery(endpoint.get) %>,<%- params.length > 0 ? '\n ' + formatParams(params, 'req.params') + ',' : '' %>
38+
<%- formatQuery(sql) %>,<%- params.length > 0 ? '\n ' + formatParams(params, 'req.params') + ',' : '' %>
3639
(err, rows, fields) => {
3740
if (err) {
3841
throw err
3942
}
43+
<% if (hasGetMany) { -%>
44+
res.json(rows)
45+
<% } else { -%>
4046
if (rows.length === 0) {
4147
res.type('application/json').status(404).end()
4248
return
4349
}
4450
res.json(rows[0])
51+
<% } -%>
4552
}
4653
)
4754
})

0 commit comments

Comments
 (0)