43 lines
1.7 KiB
Python
43 lines
1.7 KiB
Python
|
|
from sqlalchemy import Column, Integer, String, Text, DateTime, func
|
||
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||
|
|
from sqlalchemy.orm import DeclarativeBase
|
||
|
|
import config
|
||
|
|
|
||
|
|
engine = create_async_engine(config.DATABASE_URL, echo=False)
|
||
|
|
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||
|
|
|
||
|
|
|
||
|
|
class Base(DeclarativeBase):
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
class Book(Base):
|
||
|
|
__tablename__ = "books"
|
||
|
|
|
||
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||
|
|
book_id = Column(String(100), unique=True, nullable=False, index=True) # 平台书籍ID
|
||
|
|
title = Column(String(500), nullable=False) # 书名
|
||
|
|
author = Column(String(200), default="") # 作者
|
||
|
|
created_at = Column(DateTime, server_default=func.now())
|
||
|
|
|
||
|
|
|
||
|
|
class Chapter(Base):
|
||
|
|
__tablename__ = "chapters"
|
||
|
|
|
||
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||
|
|
book_id = Column(String(100), nullable=False, index=True) # 关联的书籍ID
|
||
|
|
chapter_id = Column(String(100), nullable=False, index=True) # 平台音频ID
|
||
|
|
app_chapter_id = Column(String(100), default="") # App章节ID
|
||
|
|
title = Column(String(500), default="") # 章节标题
|
||
|
|
text_content = Column(Text, default="") # TTS 文本内容
|
||
|
|
audio_file = Column(String(500), default="") # 生成的音频文件路径
|
||
|
|
status = Column(String(20), default="pending") # pending / generating / ready / error
|
||
|
|
error_msg = Column(Text, default="")
|
||
|
|
created_at = Column(DateTime, server_default=func.now())
|
||
|
|
updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())
|
||
|
|
|
||
|
|
|
||
|
|
async def init_db():
|
||
|
|
async with engine.begin() as conn:
|
||
|
|
await conn.run_sync(Base.metadata.create_all)
|