feat: add database support
This commit is contained in:
parent
16b468d9ab
commit
79ec14516a
0
backend/__init__.py
Normal file
0
backend/__init__.py
Normal file
11
backend/database.py
Normal file
11
backend/database.py
Normal 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
BIN
backend/forum.db
Normal file
Binary file not shown.
@ -1,8 +1,12 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
import models
|
||||||
|
from database import engine
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
|
models.Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
class Post:
|
class Post:
|
||||||
"""Post is a single forum post or reply"""
|
"""Post is a single forum post or reply"""
|
||||||
|
21
backend/models.py
Normal file
21
backend/models.py
Normal 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)
|
@ -1,2 +1,3 @@
|
|||||||
fastapi
|
fastapi
|
||||||
uvicorn[standard]
|
uvicorn[standard]
|
||||||
|
sqlalchemy
|
||||||
|
Loading…
Reference in New Issue
Block a user