diff --git a/README.md b/README.md index ea9a943..e261422 100644 --- a/README.md +++ b/README.md @@ -20,73 +20,40 @@ Based on FastAPI-SQLAlchemy ### Examples -### Usage inside of a route +#### Usage inside of a route Note that the session object provided by ``db.session`` is based on the Python3.7+ ``ContextVar``. This means that each session is linked to the individual request context in which it was created. ```python - from fastapi import FastAPI - from fastapi_async_sqlalchemy import SQLAlchemyMiddleware # middleware helper - from fastapi_async_sqlalchemy import db # an object to provide global access to a database session +from fastapi import FastAPI +from fastapi_async_sqlalchemy import SQLAlchemyMiddleware # middleware helper +from fastapi_async_sqlalchemy import db # an object to provide global access to a database session +from sqlalchemy import column +from sqlalchemy import table - from app.models import User +app = FastAPI() +app.add_middleware(SQLAlchemyMiddleware, db_url="postgresql+asyncpg://user:user@192.168.88.200:5432/primary_db") - app = FastAPI() +foo = table("ms_files", column("id")) - app.add_middleware(SQLAlchemyMiddleware, db_url="sqlite+aiosqlite://") - # once the middleware is applied, any route can then access the database session - # from the global ``db`` +@app.get("/") +async def get_files(): + result = await db.session.execute(foo.select()) + return result.fetchall() - @app.get("/users") - def get_users(): - users = db.session.query(User).all() - return users -``` - - -### Usage outside of a route - -Sometimes it is useful to be able to access the database outside the context of a request, such as in scheduled tasks which run in the background: - -```python - - import pytz - from apscheduler.schedulers.asyncio import AsyncIOScheduler # other schedulers are available - from fastapi import FastAPI - from fastapi_async_sqlalchemy import db - - from app.models import User, UserCount +@app.get("/db_context") +async def db_context(): + async with db(): + result = await db.session.execute(foo.select()) + return result.fetchall() - app = FastAPI() - app.add_middleware(DBSessionMiddleware, db_url="sqlite+aiosqlite://") +if __name__ == "__main__": + import uvicorn + uvicorn.run(app, host="0.0.0.0", port=8002) - - @app.on_event('startup') - async def startup_event(): - scheduler = AsyncIOScheduler(timezone=pytz.utc) - scheduler.start() - scheduler.add_job(count_users_task, "cron", hour=0) # runs every night at midnight - - - def count_users_task(): - """Count the number of users in the database and save it into the user_counts table.""" - - # we are outside of a request context, therefore we cannot rely on ``DBSessionMiddleware`` - # to create a database session for us. Instead, we can use the same ``db`` object and - # use it as a context manager, like so: - - with db(): - user_count = db.session.query(User).count() - - db.session.add(UserCount(user_count)) - db.session.commit() - - # no longer able to access a database session once the db() context manager has ended - - return users -``` +``` \ No newline at end of file diff --git a/fastapi_async_sqlalchemy/__init__.py b/fastapi_async_sqlalchemy/__init__.py index fa81c67..fa5e330 100644 --- a/fastapi_async_sqlalchemy/__init__.py +++ b/fastapi_async_sqlalchemy/__init__.py @@ -2,4 +2,4 @@ __all__ = ["db", "SQLAlchemyMiddleware"] -__version__ = "0.3.1.dev1" +__version__ = "0.3.1.dev2" diff --git a/fastapi_async_sqlalchemy/middleware.py b/fastapi_async_sqlalchemy/middleware.py index 6556744..082c14a 100644 --- a/fastapi_async_sqlalchemy/middleware.py +++ b/fastapi_async_sqlalchemy/middleware.py @@ -38,7 +38,7 @@ def __init__( engine = custom_engine global _Session - _Session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession, **session_args) + _Session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False, **session_args) async def dispatch(self, request: Request, call_next: RequestResponseEndpoint): async with db(commit_on_exit=self.commit_on_exit):