From 2ffb7006ff78efcbfba54fec804d60c49e6be606 Mon Sep 17 00:00:00 2001 From: "Brandon H. Goding" Date: Tue, 5 Mar 2024 03:09:49 -0500 Subject: [PATCH] [Issue #1312]: full examples use lifespan instead of on_startup (#1363) * docs(examples): full examples use lifespan instead of on_startup for database initialization * Update beanie.md Replaced the annotations on the notes that I missed --- docs/configuration/databases/beanie.md | 10 +++++++--- examples/beanie-oauth/app/app.py | 26 ++++++++++++++----------- examples/beanie/app/app.py | 27 +++++++++++++++----------- examples/sqlalchemy-oauth/app/app.py | 18 ++++++++++------- examples/sqlalchemy/app/app.py | 18 ++++++++++------- 5 files changed, 60 insertions(+), 39 deletions(-) diff --git a/docs/configuration/databases/beanie.md b/docs/configuration/databases/beanie.md index 5176ec2b..23e42055 100644 --- a/docs/configuration/databases/beanie.md +++ b/docs/configuration/databases/beanie.md @@ -40,20 +40,24 @@ Notice that we pass a reference to the `User` model we defined above. ## Initialize Beanie -When initializing your FastAPI app, it's important that you [**initialize Beanie**](https://roman-right.github.io/beanie/tutorial/initialization/) so it can discover your models. We can achieve this using a startup event handler on the FastAPI app: +When initializing your FastAPI app, it's important that you [**initialize Beanie**](https://roman-right.github.io/beanie/tutorial/initialization/) so it can discover your models. We can achieve this using [**Lifespan Events**](https://fastapi.tiangolo.com/advanced/events/) on the FastAPI app: ```py +from contextlib import asynccontextmanager from beanie import init_beanie -@app.on_event("startup") -async def on_startup(): +@asynccontextmanager +async def lifespan(app: FastAPI): await init_beanie( database=db, # (1)! document_models=[ User, # (2)! ], ) + yield + +app = FastAPI(lifespan=lifespan) ``` 1. This is the `db` Motor database instance we defined above. diff --git a/examples/beanie-oauth/app/app.py b/examples/beanie-oauth/app/app.py index a9ceb8f6..2b39953c 100644 --- a/examples/beanie-oauth/app/app.py +++ b/examples/beanie-oauth/app/app.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from beanie import init_beanie from fastapi import Depends, FastAPI @@ -11,7 +13,19 @@ from app.users import ( google_oauth_client, ) -app = FastAPI() + +@asynccontextmanager +async def lifespan(app: FastAPI): + await init_beanie( + database=db, + document_models=[ + User, + ], + ) + yield + + +app = FastAPI(lifespan=lifespan) app.include_router( fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] @@ -46,13 +60,3 @@ app.include_router( @app.get("/authenticated-route") async def authenticated_route(user: User = Depends(current_active_user)): return {"message": f"Hello {user.email}!"} - - -@app.on_event("startup") -async def on_startup(): - await init_beanie( - database=db, - document_models=[ - User, - ], - ) diff --git a/examples/beanie/app/app.py b/examples/beanie/app/app.py index 5550983a..b2164a21 100644 --- a/examples/beanie/app/app.py +++ b/examples/beanie/app/app.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from beanie import init_beanie from fastapi import Depends, FastAPI @@ -5,7 +7,20 @@ from app.db import User, db from app.schemas import UserCreate, UserRead, UserUpdate from app.users import auth_backend, current_active_user, fastapi_users -app = FastAPI() + +@asynccontextmanager +async def lifespan(app: FastAPI): + await init_beanie( + database=db, + document_models=[ + User, + ], + ) + yield + + +app = FastAPI(lifespan=lifespan) + app.include_router( fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] @@ -35,13 +50,3 @@ app.include_router( @app.get("/authenticated-route") async def authenticated_route(user: User = Depends(current_active_user)): return {"message": f"Hello {user.email}!"} - - -@app.on_event("startup") -async def on_startup(): - await init_beanie( - database=db, - document_models=[ - User, - ], - ) diff --git a/examples/sqlalchemy-oauth/app/app.py b/examples/sqlalchemy-oauth/app/app.py index cb764d39..27bae93f 100644 --- a/examples/sqlalchemy-oauth/app/app.py +++ b/examples/sqlalchemy-oauth/app/app.py @@ -1,3 +1,5 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI from app.db import User, create_db_and_tables @@ -10,7 +12,15 @@ from app.users import ( google_oauth_client, ) -app = FastAPI() + +@asynccontextmanager +async def lifespan(app: FastAPI): + # Not needed if you setup a migration system like Alembic + await create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) app.include_router( fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] @@ -45,9 +55,3 @@ app.include_router( @app.get("/authenticated-route") async def authenticated_route(user: User = Depends(current_active_user)): return {"message": f"Hello {user.email}!"} - - -@app.on_event("startup") -async def on_startup(): - # Not needed if you setup a migration system like Alembic - await create_db_and_tables() diff --git a/examples/sqlalchemy/app/app.py b/examples/sqlalchemy/app/app.py index 034089cb..db92154c 100644 --- a/examples/sqlalchemy/app/app.py +++ b/examples/sqlalchemy/app/app.py @@ -1,10 +1,20 @@ +from contextlib import asynccontextmanager + from fastapi import Depends, FastAPI from app.db import User, create_db_and_tables from app.schemas import UserCreate, UserRead, UserUpdate from app.users import auth_backend, current_active_user, fastapi_users -app = FastAPI() + +@asynccontextmanager +async def lifespan(app: FastAPI): + # Not needed if you setup a migration system like Alembic + await create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) app.include_router( fastapi_users.get_auth_router(auth_backend), prefix="/auth/jwt", tags=["auth"] @@ -34,9 +44,3 @@ app.include_router( @app.get("/authenticated-route") async def authenticated_route(user: User = Depends(current_active_user)): return {"message": f"Hello {user.email}!"} - - -@app.on_event("startup") -async def on_startup(): - # Not needed if you setup a migration system like Alembic - await create_db_and_tables()