@@ -6,14 +6,14 @@ from fastapi import APIRouter, Depends, HTTPException
6
6
7
7
router = APIRouter()
8
8
9
+
9
10
async def db_connection():
10
11
return psycopg2.connect(
11
- database = os.getenv('DB_NAME'),
12
- user = os.getenv('DB_USER'),
13
- password = os.getenv('DB_PASSWORD'),
14
- host = os.getenv('DB_HOST', 'localhost'),
15
- port = 5432)
16
-
12
+ database=os.getenv('DB_NAME'),
13
+ user=os.getenv('DB_USER'),
14
+ password=os.getenv('DB_PASSWORD'),
15
+ host=os.getenv('DB_HOST', 'localhost'),
16
+ port=5432)
17
17
<%
18
18
// { "get", "/v1/categories/:categoryId" } => "get_v1_categories_category_id"
19
19
function generate_method_name (method , path ) {
@@ -57,7 +57,7 @@ endpoints.forEach(function(endpoint) {
57
57
58
58
// LATER: add support for aggregated_queries (#17)
59
59
const argsFromQuery = method .query ? extractParamsFromQuery (method .query ).map (stipOurPrefixes) : []
60
- const methodArgs = Array .from (new Set ([... argsFromPath, ... argsFromQuery, ' conn = Depends(db_connection)' ]))
60
+ const methodArgs = Array .from (new Set ([... argsFromPath, ... argsFromQuery, ' conn= Depends(db_connection)' ]))
61
61
62
62
const queriesWithNames = []
63
63
if (method .query ) {
@@ -75,21 +75,22 @@ endpoints.forEach(function(endpoint) {
75
75
const params = extractParamsFromQuery (query);
76
76
const formattedParams = params .length > 0
77
77
// [ "p.categoryId" ] => [ '"categoryId": categoryId' ]
78
- ? ' , { ' + params .map (param => param .substring (2 )).map (param => ` "${ param} ": ${ param} ` ).join (' , ' ) + ' }'
78
+ ? ' , {' + params .map (param => param .substring (2 )).map (param => ` "${ param} ": ${ param} ` ).join (' , ' ) + ' }'
79
79
: ' '
80
80
queries .push ({ [name]: { sql : sql, formattedParams: formattedParams }})
81
81
}
82
82
})
83
83
84
84
if (hasGetOne || hasGetMany) {
85
85
% >
86
+
86
87
@router .get (' <%- path %>' )
87
88
def < %- pythonMethodName % > (< %- methodArgs .join (' , ' ) % > ):
88
89
try:
89
90
with conn:
90
91
< % if (hasGetOne) {
91
92
if (queries .length > 1 ) { /* we can omit cursor_factory but in this case we might get an unused import */ -% >
92
- with conn .cursor (cursor_factory = psycopg2 .extras .DictCursor ) as cur:
93
+ with conn .cursor (cursor_factory= psycopg2 .extras .DictCursor ) as cur:
93
94
result = {}
94
95
< % queries .forEach (queryInfo => {
95
96
for (const [name , query ] of Object .entries (queryInfo)) {
@@ -104,7 +105,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
104
105
} else {
105
106
const query = queries[0 ].result
106
107
-% >
107
- with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
108
+ with conn .cursor (cursor_factory= psycopg2 .extras .RealDictCursor ) as cur:
108
109
cur .execute (< %- query .sql % >< %- query .formattedParams % > )
109
110
result = cur .fetchone ()
110
111
if result is None:
@@ -114,7 +115,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
114
115
} else {
115
116
const query = queries[0 ].result
116
117
-% >
117
- with conn .cursor (cursor_factory = psycopg2 .extras .RealDictCursor ) as cur:
118
+ with conn .cursor (cursor_factory= psycopg2 .extras .RealDictCursor ) as cur:
118
119
cur .execute (< %- query .sql % >< %- query .formattedParams % > )
119
120
return cur .fetchall ()
120
121
< %
@@ -126,6 +127,7 @@ def <%- pythonMethodName %>(<%- methodArgs.join(', ') %>):
126
127
}
127
128
if (method .name === ' post' ) {
128
129
% >
130
+
129
131
@router .post (' <%- path %>' )
130
132
def < %- pythonMethodName % > ():
131
133
pass
@@ -134,6 +136,7 @@ def <%- pythonMethodName %>():
134
136
}
135
137
if (method .name === ' put' ) {
136
138
% >
139
+
137
140
@router .put (' <%- path %>' )
138
141
def < %- pythonMethodName % > ():
139
142
pass
@@ -142,6 +145,7 @@ def <%- pythonMethodName %>():
142
145
}
143
146
if (method .name === ' delete' ) {
144
147
% >
148
+
145
149
@router .delete (' <%- path %>' )
146
150
def < %- pythonMethodName % > ():
147
151
pass
@@ -150,4 +154,4 @@ def <%- pythonMethodName %>():
150
154
}
151
155
})
152
156
})
153
- %>
157
+ %>
0 commit comments