feat: add database support

This commit is contained in:
agatha 2024-04-01 00:43:12 -04:00
parent 16b468d9ab
commit 79ec14516a
6 changed files with 37 additions and 0 deletions

0
backend/__init__.py Normal file
View File

11
backend/database.py Normal file
View File

@ -0,0 +1,11 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
SQLALCHEMY_DATABASE_URL = 'sqlite:///./forum.db'
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={'check_same_thread': False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

BIN
backend/forum.db Normal file

Binary file not shown.

View File

@ -1,8 +1,12 @@
from fastapi import FastAPI
import models
from database import engine
from pydantic import BaseModel
app = FastAPI()
models.Base.metadata.create_all(bind=engine)
class Post:
"""Post is a single forum post or reply"""

21
backend/models.py Normal file
View File

@ -0,0 +1,21 @@
from database import Base
from sqlalchemy import Column, Integer, String, Boolean, ForeignKey
class Posts(Base):
__tablename__ = 'posts'
id = Column(Integer, primary_key=True)
thread_id = Column(Integer, ForeignKey("threads.id"))
author = Column(String)
title = Column(String)
content = Column(String)
class Threads(Base):
__tablename__ = 'threads'
id = Column(Integer, primary_key=True)
author = Column(String)
title = Column(String)
content = Column(String)

View File

@ -1,2 +1,3 @@
fastapi
uvicorn[standard]
sqlalchemy