Comprehensive API reference for the BaseRepository class, composite primary keys, query scoping, bulk operations, soft-delete lifecycles, and restoration methods.
BaseRepository provides standard asynchronous database access, combining Read, Write, Bulk Mutation, Soft-Delete Restoration, Keyset Pagination, and Search capabilities. It decouples business logic from SQLAlchemy 2.0 sessions, enforces context security through model-level query scoping, and natively supports composite primary keys with database partition pruning.
BaseRepository inspects all primary key columns defined on the model mapper without hardcoding single-column lookups:
Single Target Lookup (_build_pk_condition): Resolves an equality clause whether target is a model instance, a dictionary mapping ({"id": 1, "date": "..."}), a tuple/list ((1, "...")), or a scalar value.
Batch Multi-Key Operations (_build_ids_condition): When executing batch methods (get_by_ids, delete_multi, restore_multi), composite entities compile into standard SQL row-value constructor clauses:
(col1, col2) IN ((val1_a, val2_a), (val1_b, val2_b), ...)
This allows database engines (such as PostgreSQL) to execute Partition Pruning on partitioned tables (e.g., Range or List partitioning) rather than scanning all physical table partitions.
BaseRepository constructs its initial SELECT statements via _get_base_query().
If your SQLAlchemy model declares a classmethod named scope_query (or inherits from SoftDeleteMixin), _get_base_query() automatically passes the query through this hook before applying any route-level filters:
# Internal mechanics in BaseRepositorydef _get_base_query(self) -> Select: query = select(self.model) scoper = getattr(self.model, "scope_query", None) if scoper: return scoper(query) return querydef _supports_soft_delete(self) -> bool: """Determine whether the underlying model implements SoftDeleteMixin.""" return hasattr(self.model, "deleted_at") and hasattr(self.model, "soft_delete")
Fetches a sequence of records matching a list of primary keys in a single query. Supports scalar keys, composite key tuples, dictionary mappings, or model instances.
Deletes a single record by its model instance, scalar primary key, composite tuple, or composite mapping. Automatically applies soft-deletion if supported unless force=True.
Deletes multiple records matching the provided sequence of primary keys. Automatically executes an atomic soft-delete update (deleted_at = now()) unless force=True. Supports single and composite entities with partition pruning.
Restores multiple soft-deleted records using an atomic batch UPDATE ... WHERE deleted_at IS NOT NULL statement with dialect-aware RETURNING optimization and partition pruning.