Skip to content

How does the library use asynchronous mode? #32

@pylixm

Description

@pylixm

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions