mirror of
https://github.com/bellingcat/whisperbox-transcribe.git
synced 2026-06-12 21:48:35 +03:00
feat: initial project setup
This commit is contained in:
0
app/db/__init__.py
Normal file
0
app/db/__init__.py
Normal file
18
app/db/base.py
Normal file
18
app/db/base.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from typing import Generator
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.config import settings
|
||||
|
||||
engine = create_engine(settings.DATABASE_URI)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def get_db() -> Generator[Session, None, None]:
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
23
app/db/dtos.py
Normal file
23
app/db/dtos.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class WithStandardFields(BaseModel):
|
||||
id: UUID
|
||||
created_at: datetime
|
||||
updated_at: Optional[datetime]
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class AccountBase(BaseModel):
|
||||
api_key: UUID
|
||||
name: str
|
||||
|
||||
|
||||
class Account(AccountBase, WithStandardFields):
|
||||
pass
|
||||
31
app/db/models.py
Normal file
31
app/db/models.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from typing import Optional
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import Column, DateTime, String, func
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import declarative_mixin, declared_attr, Mapped
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
@declarative_mixin
|
||||
class WithStandardFields:
|
||||
@declared_attr
|
||||
def created_at(cls) -> Mapped[DateTime]:
|
||||
return Column(DateTime, server_default=func.now(), nullable=False)
|
||||
|
||||
@declared_attr
|
||||
def updated_at(cls) -> Mapped[Optional[DateTime]]:
|
||||
return Column(DateTime, onupdate=func.now())
|
||||
|
||||
@declared_attr
|
||||
def id(cls) -> Mapped[UUID]:
|
||||
return Column(UUID(as_uuid=True), primary_key=True, index=True, default=uuid.uuid4)
|
||||
|
||||
|
||||
class Account(Base, WithStandardFields):
|
||||
__tablename__ = "accounts"
|
||||
|
||||
api_key = Column(UUID(as_uuid=True), index=True, default=uuid.uuid4)
|
||||
name = Column(String(length=256), unique=True)
|
||||
Reference in New Issue
Block a user