Skip to content

Commit f408f12

Browse files
committed
feat(ts): keep formatting and indentation of the multiline SQL queries
Part of #26
1 parent 0fac891 commit f408f12

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

examples/ts/express/mysql/routes.ts

+57-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const register = (app: Express, pool: Pool) => {
2525

2626
app.get('/v1/collections/:collectionId/categories/count', (req: Request, res: Response, next: NextFunction) => {
2727
pool.query(
28-
'SELECT COUNT(DISTINCT s.category_id) AS counter FROM collections_series cs JOIN series s ON s.id = cs.series_id WHERE cs.collection_id = :collectionId',
28+
`SELECT COUNT(DISTINCT s.category_id) AS counter
29+
FROM collections_series cs
30+
JOIN series s
31+
ON s.id = cs.series_id
32+
WHERE cs.collection_id = :collectionId`,
2933
{ "collectionId": req.params.collectionId },
3034
(err, rows, fields) => {
3135
if (err) {
@@ -42,7 +46,12 @@ const register = (app: Express, pool: Pool) => {
4246

4347
app.get('/v1/categories', (req: Request, res: Response, next: NextFunction) => {
4448
pool.query(
45-
'SELECT id , name , name_ru , slug , hidden FROM categories',
49+
`SELECT id
50+
, name
51+
, name_ru
52+
, slug
53+
, hidden
54+
FROM categories`,
4655
(err, rows, fields) => {
4756
if (err) {
4857
return next(err)
@@ -54,7 +63,27 @@ const register = (app: Express, pool: Pool) => {
5463

5564
app.post('/v1/categories', (req: Request, res: Response, next: NextFunction) => {
5665
pool.query(
57-
'INSERT INTO categories ( name , name_ru , slug , hidden , created_at , created_by , updated_at , updated_by ) VALUES ( :name , :name_ru , :slug , :hidden , NOW() , :user_id , NOW() , :user_id )',
66+
`INSERT
67+
INTO categories
68+
( name
69+
, name_ru
70+
, slug
71+
, hidden
72+
, created_at
73+
, created_by
74+
, updated_at
75+
, updated_by
76+
)
77+
VALUES
78+
( :name
79+
, :name_ru
80+
, :slug
81+
, :hidden
82+
, NOW()
83+
, :user_id
84+
, NOW()
85+
, :user_id
86+
)`,
5887
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "hidden": req.body.hidden, "user_id": req.body.user_id },
5988
(err, rows, fields) => {
6089
if (err) {
@@ -67,7 +96,13 @@ const register = (app: Express, pool: Pool) => {
6796

6897
app.get('/v1/categories/search', (req: Request, res: Response, next: NextFunction) => {
6998
pool.query(
70-
'SELECT id , name , name_ru , slug , hidden FROM categories WHERE hidden = :hidden',
99+
`SELECT id
100+
, name
101+
, name_ru
102+
, slug
103+
, hidden
104+
FROM categories
105+
WHERE hidden = :hidden`,
71106
{ "hidden": parseBoolean(req.query.hidden) },
72107
(err, rows, fields) => {
73108
if (err) {
@@ -80,7 +115,13 @@ const register = (app: Express, pool: Pool) => {
80115

81116
app.get('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
82117
pool.query(
83-
'SELECT id , name , name_ru , slug , hidden FROM categories WHERE id = :categoryId',
118+
`SELECT id
119+
, name
120+
, name_ru
121+
, slug
122+
, hidden
123+
FROM categories
124+
WHERE id = :categoryId`,
84125
{ "categoryId": req.params.categoryId },
85126
(err, rows, fields) => {
86127
if (err) {
@@ -97,7 +138,14 @@ const register = (app: Express, pool: Pool) => {
97138

98139
app.put('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
99140
pool.query(
100-
'UPDATE categories SET name = :name , name_ru = :name_ru , slug = :slug , hidden = :hidden , updated_at = NOW() , updated_by = :user_id WHERE id = :categoryId',
141+
`UPDATE categories
142+
SET name = :name
143+
, name_ru = :name_ru
144+
, slug = :slug
145+
, hidden = :hidden
146+
, updated_at = NOW()
147+
, updated_by = :user_id
148+
WHERE id = :categoryId`,
101149
{ "name": req.body.name, "name_ru": req.body.name_ru, "slug": req.body.slug, "hidden": req.body.hidden, "user_id": req.body.user_id, "categoryId": req.params.categoryId },
102150
(err, rows, fields) => {
103151
if (err) {
@@ -110,7 +158,9 @@ const register = (app: Express, pool: Pool) => {
110158

111159
app.delete('/v1/categories/:categoryId', (req: Request, res: Response, next: NextFunction) => {
112160
pool.query(
113-
'DELETE FROM categories WHERE id = :categoryId',
161+
`DELETE
162+
FROM categories
163+
WHERE id = :categoryId`,
114164
{ "categoryId": req.params.categoryId },
115165
(err, rows, fields) => {
116166
if (err) {

src/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
261261

262262
// Differs from formatQuery() as it doesn't flatten query (preserve original formatting)
263263
// and also use backticks for multiline strings
264-
// (used only with JS)
264+
// (used only with JS, TS)
265265
"formatQueryForJs": (query, indentLevel) => {
266266
const sql = removePlaceholders(removeComments(query))
267267
const indent = ' '.repeat(indentLevel)

src/templates/routes.ts.ejs

+5-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endpoints.forEach(function(endpoint) {
1818
}
1919
const hasGetOne = method.name === 'get'
2020
const hasGetMany = method.name === 'get_list'
21-
const sql = formatQuery(method.query)
21+
const sql = formatQueryForJs(method.query, 12)
2222
const params = extractParamsFromQuery(method.query)
2323
const formattedParams = params.length > 0
2424
? '\n { ' + formatParamsAsJavaScriptObject(params, method) + ' },'
@@ -27,8 +27,7 @@ endpoints.forEach(function(endpoint) {
2727
if (hasGetOne || hasGetMany) {
2828
%>
2929
app.get('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
30-
pool.query(
31-
'<%- sql %>',<%- formattedParams %>
30+
pool.query(<%- sql %>,<%- formattedParams %>
3231
(err, rows, fields) => {
3332
if (err) {
3433
return next(err)
@@ -50,8 +49,7 @@ endpoints.forEach(function(endpoint) {
5049
if (method.name === 'post') {
5150
%>
5251
app.post('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
53-
pool.query(
54-
'<%- sql %>',<%- formattedParams %>
52+
pool.query(<%- sql %>,<%- formattedParams %>
5553
(err, rows, fields) => {
5654
if (err) {
5755
return next(err)
@@ -65,8 +63,7 @@ endpoints.forEach(function(endpoint) {
6563
if (method.name === 'put') {
6664
%>
6765
app.put('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
68-
pool.query(
69-
'<%- sql %>',<%- formattedParams %>
66+
pool.query(<%- sql %>,<%- formattedParams %>
7067
(err, rows, fields) => {
7168
if (err) {
7269
return next(err)
@@ -80,8 +77,7 @@ endpoints.forEach(function(endpoint) {
8077
if (method.name === 'delete') {
8178
%>
8279
app.delete('<%- path %>', (req: Request, res: Response, next: NextFunction) => {
83-
pool.query(
84-
'<%- sql %>',<%- formattedParams %>
80+
pool.query(<%- sql %>,<%- formattedParams %>
8581
(err, rows, fields) => {
8682
if (err) {
8783
return next(err)

0 commit comments

Comments
 (0)