Open
Conversation
Author
|
@frankie567 friendly ping on this — I believe this is feature complete & fully tested in the existing style. |
Author
|
@frankie567 friendly ping here, don't want this to get stale |
|
It'll be great if sync SQLAlchemy adapter will be supported out-of-box. Are there any plans to merge this? |
|
For anyone who is using sync sessions and doesn't want to maintain mirror classes, here's a hack to make all methods on import inspect
from typing import Annotated, Any, Generic
from fastapi_users.authentication.strategy.db import AP
from fastapi_users.models import ID, UP
from fastapi_users_db_sqlalchemy import (
SQLAlchemyBaseOAuthAccountTable,
SQLAlchemyUserDatabase,
)
from fastapi_users_db_sqlalchemy.access_token import SQLAlchemyAccessTokenDatabase
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Session
class FakeAsyncSession:
def __init__(self, session: Session):
self.session = session
def __getattr__(self, name: str) -> Any:
"""
If the method being called is async in AsyncSession, create a fake async version
for Session so callers can `await` as usual. Think `commit`, `refresh`,
`delete`, etc.
"""
async_session_attr = getattr(AsyncSession, name, None)
session_attr = getattr(self.session, name)
if not inspect.iscoroutinefunction(async_session_attr):
return session_attr
async def async_wrapper(*args, **kwargs):
return session_attr(*args, **kwargs)
return async_wrapper
class UserDatabase(Generic[UP, ID], SQLAlchemyUserDatabase[UP, ID]):
session: Session
def __init__(
self,
session: AsyncSession,
user_table: type[UP],
oauth_account_table: type[SQLAlchemyBaseOAuthAccountTable] | None = None,
):
super().__init__(session, user_table, oauth_account_table)
self.session = FakeAsyncSession(session)
class AccessTokenDatabase(Generic[AP], SQLAlchemyAccessTokenDatabase[AP]):
session: Session
def __init__(self, session: AsyncSession, access_token_table: type[AP]):
super().__init__(session, access_token_table)
self.session = FakeAsyncSession(session)Then both |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements support for SQLAlchemy synchronous
Session, alongside existing support forAsyncSession.Based on @frankie567's answer in this discussion on "Synchronous SQLAlchemy?": fastapi-users/fastapi-users#1144 (comment)
SQLAlchemyUserDatabase-->SQLAlchemySynchronousUserDatabaseand then:Sessionarg instead of anAsyncSessionawaitkeyword when working with this session (e.g.await self.session.commit()becomesself.session.commit())SQLAlchemyAccessTokenDatabase-->SQLAlchemySynchronousAccessTokenDatabaseand applied the same changesTested:
SYNC_DATABASE_URLthat uses a non-async sqlite adapterFollowup: