Skip to content

Commit

Permalink
docs(flask): async-in-sync example should use the portal (#418)
Browse files Browse the repository at this point in the history
Update the Flask example that uses the portal class.

Fixes #361
  • Loading branch information
cofin authored Mar 10, 2025
1 parent 05cc96f commit dee0452
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion advanced_alchemy/extensions/flask/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from advanced_alchemy.exceptions import ImproperConfigurationError
from advanced_alchemy.extensions.flask.cli import database_group
from advanced_alchemy.extensions.flask.config import SQLAlchemyAsyncConfig, SQLAlchemySyncConfig
from advanced_alchemy.utils.portals import PortalProvider
from advanced_alchemy.utils.portals import Portal, PortalProvider

if TYPE_CHECKING:
from flask import Flask
Expand Down Expand Up @@ -44,6 +44,11 @@ def __init__(
if app is not None:
self.init_app(app)

@property
def portal(self) -> "Portal":
"""Get the portal."""
return self.portal_provider.portal

@property
def config(self) -> "Sequence[Union[SQLAlchemyAsyncConfig, SQLAlchemySyncConfig]]":
"""Get the SQLAlchemy configuration(s)."""
Expand Down
5 changes: 3 additions & 2 deletions docs/usage/frameworks/flask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Advanced Alchemy supports async SQLAlchemy with Flask:
You can also safely use an AsyncSession in your routes within a sync context.


.. warning::

This is experimental and may change in the future.
Expand All @@ -94,8 +95,8 @@ You can also safely use an AsyncSession in your routes within a sync context.
@app.route("/users")
def list_users():
session = alchemy.get_sync_session()
users = session.execute(select(User))
session = alchemy.get_async_session()
users = alchemy.portal.call(session.execute, select(User))
return {"users": [user.dict() for user in users.scalars()]}
Configuration
Expand Down

0 comments on commit dee0452

Please sign in to comment.