Coverage for geoparser/db/crud/document.py: 100%
20 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 18:08 +0000
1import typing as t
2import uuid
4from sqlmodel import Session, select
6from geoparser.db.crud.base import BaseRepository
7from geoparser.db.models import Document
10class DocumentRepository(BaseRepository[Document]):
11 """
12 Repository for Document model operations.
13 """
15 model = Document
17 @classmethod
18 def get_by_project(cls, db: Session, project_id: uuid.UUID) -> t.List[Document]:
19 """
20 Get all documents for a project.
22 Args:
23 db: Database session
24 project_id: Project ID
26 Returns:
27 List of documents
28 """
29 statement = select(Document).where(Document.project_id == project_id)
30 return db.exec(statement).unique().all()
32 @classmethod
33 def get_by_ids(
34 cls, db: Session, project_id: uuid.UUID, ids: t.Sequence[uuid.UUID]
35 ) -> t.List[Document]:
36 """
37 Get the documents with the given IDs that belong to a project.
39 IDs that do not exist or belong to another project are ignored, and the
40 results are returned in no particular order.
42 Args:
43 db: Database session
44 project_id: Project ID
45 ids: Document IDs to retrieve
47 Returns:
48 List of documents
49 """
50 documents = []
52 # Query in chunks to stay below the SQLite limit on bound parameters
53 chunk_size = 500
54 for offset in range(0, len(ids), chunk_size):
55 chunk = ids[offset : offset + chunk_size]
56 statement = select(Document).where(
57 Document.project_id == project_id, Document.id.in_(chunk)
58 )
59 documents.extend(db.exec(statement).unique().all())
61 return documents