"""SQLite-слой для raw-текстов документов — для переиндексации.""" import logging from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from db.models import Document logger = logging.getLogger(__name__) async def save_document_raw( session: AsyncSession, document_id: str, name: str, file_type: str, raw_text: str, ) -> None: """Сохранить (или перезаписать) исходный текст документа в SQLite.""" existing = await session.get(Document, document_id) if existing: existing.name = name existing.file_type = file_type existing.raw_text = raw_text else: session.add(Document( id=document_id, name=name, file_type=file_type, raw_text=raw_text, )) await session.commit() async def get_document_raw(session: AsyncSession, document_id: str) -> Document | None: return await session.get(Document, document_id) async def list_documents_raw(session: AsyncSession) -> list[Document]: stmt = select(Document).order_by(Document.created_at) return list((await session.execute(stmt)).scalars().all()) async def delete_document_raw(session: AsyncSession, document_id: str) -> bool: doc = await session.get(Document, document_id) if doc is None: return False await session.delete(doc) await session.commit() return True