@@ -16,7 +16,7 @@ router = APIRouter()
16
16
<%
17
17
// { "get", "/v1/categories/:categoryId" } => "get_v1_categories_category_id"
18
18
function generate_method_name (method , path ) {
19
- const name = camel2snakeCase (path).replace (/ \/ / g , ' _' ).replace (/ [^ _a-z0-9 ] / g , ' ' );
19
+ const name = camel2snakeCase (path).replace (/ \/ / g , ' _' ).replace (/ [^ _a-z0-9 ] / g , ' ' )
20
20
return ` ${ method}${ name} `
21
21
}
22
22
@@ -39,7 +39,7 @@ function formatQueryForPython(query, indentLevel) {
39
39
if (isMultilineSql) {
40
40
const indent = ' ' .repeat (indentLevel)
41
41
const indentedSql = sql .replace (/ \n / g , ' \n ' + indent)
42
- return ` \n ${ indent} """\n ${ indent}${ indentedSql} \n ${ indent} """` ;
42
+ return ` \n ${ indent} """\n ${ indent}${ indentedSql} \n ${ indent} """`
43
43
}
44
44
return ` "${ sql} "`
45
45
}
@@ -56,8 +56,8 @@ function formatQueryForPython(query, indentLevel) {
56
56
function extractInsertValues (queryAst ) {
57
57
const values = queryAst .values .flatMap (elem => elem .value )
58
58
.map (elem => elem .type === ' param' ? elem .value : null )
59
- .filter (elem => elem); // filter out nulls
60
- return Array .from (new Set (values));
59
+ .filter (elem => elem) // filter out nulls
60
+ return Array .from (new Set (values))
61
61
}
62
62
63
63
// LATER: reduce duplication with routes.go.ejs
@@ -79,22 +79,22 @@ function extractUpdateValues(queryAst) {
79
79
// LATER: reduce duplication with routes.go.ejs
80
80
function extractProperties (queryAst ) {
81
81
if (queryAst .type === ' insert' ) {
82
- return extractInsertValues (queryAst);
82
+ return extractInsertValues (queryAst)
83
83
}
84
84
if (queryAst .type === ' update' ) {
85
- return extractUpdateValues (queryAst);
85
+ return extractUpdateValues (queryAst)
86
86
}
87
- return [];
87
+ return []
88
88
}
89
89
90
90
// LATER: try to reduce duplication with routes.go.ejs
91
91
function findOutType (fieldsInfo , fieldName ) {
92
- const defaultType = ' str' ;
93
- const hasTypeInfo = fieldsInfo .hasOwnProperty (fieldName) && fieldsInfo[fieldName].hasOwnProperty (' type' );
92
+ const defaultType = ' str'
93
+ const hasTypeInfo = fieldsInfo .hasOwnProperty (fieldName) && fieldsInfo[fieldName].hasOwnProperty (' type' )
94
94
if (hasTypeInfo && fieldsInfo[fieldName].type === ' integer' ) {
95
- return ' int' ;
95
+ return ' int'
96
96
}
97
- return defaultType;
97
+ return defaultType
98
98
}
99
99
100
100
// LATER: reduce duplication with routes.go.ejs
@@ -104,24 +104,24 @@ function addTypes(props, fieldsInfo) {
104
104
" name" : prop,
105
105
" type" : findOutType (fieldsInfo, prop),
106
106
}
107
- });
107
+ })
108
108
}
109
109
110
110
// LATER: reduce duplication with routes.go.ejs
111
111
function query2dto (parser , method ) {
112
- const query = removePlaceholders (method .query );
113
- const queryAst = parser .astify (query);
114
- const props = extractProperties (queryAst);
112
+ const query = removePlaceholders (method .query )
113
+ const queryAst = parser .astify (query)
114
+ const props = extractProperties (queryAst)
115
115
if (props .length === 0 ) {
116
- console .warn (' Could not create DTO for query:' , formatQuery (query));
117
- console .debug (' Query AST:' );
118
- console .debug (queryAst);
119
- return null ;
116
+ console .warn (' Could not create DTO for query:' , formatQuery (query))
117
+ console .debug (' Query AST:' )
118
+ console .debug (queryAst)
119
+ return null
120
120
}
121
- const fieldsInfo = method .dto && method .dto .fields ? method .dto .fields : {};
122
- const propsWithTypes = addTypes (props, fieldsInfo);
123
- const hasName = method .dto && method .dto .name && method .dto .name .length > 0 ;
124
- const name = hasName ? method .dto .name : " Dto" + ++ globalDtoCounter;
121
+ const fieldsInfo = method .dto && method .dto .fields ? method .dto .fields : {}
122
+ const propsWithTypes = addTypes (props, fieldsInfo)
123
+ const hasName = method .dto && method .dto .name && method .dto .name .length > 0
124
+ const name = hasName ? method .dto .name : " Dto" + ++ globalDtoCounter
125
125
return {
126
126
" name" : name,
127
127
" hasUserProvidedName" : hasName,
@@ -130,34 +130,34 @@ function query2dto(parser, method) {
130
130
// [ {name:foo, type:int}, {name:bar, type:string} ] => "foo=int bar=string"
131
131
// LATER: sort before join
132
132
" signature" : propsWithTypes .map (field => ` ${ field .name } =${ field .type } ` ).join (' ' )
133
- };
133
+ }
134
134
}
135
135
136
136
// https://fastapi.tiangolo.com/tutorial/body/
137
137
function dto2model (dto ) {
138
- let result = ` class ${ dto .name } (BaseModel):\n ` ;
138
+ let result = ` class ${ dto .name } (BaseModel):\n `
139
139
dto .props .forEach (prop => {
140
140
result += ` ${ prop .name } : Optional[${ prop .type } ] = None\n `
141
- });
142
- return result;
141
+ })
142
+ return result
143
143
}
144
144
145
- let globalDtoCounter = 0 ;
146
- const dtoCache = {};
145
+ let globalDtoCounter = 0
146
+ const dtoCache = {}
147
147
148
148
// LATER: reduce duplication with routes.go.ejs
149
149
function cacheDto (dto ) {
150
- dtoCache[dto .signature ] = dto .name ;
151
- return dto;
150
+ dtoCache[dto .signature ] = dto .name
151
+ return dto
152
152
}
153
153
154
154
// LATER: reduce duplication with routes.go.ejs
155
155
function dtoInCache (dto ) {
156
156
// always prefer user specified name even when we have a similar DTO in cache
157
157
if (dto .hasUserProvidedName ) {
158
- return false ;
158
+ return false
159
159
}
160
- return dtoCache .hasOwnProperty (dto .signature );
160
+ return dtoCache .hasOwnProperty (dto .signature )
161
161
}
162
162
163
163
// Generate models
@@ -173,8 +173,8 @@ endpoints.forEach(function(endpoint) {
173
173
% >
174
174
< %- model -% >
175
175
< %
176
- });
177
- });
176
+ })
177
+ })
178
178
179
179
// Generate endpoints
180
180
endpoints .forEach (function (endpoint ) {
@@ -205,7 +205,7 @@ endpoints.forEach(function(endpoint) {
205
205
queriesWithNames .forEach (queryWithName => {
206
206
for (const [name , query ] of Object .entries (queryWithName)) {
207
207
const sql = convertToPsycopgNamedArguments (formatQueryForPython (query, 20 ))
208
- const params = extractParamsFromQuery (query);
208
+ const params = extractParamsFromQuery (query)
209
209
const formattedParams = formatParamsAsPythonDict (params)
210
210
queries .push ({ [name]: { sql : sql, formattedParams: formattedParams }})
211
211
}
@@ -255,13 +255,13 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
255
255
< %
256
256
}
257
257
if (method .name === ' post' ) {
258
- const dto = query2dto (sqlParser, method);
258
+ const dto = query2dto (sqlParser, method)
259
259
// LATER: do we really need signature and cache?
260
- const cacheKey = dto ? dto .signature : null ;
261
- const model = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
260
+ const cacheKey = dto ? dto .signature : null
261
+ const model = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name
262
262
263
263
const sql = convertToPsycopgNamedArguments (formatQueryForPython (method .query , 20 ))
264
- const params = extractParamsFromQuery (method .query );
264
+ const params = extractParamsFromQuery (method .query )
265
265
const formattedParams = formatParamsAsPythonDict (params)
266
266
267
267
% >
@@ -280,15 +280,15 @@ def <%- pythonMethodName %>(body: <%- model %>, conn=Depends(db_connection)):
280
280
}
281
281
if (method .name === ' put' ) {
282
282
// TODO: reduce duplication with POST
283
- const dto = query2dto (sqlParser, method);
283
+ const dto = query2dto (sqlParser, method)
284
284
// LATER: do we really need signature and cache?
285
- const cacheKey = dto ? dto .signature : null ;
286
- const model = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name ;
285
+ const cacheKey = dto ? dto .signature : null
286
+ const model = dtoInCache (dto) ? dtoCache[cacheKey] : dto .name
287
287
288
288
const methodArgs = [ ` body: ${ model} ` , ... argsFromPath, ' conn=Depends(db_connection)' ]
289
289
290
290
const sql = convertToPsycopgNamedArguments (formatQueryForPython (method .query , 20 ))
291
- const params = extractParamsFromQuery (method .query );
291
+ const params = extractParamsFromQuery (method .query )
292
292
const formattedParams = formatParamsAsPythonDict (params)
293
293
% >
294
294
0 commit comments