Bulk
plateforme.core.database.bulk
This module provides utilities for managing database sessions bulk within the Plateforme framework using SQLAlchemy features.
BulkData
module-attribute
A type alias for bulk data used in the bulk manager.
BulkSignature
module-attribute
A type alias for bulk fields signature used in query conditions.
BulkValue
module-attribute
A type alias for a bulk value used in query conditions.
BulkConditions
module-attribute
BulkConditions = dict[BulkSignature, list[BulkValue]]
A type alias that stores bulk conditions for the resolution.
BulkMap
module-attribute
A type alias that maps bulk entries to their hash values.
BulkQuery
module-attribute
BulkQuery = tuple[BulkSignature, Select[Any]]
A type alias for a bulk query used for the resolution.
BulkProcess
module-attribute
A type alias for a bulk process used for the resolution.
BulkResult
module-attribute
BulkResult = tuple[BulkSignature, Result[Any]]
A type alias for a bulk result used for the resolution.
BulkEntry
dataclass
BulkEntry(
*,
instance: BaseResource,
is_proxy: bool = False,
is_reference: bool = False,
)
A metadata class for bulk resource entries.
instance
class-attribute
instance-attribute
instance: BaseResource = field(kw_only=False)
The bulk entry resource instance.
is_proxy
class-attribute
instance-attribute
Whether the bulk entry resource instance is a proxy or not.
Bulk
Bulk(session: _T, *, proxy_reference: bool = True)
A bulk registry and operation manager for resources.
It is used to register resources for bulk operations and commit or rollback them in a streamlined manner. The bulk manager can be used to resolve resource references and update the resource instances with the resolved identifiers.
Attributes:
Name | Type | Description |
---|---|---|
session |
_T
|
The async or sync session used for the bulk operations. |
proxy_reference |
bool
|
Whether resource references should be encapsulated with a proxy or not. |
resolved |
BulkData
|
The resolved resource entries in the bulk. |
unresolved |
BulkData
|
The unresolved resource entries in the bulk. |
Initialize the session bulk manager.
The proxy option indicates that the provided resource references should be encapsulated with a proxy, this is done when validating the resource using the Pydantic core schema. This can be useful to resolve the references that target the same resource into a single instance. Thus, modifying a resolved instance will affect all references that target the same resource.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
_T
|
The session to use for the bulk operations. |
required |
proxy_reference
|
bool
|
Whether the registered resource references should
be encapsulated with a proxy or not. Defaults to |
True
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/database/bulk.py
add
add(
instance: BaseResource | BaseSpec,
*,
is_reference: bool = False,
) -> None
Add a resource instance to the bulk manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
BaseResource | BaseSpec
|
The resource instance to add to the bulk manager. |
required |
is_reference
|
bool
|
Whether the provided resource instance is a reference
or not. Defaults to |
False
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/database/bulk.py
get
get(
entity: ResourceType | SpecType,
*,
resolved: bool | None = None,
scope: Literal["all", "references", "values"] = "all",
) -> list[BaseResource]
Get the resource instances registered in the bulk manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity
|
ResourceType | SpecType
|
The resource type to get the resource instances for. |
required |
resolved
|
bool | None
|
Whether to get only the resolved resources or not:
- |
None
|
scope
|
Literal['all', 'references', 'values']
|
The scope of the resource instances to get:
- |
'all'
|
Returns:
Type | Description |
---|---|
list[BaseResource]
|
The list of resource instances registered in the bulk manager for |
list[BaseResource]
|
the specified resource type and options. |
Source code in .venv/lib/python3.12/site-packages/plateforme/core/database/bulk.py
resolve
abstractmethod
resolve(
*,
raise_errors: bool = True,
scope: Literal["all", "references", "values"] = "all",
strategy: Literal["bind", "hydrate"] = "bind",
) -> None | Awaitable[None]
Resolve the specified scope of resource entries in the bulk.
It must be implemented by the subclass either as a synchronous or asynchronous method to resolve the registered resource entries in the bulk.
For resource references, if the proxy_reference
option is enabled,
the resolved instances replace the reference proxy targets. Otherwise,
the resolved instances are used to update the reference proxy target.
For resource values, the resolved instances are used to update the resource instances with the fetched data, no proxy is used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raise_errors
|
bool
|
Whether to raise errors when failing to resolve
resource references or values. Defaults to |
True
|
scope
|
Literal['all', 'references', 'values']
|
The scope of the resources to resolve, it can be either:
- |
'all'
|
strategy
|
Literal['bind', 'hydrate']
|
The resolution strategy to use, it can be either:
- |
'bind'
|
Source code in .venv/lib/python3.12/site-packages/plateforme/core/database/bulk.py
generate_hash
Generate a hash for the provided value.