Skip to content

Commit d28e6a6

Browse files
authored
Merge pull request #180 from grillazz/179-reflect-latest-and-greatest-fastapi-improvements
179 reflect latest and greatest fastapi improvements
2 parents adfb049 + b0cfe92 commit d28e6a6

File tree

6 files changed

+148
-147
lines changed

6 files changed

+148
-147
lines changed

app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def lifespan(_app: FastAPI):
5151
await _app.postgres_pool.close()
5252

5353

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

5656
app.include_router(stuff_router)
5757
app.include_router(nonsense_router)

app/models/stuff.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
import uuid
22

3-
from sqlalchemy.dialects import postgresql
43
from sqlalchemy import String, select, ForeignKey
54
from sqlalchemy.dialects.postgresql import UUID
65
from sqlalchemy.ext.asyncio import AsyncSession
76
from sqlalchemy.orm import mapped_column, Mapped, relationship, joinedload
87

98
from app.models.base import Base
109
from app.models.nonsense import Nonsense
11-
12-
from functools import wraps
13-
14-
15-
def compile_sql_or_scalar(func):
16-
@wraps(func)
17-
async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs):
18-
stmt = await func(cls, db_session, name, *args, **kwargs)
19-
if compile_sql:
20-
return stmt.compile(
21-
dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}
22-
)
23-
result = await db_session.execute(stmt)
24-
return result.scalars().first()
25-
26-
return wrapper
10+
from app.utils.decorators import compile_sql_or_scalar
2711

2812

2913
class Stuff(Base):

app/utils/decorators.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from sqlalchemy.dialects import postgresql
2+
3+
from functools import wraps
4+
5+
6+
def compile_sql_or_scalar(func):
7+
"""
8+
A decorator that compiles an SQL statement or executes it and returns the first scalar result.
9+
10+
Args:
11+
func (Callable): The function to be decorated. It should return an SQLAlchemy statement.
12+
13+
Returns:
14+
Callable: A wrapper function that either compiles the SQL statement or executes it and returns the first scalar result.
15+
"""
16+
17+
@wraps(func)
18+
async def wrapper(cls, db_session, name, compile_sql=False, *args, **kwargs):
19+
"""
20+
Wrapper function that either compiles the SQL statement or executes it.
21+
22+
Args:
23+
cls (Type): The class on which the method is called.
24+
db_session (AsyncSession): The SQLAlchemy async session.
25+
name (str): The name to be used in the SQL statement.
26+
compile_sql (bool, optional): If True, compiles the SQL statement. Defaults to False.
27+
*args: Additional positional arguments.
28+
**kwargs: Additional keyword arguments.
29+
30+
Returns:
31+
Compiled SQL statement or the first scalar result of the executed statement.
32+
"""
33+
stmt = await func(cls, db_session, name, *args, **kwargs)
34+
if compile_sql:
35+
return stmt.compile(
36+
dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}
37+
)
38+
result = await db_session.execute(stmt)
39+
return result.scalars().first()
40+
41+
return wrapper

0 commit comments

Comments
 (0)