create all on startup

This commit is contained in:
Waylon Walker 2023-05-22 08:24:01 -05:00
parent ad6471da8c
commit 1c60f96697
No known key found for this signature in database
GPG key ID: 66E2BF2B4190EFE4
2 changed files with 46 additions and 3 deletions

View file

@ -1,13 +1,20 @@
from typing import Annotated
from fastapi import APIRouter, Depends
from sqlmodel import SQLModel
from learn_sql_model.api.user import oauth2_scheme
from learn_sql_model.config import get_config
from learn_sql_model.models.hero import Hero
hero_router = APIRouter()
@hero_router.on_event("startup")
def on_startup() -> None:
SQLModel.metadata.create_all(get_config().database.engine)
@hero_router.get("/items/")
async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
return {"token": token}
@ -29,3 +36,8 @@ def post_hero(hero: Hero) -> Hero:
def get_heros() -> list[Hero]:
"get all heros"
return Hero().get()
# Alternatively
# with get_config().database.session as session:
# statement = select(Hero)
# results = session.exec(statement).all()
# return results