mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-08-26 16:40:40 +03:00
add type for get_db method
This commit is contained in:
parent
234f9c36d8
commit
9f6b13424f
@ -1,3 +1,4 @@
|
|||||||
|
from typing import Generator
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
@ -18,7 +19,7 @@ async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession
|
|||||||
|
|
||||||
|
|
||||||
# Dependency
|
# Dependency
|
||||||
async def get_db():
|
async def get_db() -> Generator:
|
||||||
session = async_session()
|
session = async_session()
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
|
@ -22,7 +22,8 @@ class Stuff(Base):
|
|||||||
async def find(cls, db_session: AsyncSession, name: str):
|
async def find(cls, db_session: AsyncSession, name: str):
|
||||||
stmt = select(cls).where(cls.name == name)
|
stmt = select(cls).where(cls.name == name)
|
||||||
result = await db_session.execute(stmt)
|
result = await db_session.execute(stmt)
|
||||||
if instance := result.scalars().first() is None:
|
instance = result.scalars().first()
|
||||||
|
if instance is None:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
detail={"Record not found": f"There is no record for requested name value : {name}"},
|
detail={"Record not found": f"There is no record for requested name value : {name}"},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user