Skip to content

Commit

Permalink
feat: enable flask support (#86)
Browse files Browse the repository at this point in the history
The PR enables Flask support by changing how we validate the session object.

In normal Sessions, the `bind` attribute is never empty. However, when using scoped sessions, this is intended to be empty.  This change will call the `get_bind()` function when the bind is not set.
  • Loading branch information
cofin committed Oct 31, 2023
1 parent 103f9bf commit 15a7d76
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 22 deletions.
7 changes: 1 addition & 6 deletions advanced_alchemy/repository/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ def __init__(
self.statement = lambda_stmt(lambda: statement)
else:
self.statement = statement
if not self.session.bind:
# this shouldn't actually ever happen, but we include it anyway to properly
# narrow down the types
msg = "Session improperly configure"
raise ValueError(msg)
self._dialect = self.session.bind.dialect
self._dialect = self.session.bind.dialect if self.session.bind is not None else self.session.get_bind().dialect
self._prefer_any = any(self._dialect.name == engine_type for engine_type in self.prefer_any_dialects or ())

@classmethod
Expand Down
7 changes: 1 addition & 6 deletions advanced_alchemy/repository/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ def __init__(
self.statement = lambda_stmt(lambda: statement)
else:
self.statement = statement
if not self.session.bind:
# this shouldn't actually ever happen, but we include it anyway to properly
# narrow down the types
msg = "Session improperly configure"
raise ValueError(msg)
self._dialect = self.session.bind.dialect
self._dialect = self.session.bind.dialect if self.session.bind is not None else self.session.get_bind().dialect
self._prefer_any = any(self._dialect.name == engine_type for engine_type in self.prefer_any_dialects or ())

@classmethod
Expand Down
47 changes: 47 additions & 0 deletions examples/flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import Mapped, sessionmaker

from advanced_alchemy import SQLAlchemySyncRepository
from advanced_alchemy.base import UUIDBase

SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)


class Message(UUIDBase):
text: Mapped[str]


class MessageRepository(SQLAlchemySyncRepository[Message]):
model_type = Message


# Working
with app.app_context():
with db.engine.begin() as conn:
Message.metadata.create_all(conn)

session = sessionmaker(db.engine)()

repo = MessageRepository(session=session)
repo.add(Message(text="Hello, world!"))

message = repo.list()[0]
assert message.text == "Hello, world!" # noqa: S101


# Not working
with app.app_context():
with db.engine.begin() as conn:
Message.metadata.create_all(conn)

session = db.session
repo = MessageRepository(session=session)
repo.add(Message(text="Hello, world!"))

message = repo.list()[0]
assert message.text == "Hello, world!" # noqa: S101
80 changes: 72 additions & 8 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ dependencies = [
"greenlet; sys_platform == \"darwin\"",
]
description = "Ready-to-go SQLAlchemy concoctions."
keywords = ["sqlalchemy", "alembic", "litestar", "sanic", "fastapi"]
keywords = ["sqlalchemy", "alembic", "litestar", "sanic", "fastapi", "flask"]
license = {text = "MIT"}
name = "advanced_alchemy"
readme = "README.md"
requires-python = ">=3.8"
version = "0.5.2"
version = "0.5.3"

[project.urls]
Changelog = "https://docs.advanced-alchemy.jolt.rs/latest/changelog"
Expand Down Expand Up @@ -89,6 +89,8 @@ extensions = [
"sanic-testing>=23.6.0",
"socketify>=0.0.27",
"msgspec>=0.18.2",
"flask>=3.0.0",
"flask-sqlalchemy>=3.1.1",
]
linting = [
"pre-commit>=3.4.0",
Expand Down

0 comments on commit 15a7d76

Please sign in to comment.