mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-16 03:40:23 +08:00
* 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
This commit is contained in:

committed by
GitHub

parent
0df82afb32
commit
2ffb7006ff
@ -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.
|
||||
|
@ -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,
|
||||
],
|
||||
)
|
||||
|
@ -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,
|
||||
],
|
||||
)
|
||||
|
@ -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()
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user