Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
fotinakis wants to merge1 commit intofastapi-users:main
base:main
Choose a base branch
Loading
fromfotinakis:main

Conversation

fotinakis
Copy link

This PR implements support for SQLAlchemy synchronousSession, alongside existing support forAsyncSession.

Based on@frankie567's answer in this discussion on "Synchronous SQLAlchemy?":fastapi-users/fastapi-users#1144 (comment)

  • CopiedSQLAlchemyUserDatabase -->SQLAlchemySynchronousUserDatabase and then:
    • Accept a sqlalchemySession arg instead of anAsyncSession
    • Remove theawait keyword when working with this session (e.g.await self.session.commit() becomesself.session.commit())
    • The queries didn't have to change
  • CopiedSQLAlchemyAccessTokenDatabase -->SQLAlchemySynchronousAccessTokenDatabase and applied the same changes

Tested:

  • Added aSYNC_DATABASE_URL that uses a non-async sqlite adapter
  • Used pytestparameterized fixtures to run the same tests with the sync & async engines
  • Also tested similar setup in a real-world app

Followup:

  • If this is accepted, I'd be happy to update documentation to reference these new object types

BredoGen, jerivas, and KCerveny reacted with thumbs up emoji
@fotinakis
Copy link
Author

@frankie567 friendly ping on this — I believe this is feature complete & fully tested in the existing style.

@fotinakis
Copy link
Author

@frankie567 friendly ping here, don't want this to get stale

@BredoGen
Copy link

It'll be great if sync SQLAlchemy adapter will be supported out-of-box.

Are there any plans to merge this?

jerivas and KCerveny reacted with eyes emoji

@jerivas
Copy link

For anyone who is using sync sessions and doesn't want to maintain mirror classes, here's a hack to make all methods onSession awaitable based onAsyncSession:

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 bothUserDatabase andAccessTokenDatabase can be used in place of the classes provided by this library.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@fotinakis@BredoGen@jerivas

[8]ページ先頭

©2009-2025 Movatter.jp