Skip to content

Commit 96f6b62

Browse files
committed
feat(python): extract database initialisation logic from routes.py to db.py
Fix #28
1 parent 3799485 commit 96f6b62

File tree

6 files changed

+41
-22
lines changed

6 files changed

+41
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
4949
| -----------| ----------------------------| ---------------------------| --------- |
5050
| JavaScript | `npx query2app --lang js` | [`app.js`](examples/js/app.js)<br/>[`routes.js`](examples/js/routes.js)<br/>[`package.json`](examples/js/package.json) | Web: [`express`](https://www.npmjs.com/package/express), [`body-parser`](https://www.npmjs.com/package/body-parser)<br>Database: [`mysql`](https://www.npmjs.com/package/mysql) |
5151
| Golang | `npx query2app --lang go` | [`app.go`](examples/go/app.go)<br/>[`routes.go`](examples/go/routes.go)<br/>[`go.mod`](examples/go/go.mod) | Web: [`go-chi/chi`](https://github.com/go-chi/chi)<br/>Database: [`go-sql-driver/mysql`](https://github.com/go-sql-driver/mysql), [`jmoiron/sqlx`](https://github.com/jmoiron/sqlx) |
52-
| Python | `npx query2app --lang python` | [`app.py`](examples/python/app.py)<br/>[`routes.py`](examples/python/routes.py)<br/>[`requirements.txt`](examples/python/requirements.txt) | Web: [FastAPI](https://github.com/tiangolo/fastapi), [Uvicorn](https://www.uvicorn.org)<br/>Database: [psycopg2](https://pypi.org/project/psycopg2/) |
52+
| Python | `npx query2app --lang python` | [`app.py`](examples/python/app.py)<br/>[`db.py`](examples/python/db.py)<br/>[`routes.py`](examples/python/routes.py)<br/>[`requirements.txt`](examples/python/requirements.txt) | Web: [FastAPI](https://github.com/tiangolo/fastapi), [Uvicorn](https://www.uvicorn.org)<br/>Database: [psycopg2](https://pypi.org/project/psycopg2/) |
5353

5454
1. Run the application
5555
| Language | Commands to run the application |

examples/python/db.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import psycopg2
3+
4+
5+
async def db_connection():
6+
return psycopg2.connect(
7+
database=os.getenv('DB_NAME'),
8+
user=os.getenv('DB_USER'),
9+
password=os.getenv('DB_PASSWORD'),
10+
host=os.getenv('DB_HOST', 'localhost'),
11+
port=5432)

examples/python/routes.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import os
21
import psycopg2
32
import psycopg2.extras
43

54
from fastapi import APIRouter, Depends, HTTPException
65

7-
router = APIRouter()
8-
6+
from db import db_connection
97

10-
async def db_connection():
11-
return psycopg2.connect(
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)
8+
router = APIRouter()
179

1810

1911
@router.get('/v1/categories/count')

src/cli.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,21 @@ const createApp = async (destDir, lang) => {
8888
console.log('Generate', fileName);
8989
const resultFile = path.join(destDir, fileName);
9090

91-
fs.copyFileSync(`${__dirname}/templates/app.${ext}`, resultFile)
91+
fs.copyFileSync(`${__dirname}/templates/${fileName}`, resultFile)
9292
};
9393

94+
const createDb = async (destDir, lang) => {
95+
if (lang !== 'python') {
96+
return
97+
}
98+
const fileName = 'db.py'
99+
console.log('Generate', fileName);
100+
const resultFile = path.join(destDir, fileName);
101+
102+
// @todo #28 Create db.py with async API
103+
fs.copyFileSync(`${__dirname}/templates/${fileName}`, resultFile)
104+
}
105+
94106
// "-- comment\nSELECT * FROM foo" => "SELECT * FROM foo"
95107
const removeComments = (query) => query.replace(/--.*\n/g, '');
96108

@@ -330,6 +342,7 @@ if (!fs.existsSync(destDir)) {
330342
}
331343

332344
createApp(destDir, argv.lang, config);
345+
createDb(destDir, argv.lang)
333346
createEndpoints(destDir, argv.lang, config);
334347
createDependenciesDescriptor(destDir, argv.lang);
335348
showInstructions(argv.lang);

src/templates/db.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
import psycopg2
3+
4+
5+
async def db_connection():
6+
return psycopg2.connect(
7+
database=os.getenv('DB_NAME'),
8+
user=os.getenv('DB_USER'),
9+
password=os.getenv('DB_PASSWORD'),
10+
host=os.getenv('DB_HOST', 'localhost'),
11+
port=5432)

src/templates/routes.py.ejs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
1-
import os
21
import psycopg2
32
import psycopg2.extras
43

54
from fastapi import APIRouter, Depends, HTTPException
65

7-
router = APIRouter()
8-
6+
from db import db_connection
97

10-
async def db_connection():
11-
return psycopg2.connect(
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)
8+
router = APIRouter()
179
<%
1810
// { "get", "/v1/categories/:categoryId" } => "get_v1_categories_category_id"
1911
function generate_method_name(method, path) {

0 commit comments

Comments
 (0)