Add database engine and session creation

This commit is contained in:
linuskmr 2021-12-22 12:10:08 +01:00
parent d381888f42
commit 3b50678d0a
2 changed files with 43 additions and 3 deletions

29
backend/src/database.py Normal file
View file

@ -0,0 +1,29 @@
from sqlmodel import SQLModel, create_engine, Session
db_name = "database.db"
db_connection_url = f"sqlite:///{db_name}"
# Disable check_same_thread. Default is True, but FastAPI may use different threads for handling one request due to a
# paused async function may resume in another thread. We will make sure that sessions do not get shared between
# requests by injecting the session as dependency.
connect_args = {"check_same_thread": False}
# Connect to database
engine = create_engine(db_connection_url, echo=True, connect_args=connect_args)
def create_db_and_tables():
"""Creates database and tables if they do not exist yet."""
SQLModel.metadata.create_all(engine)
def get_session():
"""Yields a database session. This method is used to inject a session into request handling functions.
Example:
async def my_route(*, session: Session = Depends(get_session), hero: HeroCreate)
"""
with Session(engine) as session:
yield session

View file

@ -4,6 +4,7 @@ import uvicorn
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from src import database
from src.routes.projects import router as project_router from src.routes.projects import router as project_router
from src.routes.tags import router as tag_router from src.routes.tags import router as tag_router
from src.routes.records import router as record_router from src.routes.records import router as record_router
@ -15,10 +16,11 @@ Welcome to the Juggl API.
This API allows you to retrieve your timetracking data. This API allows you to retrieve your timetracking data.
""" """
app = FastAPI( app = FastAPI(
title="Juggl API", title="Juggl API",
description=description, description=description,
contact={ # Contact information. See https://fastapi.tiangolo.com/tutorial/metadata/ contact={ # Optional contact information. See https://fastapi.tiangolo.com/tutorial/metadata/
"name": "Juggl Management", "name": "Juggl Management",
"url": "https://juggl.giller.dev/contact", "url": "https://juggl.giller.dev/contact",
"email": "help@juggl.giller.dev", "email": "help@juggl.giller.dev",
@ -27,6 +29,7 @@ app = FastAPI(
) )
"""The main FastAPI instance""" """The main FastAPI instance"""
# Enable CORS to allow requests from other domains/origins. # Enable CORS to allow requests from other domains/origins.
# See https://en.wikipedia.org/wiki/Cross-origin_resource_sharing # See https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
app.add_middleware( app.add_middleware(
@ -39,6 +42,14 @@ app.add_middleware(
allow_headers=["*"], allow_headers=["*"],
) )
@app.on_event("startup")
def on_startup():
"""Code executed on HTTP server startup. Until now only creating databases and tables."""
database.create_db_and_tables()
# Add subroutes # Add subroutes
app.include_router(project_router) app.include_router(project_router)
app.include_router(tag_router) app.include_router(tag_router)
@ -57,11 +68,11 @@ def parse_args() -> argparse.Namespace:
"""Parses command line arguments on application startup.""" """Parses command line arguments on application startup."""
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Juggl Backend v2", description="Juggl Backend API v2. Start a server listening on port.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter # show default values formatter_class=argparse.ArgumentDefaultsHelpFormatter # show default values
) )
parser.add_argument("--dev", default=False, action=argparse.BooleanOptionalAction, parser.add_argument("--dev", default=False, action=argparse.BooleanOptionalAction,
help="Start a dev server with auto-reloading") help="Enable auto-reloading on file-save")
parser.add_argument("--port", type=int, default=8192, help="Port on which the API should run") parser.add_argument("--port", type=int, default=8192, help="Port on which the API should run")
return parser.parse_args() return parser.parse_args()