Base Repository¶
base_repository
¶
Base repository classes for common functionality.
Provides shared infrastructure for repository implementations.
BaseReadRepository
¶
Bases: ReadOnlyRepositoryPort[TEntity, TId]
Base class for read-only repositories with common functionality.
Provides in-memory storage and basic query operations.
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
__init__(storage: Mapping[TId, TEntity] | None = None) -> None
¶
Initialize the read repository with optional external storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
Mapping[TId, TEntity] | None
|
An optional mapping to use as backing storage. |
None
|
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
get_by_id(id: TId) -> TEntity | None
async
¶
Retrieve an entity by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
TId
|
Unique identifier of the resource. |
required |
Returns:
| Type | Description |
|---|---|
TEntity | None
|
The retrieved instance or None if not found. |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
list_all() -> Sequence[TEntity]
async
¶
Retrieve all resources in the repository.
Returns:
| Type | Description |
|---|---|
Sequence[TEntity]
|
A sequence of entity instances. |
find_matching(spec: Specification[TEntity]) -> Sequence[TEntity]
async
¶
Return all stored entities that satisfy the given specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
The specification predicate to filter by. |
required |
Returns:
| Type | Description |
|---|---|
Sequence[TEntity]
|
A list of matching entities. |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
count_matching(spec: Specification[TEntity]) -> int
async
¶
Return the count of entities satisfying the specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
The specification predicate to filter by. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The count of matching entities. |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
exists_matching(spec: Specification[TEntity]) -> bool
async
¶
Return True if at least one entity satisfies the specification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
Specification[TEntity]
|
The specification predicate to filter by. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if any entity matches, False otherwise. |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
BaseWriteRepository
¶
Bases: WriteOnlyRepositoryPort[TEntity, TId]
Base class for write-only repositories with common functionality.
Provides in-memory storage and basic write operations with ID validation.
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
__init__(storage: MutableMapping[TId, TEntity] | None = None) -> None
¶
Initialize the write repository with optional external storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
MutableMapping[TId, TEntity] | None
|
An optional mutable mapping to use as backing storage. |
None
|
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
delete_by_id(id: TId) -> None
async
¶
Delete an entity by ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
TId
|
Unique identifier of the entity. |
required |
Raises:
| Type | Description |
|---|---|
RepositoryError
|
If the ID is None, an empty string, |
RepositoryNotFoundError
|
If no entity exists with the given ID. |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
save(aggregate: TEntity) -> None
async
¶
Persist an entity instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
aggregate
|
TEntity
|
The entity to save. |
required |
Raises:
| Type | Description |
|---|---|
RepositoryError
|
If the entity has no valid identifier |
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
BaseRepository
¶
Bases: BaseReadRepository[TEntity, TId], BaseWriteRepository[TEntity, TId]
Full CRUD repository base class combining read and write operations.
Suitable for non-CQRS applications or simplified contexts.
Source code in src/forging_blocks/infrastructure/repositories/base_repository.py
__init__(storage: MutableMapping[TId, TEntity] | None = None) -> None
¶
Initialize the repository with optional external storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
storage
|
MutableMapping[TId, TEntity] | None
|
An optional mutable mapping to use as backing storage. |
None
|