Skip to content

Commit

Permalink
chore(python): implement logic for accessing PostgreSQL and make a si…
Browse files Browse the repository at this point in the history
…mplest GET query work

Part of #16
Relate to #15
  • Loading branch information
php-coder committed Sep 16, 2022
1 parent 4a9a191 commit 6be61e5
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
| -----------| ----------------------------| ---------------------------| --------- |
| 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) |
| 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) |
| 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) |
| 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: [](https://pypi.org/project/psycopg2/) |

1. Run the application
| Language | Commands to run the application |
| -----------| --------------------------------|
| JavaScript | <pre>$ npm install<br/>$ export DB_NAME=my-db DB_USER=my-user DB_PASSWORD=my-password<br/>$ npm start</pre> |
| Golang | <pre>$ go run *.go</pre>or<pre>$ go build -o app<br/>$ ./app</pre> |
| Python | <pre>$ pip install -r requirements.txt<br/>$ uvicorn app:app</pre> |
| Python | <pre>$ pip install -r requirements.txt<br/>$ export DB_NAME=my-db DB_USER=my-user DB_PASSWORD=my-password<br/>$ uvicorn app:app</pre> |
---
:bulb: **NOTE**
Expand Down
1 change: 1 addition & 0 deletions examples/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fastapi===0.83.0
uvicorn==0.18.3
psycopg2-binary==2.9.3
45 changes: 42 additions & 3 deletions examples/python/routes.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
import os
import psycopg2

from fastapi import APIRouter

router = APIRouter()


@router.get('/v1/categories/count')
def get_v1_categories_count():
pass
conn = psycopg2.connect(
database = os.getenv('DB_NAME'),
user = os.getenv('DB_USER'),
password = os.getenv('DB_PASSWORD'),
host = os.getenv('DB_HOST', 'localhost'),
port = 5432)
try:
with conn:
with conn.cursor() as cur:
cur.execute('SELECT COUNT(*) AS counter FROM categories')
return cur.fetchone()[0]
finally:
conn.close()

@router.get('/v1/collections/:collectionId/categories/count')
def get_v1_collections_collection_id_categories_count():
pass
conn = psycopg2.connect(
database = os.getenv('DB_NAME'),
user = os.getenv('DB_USER'),
password = os.getenv('DB_PASSWORD'),
host = os.getenv('DB_HOST', 'localhost'),
port = 5432)
try:
with conn:
with conn.cursor() as cur:
cur.execute('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')
return cur.fetchone()[0]
finally:
conn.close()

@router.get('/v1/categories')
def get_list_v1_categories():
Expand All @@ -21,7 +48,19 @@ def post_v1_categories():

@router.get('/v1/categories/:categoryId')
def get_v1_categories_category_id():
pass
conn = psycopg2.connect(
database = os.getenv('DB_NAME'),
user = os.getenv('DB_USER'),
password = os.getenv('DB_PASSWORD'),
host = os.getenv('DB_HOST', 'localhost'),
port = 5432)
try:
with conn:
with conn.cursor() as cur:
cur.execute('SELECT id , name , name_ru , slug FROM categories WHERE id = :categoryId')
return cur.fetchone()[0]
finally:
conn.close()

@router.put('/v1/categories/:categoryId')
def put_v1_categories_category_id():
Expand Down
1 change: 1 addition & 0 deletions src/templates/requirements.txt.ejs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
fastapi===0.83.0
uvicorn==0.18.3
psycopg2-binary==2.9.3
23 changes: 22 additions & 1 deletion src/templates/routes.py.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import psycopg2

from fastapi import APIRouter

router = APIRouter()
Expand All @@ -14,14 +17,32 @@ endpoints.forEach(function(endpoint) {
endpoint.methods.forEach(function(method) {
const pythonMethodName = generate_method_name(method.name, path)
const sql = formatQuery(method.query)
if (method.name === 'get' || method.name === 'get_list') {
%>
@router.get('<%- path %>')
def <%- pythonMethodName %>():
<% if (method.name === 'get') { -%>
conn = psycopg2.connect(
database = os.getenv('DB_NAME'),
user = os.getenv('DB_USER'),
password = os.getenv('DB_PASSWORD'),
host = os.getenv('DB_HOST', 'localhost'),
port = 5432)
try:
with conn:
with conn.cursor() as cur:
cur.execute('<%- sql %>')
return cur.fetchone()[0]
finally:
conn.close()
<%
} else {
-%>
pass
<%
}
}
if (method.name === 'post') {
%>
Expand Down

0 comments on commit 6be61e5

Please sign in to comment.