Skip to content

Commit e9b8d9d

Browse files
committedJul 15, 2020
feat: add support for DELETE requests
1 parent b74706f commit e9b8d9d

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed
 

‎README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
3535
UPDATE categories
3636
SET name = :name, name_ru = :nameRu, slug = :slug, updated_at = NOW(), updated_by = :userId
3737
WHERE id = :categoryId
38+
delete: DELETE FROM categories WHERE id = :categoryId
3839
```
3940

4041
1. Generate code
@@ -77,6 +78,11 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
7778
$ curl -i -H 'Content-Type: application/json' -d '{"name":"Fauna","nameRu":"Фауна","slug":"fauna","userId":101,"categoryId":1}' -X PUT http://localhost:3000/v1/categories/1
7879
HTTP/1.1 204 No Content
7980
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
80-
Date: Wed, 15 Jul 2020 18:30:36 GMT
81+
Date: Wed, 15 Jul 2020 18:06:34 GMT
82+
Connection: keep-alive
83+
$ curl -i -X DELETE http://localhost:3000/v1/categories/1
84+
HTTP/1.1 204 No Content
85+
ETag: W/"a-bAsFyilMr4Ra1hIU5PyoyFRunpI"
86+
Date: Wed, 15 Jul 2020 18:06:35 GMT
8187
Connection: keep-alive
8288
```

‎examples/js/app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ app.put('/v1/categories/:categoryId', (req, res) => {
8080
)
8181
})
8282

83+
app.delete('/v1/categories/:categoryId', (req, res) => {
84+
pool.query(
85+
'DELETE FROM categories WHERE id = :categoryId',
86+
{ "categoryId": req.params.categoryId },
87+
(err, rows, fields) => {
88+
if (err) {
89+
throw err
90+
}
91+
res.sendStatus(204)
92+
}
93+
)
94+
})
95+
8396
const port = process.env.PORT || 3000;
8497
app.listen(port, () => {
8598
console.log(`Listen on ${port}`)

‎examples/js/endpoints.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@
4040
, updated_at = NOW()
4141
, updated_by = :userId
4242
WHERE id = :categoryId
43+
delete: >-
44+
DELETE
45+
FROM categories
46+
WHERE id = :categoryId

‎src/cli.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const createEndpoints = async (destDir, fileName, config) => {
3838
if (endpoint.hasOwnProperty('put')) {
3939
console.log('PUT', endpoint.path, '=>', flattenQuery(endpoint.put));
4040
}
41+
if (endpoint.hasOwnProperty('delete')) {
42+
console.log('DELETE', endpoint.path, '=>', flattenQuery(endpoint.delete));
43+
}
4144
}
4245

4346
const resultedCode = await ejs.renderFile(

‎src/templates/app.js.ejs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ app.put('<%- endpoint.path %>', (req, res) => {
7575
}
7676
)
7777
})
78+
<%
79+
}
80+
if (endpoint.hasOwnProperty('delete')) {
81+
const params = extractParams(endpoint.delete);
82+
%>
83+
app.delete('<%- endpoint.path %>', (req, res) => {
84+
pool.query(
85+
<%- formatQuery(endpoint.delete) %>,<%- params.length > 0 ? '\n ' + formatParams(params, 'req.params') + ',' : '' %>
86+
(err, rows, fields) => {
87+
if (err) {
88+
throw err
89+
}
90+
res.sendStatus(204)
91+
}
92+
)
93+
})
7894
<%
7995
}
8096
});

0 commit comments

Comments
 (0)