Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

179 reflect latest and greatest fastapi improvements #180

Merged
merged 4 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def lifespan(_app: FastAPI):
await _app.postgres_pool.close()


app = FastAPI(title="Stuff And Nonsense API", version="0.15", lifespan=lifespan)
app = FastAPI(title="Stuff And Nonsense API", version="0.16", lifespan=lifespan)

app.include_router(stuff_router)
app.include_router(nonsense_router)
Expand Down
18 changes: 1 addition & 17 deletions app/models/stuff.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
import uuid

from sqlalchemy.dialects import postgresql
from sqlalchemy import String, select, ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import mapped_column, Mapped, relationship, joinedload

from app.models.base import Base
from app.models.nonsense import Nonsense

from functools import wraps


def compile_sql_or_scalar(func):
@wraps(func)
async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs):
stmt = await func(cls, db_session, name, *args, **kwargs)
if compile_sql:
return stmt.compile(
dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}
)
result = await db_session.execute(stmt)
return result.scalars().first()

return wrapper
from app.utils.decorators import compile_sql_or_scalar


class Stuff(Base):
Expand Down
41 changes: 41 additions & 0 deletions app/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from sqlalchemy.dialects import postgresql

from functools import wraps


def compile_sql_or_scalar(func):
"""
A decorator that compiles an SQL statement or executes it and returns the first scalar result.

Args:
func (Callable): The function to be decorated. It should return an SQLAlchemy statement.

Returns:
Callable: A wrapper function that either compiles the SQL statement or executes it and returns the first scalar result.
"""

@wraps(func)
async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs):
"""
Wrapper function that either compiles the SQL statement or executes it.

Args:
cls (Type): The class on which the method is called.
db_session (AsyncSession): The SQLAlchemy async session.
name (str): The name to be used in the SQL statement.
compile_sql (bool, optional): If True, compiles the SQL statement. Defaults to False.
*args: Additional positional arguments.
**kwargs: Additional keyword arguments.

Returns:
Compiled SQL statement or the first scalar result of the executed statement.
"""
stmt = await func(cls, db_session, name, *args, **kwargs)
if compile_sql:
return stmt.compile(
dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}
)
result = await db_session.execute(stmt)
return result.scalars().first()

return wrapper
Loading