Add new deps, writen a db engine, docker-compose and .env-example
This commit is contained in:
5
backend/app/.env-example
Normal file
5
backend/app/.env-example
Normal file
@@ -0,0 +1,5 @@
|
||||
POSTGRES_USER=
|
||||
POSTGRES_PASSWORD=
|
||||
POSTGRES_DB=
|
||||
POSTGRES_HOST=
|
||||
POSTGRES_PORT=
|
||||
42
backend/app/db_engine.py
Normal file
42
backend/app/db_engine.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
import dotenv
|
||||
import os
|
||||
|
||||
dotenv.load_dotenv()
|
||||
|
||||
PG_USER = dotenv.get_key(".env", "PG_USER")
|
||||
PG_PASSWORD = dotenv.get_key(".env", "PG_PASSWORD")
|
||||
PG_HOST = dotenv.get_key(".env", "PG_HOST")
|
||||
PG_PORT = dotenv.get_key(".env", "PG_PORT")
|
||||
PG_DB = dotenv.get_key(".env", "PG_DB")
|
||||
|
||||
DATABASE_URL = (
|
||||
f"postgresql+asyncpg://{PG_USER}:{PG_PASSWORD}@{PG_HOST}:{PG_PORT}/{PG_DB}"
|
||||
)
|
||||
|
||||
engine = create_async_engine(
|
||||
DATABASE_URL,
|
||||
echo=False,
|
||||
future=True,
|
||||
)
|
||||
|
||||
async_session_maker = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False,
|
||||
)
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
async def get_async_session():
|
||||
async with async_session_maker() as session:
|
||||
yield session
|
||||
|
||||
|
||||
async def init_db():
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
0
backend/app/models/guestbook.py
Normal file
0
backend/app/models/guestbook.py
Normal file
15
backend/app/models/posts.py
Normal file
15
backend/app/models/posts.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db_engine import Base
|
||||
|
||||
|
||||
class Post(Base):
|
||||
__tablename__ = "posts"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True, autoincrement=True, nullable=False)
|
||||
title = Column(String, index=True, nullable=False)
|
||||
content = Column(String, index=True, nullable=False)
|
||||
images = Column(String, index=True)
|
||||
updated_at = Column(DateTime, index=True)
|
||||
|
||||
Reference in New Issue
Block a user