diff --git a/src/app/api/crud.py b/src/app/api/crud.py index 3c9c33f..55f3023 100644 --- a/src/app/api/crud.py +++ b/src/app/api/crud.py @@ -1,20 +1,31 @@ from app.api.models import NoteSchema -from app.db import notes, database +from app.db import notes, async_session async def post(payload: NoteSchema): query = notes.insert().values(title=payload.title, description=payload.description) - return await database.execute(query=query) + + with async_session() as db: + response = await db.execute(query=query) + + return response async def get(id: int): query = notes.select().where(id == notes.c.id) - return await database.fetch_one(query=query) + + with async_session() as db: + response = await db.execute(query=query) + + return response async def get_all(): query = notes.select() - return await database.fetch_all(query=query) + with async_session() as db: + response = await db.execute(query=query) + + return response async def put(id: int, payload: NoteSchema): @@ -25,9 +36,17 @@ async def put(id: int, payload: NoteSchema): .values(title=payload.title, description=payload.description) .returning(notes.c.id) ) - return await database.execute(query=query) + + with async_session() as db: + response = await db.execute(query=query) + + return response async def delete(id: int): query = notes.delete().where(id == notes.c.id) - return await database.execute(query=query) + + with async_session() as db: + response = await db.execute(query=query) + + return response diff --git a/src/app/db.py b/src/app/db.py index 465814b..02af266 100644 --- a/src/app/db.py +++ b/src/app/db.py @@ -1,5 +1,7 @@ import os +import logging +logger = logging.getLogger(__name__) from sqlalchemy import ( Column, DateTime, @@ -7,16 +9,16 @@ MetaData, String, Table, - create_engine ) +from sqlalchemy.orm import sessionmaker +from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession from sqlalchemy.sql import func -from databases import Database - -DATABASE_URL = os.getenv("DATABASE_URL") +DATABASE_URL = os.getenv("DATABASE_URL").replace('postgresql', 'postgresql+asyncpg') +logger.debug(DATABASE_URL, 'DATABASE_URL') # SQLAlchemy -engine = create_engine(DATABASE_URL) +engine = create_async_engine(DATABASE_URL, echo=True) metadata = MetaData() notes = Table( "notes", @@ -27,5 +29,7 @@ Column("created_date", DateTime, default=func.now(), nullable=False), ) -# databases query builder -database = Database(DATABASE_URL) +# sessionmaker version +async_session = sessionmaker( + engine, expire_on_commit=False, class_=AsyncSession +) \ No newline at end of file diff --git a/src/app/main.py b/src/app/main.py index 2533c73..85377c7 100644 --- a/src/app/main.py +++ b/src/app/main.py @@ -1,21 +1,30 @@ +import asyncio from fastapi import FastAPI from app.api import notes, ping -from app.db import database, engine, metadata +from app.db import engine, metadata + + +"""async def async_main(): + async with engine.begin() as conn: + await conn.run_sync(metadata.create_all) + + await engine.dispose() + +asyncio.run(async_main())""" -metadata.create_all(engine) app = FastAPI() @app.on_event("startup") async def startup(): - await database.connect() + await engine.connect() @app.on_event("shutdown") async def shutdown(): - await database.disconnect() + await engine.dispose() app.include_router(ping.router) diff --git a/src/requirements.txt b/src/requirements.txt index 1e704df..6c7daed 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,9 +1,9 @@ -databases[postgresql]==0.6.2 psycopg2-binary==2.9.5 SQLAlchemy==1.4.41 asyncpg==0.27.0 fastapi==0.87.0 uvicorn==0.20.0 +asyncpg # dev pytest==7.2.0