Skip to content

Commit

Permalink
✨ SQLAlchemy 2.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy committed Mar 1, 2023
1 parent a7c45f1 commit e64765b
Show file tree
Hide file tree
Showing 5 changed files with 2,032 additions and 1,855 deletions.
8 changes: 4 additions & 4 deletions flama/resources/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def create(

return http.APIResponse(
schema=rest_schemas.output.schema,
content={**element, **dict(result.inserted_primary_key)},
content={**element, **dict(zip([x.name for x in self.model.primary_key], result.inserted_primary_key))},
status_code=201,
)

Expand Down Expand Up @@ -91,12 +91,12 @@ async def retrieve(
async with app.sqlalchemy.engine.begin() as connection:
query = self.model.select().where(self.model.c[rest_model.primary_key.name] == element_id)
result = await connection.execute(query)
element = result.fetchone()
element = result.first()

if element is None:
raise exceptions.HTTPException(status_code=404)

return dict(element)
return element._asdict()

retrieve.__doc__ = f"""
tags:
Expand Down Expand Up @@ -238,7 +238,7 @@ async def filter(self, app, *clauses, **filters) -> t.List[t.Dict]:
if where_clauses:
query = query.where(sqlalchemy.and_(*where_clauses))

return [dict(row) async for row in await connection.stream(query)]
return [row._asdict() async for row in await connection.stream(query)]

@resource_method("/", methods=["GET"], name=f"{name}-list")
@paginator.page_number(schema_name=rest_schemas.output.name)
Expand Down
2 changes: 1 addition & 1 deletion flama/resources/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
try:
from sqlalchemy import Table
except Exception: # pragma: no cover
Table = typing.Any
Table = typing.Any # type: ignore[assignment,misc]

__all__ = ["Model", "PrimaryKey", "Schema", "Metadata", "MethodMetadata"]

Expand Down
8 changes: 4 additions & 4 deletions flama/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

metadata = sqlalchemy.MetaData()
except Exception: # pragma: no cover
sqlalchemy = None
metadata = None
sqlalchemy = None # type: ignore[assignment]
metadata = None # type: ignore[assignment]

__all__ = ["metadata", "SQLAlchemyModule"]

Expand All @@ -25,7 +25,7 @@ def __init__(self, database: t.Optional[str] = None):
self._metadata: t.Optional["sqlalchemy.MetaData"] = metadata

@property
def engine(self) -> "AsyncEngine":
def engine(self) -> t.Optional["AsyncEngine"]:
assert sqlalchemy is not None, "sqlalchemy[asyncio] must be installed to use SQLAlchemyModule."
return self._engine

Expand All @@ -38,7 +38,7 @@ def engine(self):
self._engine = None

@property
def metadata(self) -> "sqlalchemy.MetaData":
def metadata(self) -> t.Optional["sqlalchemy.MetaData"]:
assert sqlalchemy is not None, "sqlalchemy[asyncio] must be installed to use SQLAlchemyModule."
return self._metadata

Expand Down
Loading

0 comments on commit e64765b

Please sign in to comment.