Skip to content

Backend for async SQLAlchemy ORM#121

Open
il-s wants to merge 10 commits into
awtkns:masterfrom
il-s:master
Open

Backend for async SQLAlchemy ORM#121
il-s wants to merge 10 commits into
awtkns:masterfrom
il-s:master

Conversation

@il-s

@il-s il-s commented Nov 25, 2021

Copy link
Copy Markdown

Hi, I liked the idea of ​​your project and needed to make a fastapi router that will work with SQLAlchemy in asynchronous mode.

Also this answers issue number #87

@vercel

vercel Bot commented Nov 25, 2021

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/flortz/fastapi-crudrouter/EAS4myHY5LcKjZqzoaV1XjS28kmP
✅ Preview: https://fastapi-crudrouter-git-fork-il-s-master-flortz.vercel.app

@michaeltoohig

Copy link
Copy Markdown

Wow, I was just working on this myself after abandoning ormar in favor of using async sqlalchemy but yours looks great and I hope it is merged quickly.

@awtkns

awtkns commented Nov 30, 2021

Copy link
Copy Markdown
Owner

Hi @il-s thanks for this! Sorry for the delay, I've been quite busy.

I am totally on board with adding support for this, however I have a couple questions:

  1. Instead of a brand new implementation, do you think it would be better to simply have async=true as an optional parameter?
  2. For this to be merged, the implementation will need to be added to the test suite. Did you need help with that?

Thanks again for this 🚀

@il-s

il-s commented Nov 30, 2021

Copy link
Copy Markdown
Author

@awtkns
ok i will try to implement option 1 (with async = true parameter)

@il-s

il-s commented Nov 30, 2021

Copy link
Copy Markdown
Author

for the test, I used db_func like so:

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import declarative_base, sessionmaker

Base = declarative_base()

class AsyncDatabaseSession:
    def __init__(self):
        self._session = None
        self._engine = None
    def __getattr__(self, name):
        return getattr(self._session, name)
    async def init(self):
        self._engine = create_async_engine(
            "postgresql+asyncpg://postgres:postgres@localhost/postgres",
            echo=True,
        )

        self._session = sessionmaker(
            self._engine, expire_on_commit=True, class_=AsyncSession
        )()

main_session = AsyncDatabaseSession()

async def get_db():
    yield main_session._session
    await main_session._session.commit()

`

@il-s

il-s commented Dec 4, 2021

Copy link
Copy Markdown
Author

@awtkns, need review!

@awtkns

awtkns commented Dec 5, 2021

Copy link
Copy Markdown
Owner

HI @il-s, looking good!

I am hoping to get this in and released tomorrow! I just need to add it to the test framework and write some documentation for it 🚀

@il-s

il-s commented Jan 28, 2022

Copy link
Copy Markdown
Author

@awtkns
Any ideas on how to do a merge?

@JonasKs

JonasKs commented Feb 9, 2022

Copy link
Copy Markdown

Hi @awtkns , any status on this?

try:
(model,) = (
await db.execute(
select(self.db_model).where(self.db_model.id == item_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just fyi... my team noticed this doesn't work with PK's not named "id". I think this code might work better:

Suggested change
select(self.db_model).where(self.db_model.id == item_id)
select(self.db_model).where(getattr(self.db_model, self._pk) == item_id)

@vercel

vercel Bot commented Jan 11, 2023

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
fastapi-crudrouter ✅ Ready (Inspect) Visit Preview Jan 11, 2023 at 7:08AM (UTC)

@VolDr VolDr left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants