Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork14
Synchronous Session support.#13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@frankie567 friendly ping on this — I believe this is feature complete & fully tested in the existing style. |
@frankie567 friendly ping here, don't want this to get stale |
BredoGen commentedSep 30, 2023
It'll be great if sync SQLAlchemy adapter will be supported out-of-box. Are there any plans to merge this? |
jerivas commentedNov 2, 2023
For anyone who is using sync sessions and doesn't want to maintain mirror classes, here's a hack to make all methods on importinspectfromtypingimportAnnotated,Any,Genericfromfastapi_users.authentication.strategy.dbimportAPfromfastapi_users.modelsimportID,UPfromfastapi_users_db_sqlalchemyimport (SQLAlchemyBaseOAuthAccountTable,SQLAlchemyUserDatabase,)fromfastapi_users_db_sqlalchemy.access_tokenimportSQLAlchemyAccessTokenDatabasefromsqlalchemy.ext.asyncioimportAsyncSessionfromsqlalchemy.ormimportSessionclassFakeAsyncSession:def__init__(self,session:Session):self.session=sessiondef__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)ifnotinspect.iscoroutinefunction(async_session_attr):returnsession_attrasyncdefasync_wrapper(*args,**kwargs):returnsession_attr(*args,**kwargs)returnasync_wrapperclassUserDatabase(Generic[UP,ID],SQLAlchemyUserDatabase[UP,ID]):session:Sessiondef__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)classAccessTokenDatabase(Generic[AP],SQLAlchemyAccessTokenDatabase[AP]):session:Sessiondef__init__(self,session:AsyncSession,access_token_table:type[AP]):super().__init__(session,access_token_table)self.session=FakeAsyncSession(session) Then both |
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
-->SQLAlchemySynchronousUserDatabase
and then:Session
arg instead of anAsyncSession
await
keyword when working with this session (e.g.await self.session.commit()
becomesself.session.commit()
)SQLAlchemyAccessTokenDatabase
-->SQLAlchemySynchronousAccessTokenDatabase
and applied the same changesTested:
SYNC_DATABASE_URL
that uses a non-async sqlite adapterFollowup: