Created sqlite connection

This commit is contained in:
Yarne Coppens 2024-09-10 13:56:36 +02:00
parent 7318c6c81b
commit c8af5f6638
5 changed files with 25 additions and 8 deletions

1
.gitignore vendored
View file

@ -161,3 +161,4 @@ cython_debug/
#.idea/
.vscode/
db/database.db

View file

@ -9,7 +9,8 @@ class UserBase(SQLModel):
class UserPublic(UserBase):
username: str
email: str
full_name: str
class UserInDB(UserBase, table=True):
id: int | None = Field(default=None, primary_key=True)
hashed_password: str

View file

@ -1,4 +1,5 @@
from src.classes import product_classes
from src.modules import db_connection
melon = product_classes.Product(name="Meloen", price=2.0, barcode=1000 ,image_filename="melon")
@ -24,4 +25,7 @@ def get_all_products() -> list[product_classes.Product]:
def get_single_product(barcode: int) -> product_classes.Product:
for product in product_list:
if product.barcode == barcode:
return product
return product
def create_db_and_tables() -> None:
db_connection.create_db_and_tables()

View file

@ -2,14 +2,23 @@ from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
import os
from contextlib import asynccontextmanager
from src.classes import product_classes, cash_classes
from src.classes import product_classes, cash_classes, user_classes
from src.config import definitions
from src.modules import price_to_cash_calculator
from shop_validators import image_validator
from src import data_connection
app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
data_connection.create_db_and_tables()
yield
# Shutdown
app = FastAPI(lifespan=lifespan)
origins = [
"*" #TODO change this
@ -23,7 +32,6 @@ app.add_middleware(
allow_headers=["*"],
)
@app.get("/")
def read_root():
return {"Hello": "World"}

View file

@ -1,11 +1,14 @@
from sqlmodel import create_engine
from sqlmodel import create_engine, SQLModel
from src.config import definitions
sqlite_url = definitions.SQLITE_URL
connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=False, connect_args=connect_args)
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)
def get_engine():
return engine
return engine
def create_db_and_tables() -> None:
SQLModel.metadata.create_all(engine)