You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an async generator function which yields a db async session within it. Now, I am looking for a dependency injection approach to resolve it exactly like Depends() acts in FastAPI!
Now, outside of the request, how can I inject the get_db within a new method and get rid of async for inside of the method?
@cli.command(name="create_superuser")asyncdefcreate_superuser(): # Note: how to pass db session here as param?username=click.prompt("Username", type=str)
email=click.prompt("Email (optional)", type=str, default="")
password=getpass("Password: ")
confirm_password=getpass("Confirm Password: ")
ifpassword!=confirm_password:
click.echo("Passwords do not match")
returnasyncfordbindatabase.get_db(): # Note: remove it from hereuser=schemas.UserAdminCreate(
username=username,
email=Noneifnotemailelseemail,
password=password,
role="admin",
)
awaitcrud.create_user(db=db, user=user)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have an async generator function which yields a db async session within it. Now, I am looking for a dependency injection approach to resolve it exactly like
Depends()
acts in FastAPI!Here's the
get_db()
async generator:In the FastAPI router, I can simply use
Depends()
to resolve an async generator and reach to the async session (db session):Now, outside of the request, how can I inject the
get_db
within a new method and get rid ofasync for
inside of the method?Can I use this package to do something like this?
Beta Was this translation helpful? Give feedback.
All reactions