-
Notifications
You must be signed in to change notification settings - Fork 43
Open
Description
How does the library use asynchronous mode?
Below is the code I tried, but it not work.
from fastapi import FastAPI
from fastapi_sqlalchemy import DBSessionMiddleware # middleware helper
from fastapi_sqlalchemy import db # an object to provide global access to a database session
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from models.user import User
app = FastAPI()
app.add_middleware(DBSessionMiddleware, db_url="sqlite://",
custom_engine=create_async_engine,
session_args={
'expire_on_commit': False,
'class_': AsyncSession
})
# once the middleware is applied, any route can then access the database session
# from the global ``db``
@app.get("/users")
async def get_users():
users = await db.session.query(User).all()
return users
if __name__ == '__main__':
import uvicorn
uvicorn.run(app=app, host="127.0.0.1", port=8001, log_level="debug")Does anyone else have any ideas?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels