drug.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from typing import Optional
  2. from datetime import datetime, timezone
  3. from sqlalchemy import Column, String, Text, Integer, Float, DateTime, JSON, func
  4. from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
  5. from sqlalchemy.orm import Mapped, mapped_column
  6. from app.models.user import Base
  7. engine = create_async_engine(
  8. "postgresql+asyncpg://postgres:pharma2025@localhost:5432/pharmacopoeia",
  9. echo=False,
  10. pool_size=5,
  11. max_overflow=10,
  12. )
  13. async_session_factory = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
  14. def get_db():
  15. return async_session_factory
  16. class Drug(Base):
  17. __tablename__ = "drugs"
  18. id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
  19. drug_id: Mapped[str] = mapped_column(String(64), unique=True, nullable=False, index=True)
  20. name: Mapped[str] = mapped_column(String(256), nullable=False, index=True)
  21. name_en: Mapped[Optional[str]] = mapped_column(String(256))
  22. pinyin: Mapped[Optional[str]] = mapped_column(String(256))
  23. category: Mapped[Optional[str]] = mapped_column(String(64), index=True)
  24. subcategory: Mapped[Optional[str]] = mapped_column(String(128))
  25. approval_number: Mapped[Optional[str]] = mapped_column(String(64))
  26. sections: Mapped[Optional[dict]] = mapped_column(JSON)
  27. source_version: Mapped[Optional[str]] = mapped_column(String(32))
  28. source_volume: Mapped[Optional[str]] = mapped_column(String(32))
  29. source_page: Mapped[Optional[str]] = mapped_column(String(128))
  30. is_active: Mapped[bool] = mapped_column(default=True)
  31. created_at: Mapped[datetime] = mapped_column(
  32. DateTime(timezone=True), server_default=func.now()
  33. )
  34. updated_at: Mapped[datetime] = mapped_column(
  35. DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
  36. )
  37. class DrugChunk(Base):
  38. __tablename__ = "drug_chunks"
  39. id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
  40. drug_id: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
  41. section: Mapped[Optional[str]] = mapped_column(String(64))
  42. content: Mapped[str] = mapped_column(Text, nullable=False)
  43. source: Mapped[Optional[str]] = mapped_column(Text)
  44. chunk_index: Mapped[int] = mapped_column(default=0)
  45. embedding: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
  46. created_at: Mapped[datetime] = mapped_column(
  47. DateTime(timezone=True), server_default=func.now()
  48. )