Backends API¶
Backend¶
Backend ¶
Bases: ABC
Abstract base class for storage backends.
All backends (DynamoDB, OpenSearch, Composite, etc.) must implement this interface to provide consistent CRUD operations.
Each backend has an adapter that knows how to map models to the backend's native storage format.
Backends support mixin extensions via the extension discovery system. Mixins can define inner classes that extend BackendExtension to provide backend-specific behavior.
Initialize backend with an adapter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adapter
|
ModelAdapter
|
Adapter for mapping models to storage format |
required |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
Attributes¶
backend_name
abstractmethod
property
¶
Backend identifier (e.g., 'dynamodb', 'memory', 'opensearch').
Used by extensions to determine if they should be activated.
Functions¶
create
abstractmethod
¶
Create a new record in the backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class being created |
required |
data
|
dict[str, Any]
|
Dictionary of field values |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary of created record data |
Raises:
Type | Description |
---|---|
ValidationError
|
If data validation fails |
DuplicateKeyError
|
If unique constraint is violated |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
upsert
abstractmethod
¶
Create or update a record (upsert).
If a record with the same key exists, it will be overwritten. Unlike create(), this does not raise DuplicateKeyError.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class being upserted |
required |
data
|
dict[str, Any]
|
Dictionary of field values |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary of upserted record data |
Raises:
Type | Description |
---|---|
ValidationError
|
If data validation fails |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
get
abstractmethod
¶
Get a single record by primary key or unique field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to query |
required |
**filters
|
Any
|
Filter conditions (typically primary key) |
{}
|
Returns:
Type | Description |
---|---|
Optional[dict[str, Any]]
|
Dictionary of record data, or None if not found |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
update
abstractmethod
¶
Update an existing record.
The backend is responsible for extracting keys from the instance using the model's metadata (e.g., partition key, sort key decorators).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class being updated |
required |
instance
|
Model
|
The model instance with updated data |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary of updated record data |
Raises:
Type | Description |
---|---|
NotFoundError
|
If record doesn't exist |
ValidationError
|
If update data is invalid |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
delete
abstractmethod
¶
Delete a record.
The backend is responsible for extracting keys from the instance using the model's metadata (e.g., partition key, sort key decorators).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class being deleted |
required |
instance
|
Model
|
The model instance to delete |
required |
Returns:
Type | Description |
---|---|
bool
|
True if deleted, False if not found |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
query
abstractmethod
¶
Create a query builder for this backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to query |
required |
Returns:
Type | Description |
---|---|
QueryBuilder
|
QueryBuilder instance for fluent query construction |
Example
backend.query(User).filter(age__gte=18).limit(10).all()
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
count
abstractmethod
¶
Count records matching filters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to count |
required |
**filters
|
Any
|
Optional filter conditions |
{}
|
Returns:
Type | Description |
---|---|
int
|
Number of matching records |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
exists
abstractmethod
¶
Check if a record exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to check |
required |
**filters
|
Any
|
Filter conditions |
{}
|
Returns:
Type | Description |
---|---|
bool
|
True if at least one record matches, False otherwise |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
batch_create ¶
Create multiple records in a single batch operation.
Default implementation calls create() for each item. Backends should override with optimized batch operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class being created |
required |
items
|
list[dict[str, Any]]
|
List of record data dictionaries |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of created record data |
Raises:
Type | Description |
---|---|
ValidationError
|
If any item validation fails |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
batch_get ¶
Get multiple records by their keys in a single batch operation.
Default implementation calls get() for each key. Backends should override with optimized batch operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to query |
required |
keys
|
list[dict[str, Any]]
|
List of key dictionaries |
required |
Returns:
Type | Description |
---|---|
list[Optional[dict[str, Any]]]
|
List of record data (None for missing records) |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/base.py
initialize ¶
Initialize the backend (create tables, indexes, etc.).
Optional method for backends that need setup.
close ¶
Close connections and cleanup resources.
Optional method for backends with persistent connections.
InMemoryBackend¶
InMemoryBackend ¶
Bases: Backend
In-memory storage backend using Python dicts.
Stores all data in memory. Data is lost when the process ends. Useful for testing and examples.
Example
from restmachine_orm.backends.memory import InMemoryBackend from restmachine_orm.backends.adapters import InMemoryAdapter
class User(Model): ... class Meta: ... backend = InMemoryBackend(InMemoryAdapter()) ... id: str = Field(primary_key=True) ... name: str
user = User.create(id="123", name="Alice")
Initialize in-memory backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adapter
|
Optional[InMemoryAdapter]
|
Optional adapter (uses InMemoryAdapter by default) |
None
|
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
Attributes¶
Functions¶
create ¶
Create a new record in memory.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
upsert ¶
Create or update a record (upsert).
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
get ¶
Get a single record by primary key.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
update ¶
Update an existing record.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
delete ¶
Delete a record.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
query ¶
Create a query builder for this backend.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
count ¶
Count records matching filters.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
exists ¶
clear ¶
Clear storage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
Optional[type[Model]]
|
Optional model class to clear. If None, clears all. |
None
|
Source code in packages/restmachine-orm/src/restmachine_orm/backends/memory.py
InMemoryAdapter¶
InMemoryAdapter ¶
Bases: ModelAdapter
Adapter for in-memory storage (testing and examples).
Simple adapter that uses the primary key field as-is. No special transformation needed for in-memory dicts.
Functions¶
model_to_storage ¶
storage_to_model ¶
Return data as-is for model instantiation.
get_primary_key_value ¶
Get primary key value.
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
ModelAdapter¶
ModelAdapter ¶
Bases: ABC
Abstract adapter for mapping models to storage backends.
Each backend implements its own adapter that understands how to translate between the generic Model representation and the backend-specific storage format.
Functions¶
model_to_storage
abstractmethod
¶
Transform a model instance to backend storage format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Model
|
Model instance to transform |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary in backend-specific format |
Example (DynamoDB): >>> adapter.model_to_storage(user) { 'pk': 'USER#123', 'sk': 'METADATA', 'entity_type': 'User', 'id': '123', 'email': 'alice@example.com', 'name': 'Alice' }
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
storage_to_model
abstractmethod
¶
Transform backend storage format to model data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
The model class to instantiate |
required |
data
|
dict[str, Any]
|
Data from backend storage |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary suitable for model instantiation |
Example (DynamoDB): >>> adapter.storage_to_model(User, { ... 'pk': 'USER#123', ... 'sk': 'METADATA', ... 'entity_type': 'User', ... 'id': '123', ... 'email': 'alice@example.com' ... })
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
get_primary_key_value
abstractmethod
¶
Get the primary key value for a model instance.
For simple backends (like SQL), this is just the primary key field. For DynamoDB, this might be a composite key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Model
|
Model instance |
required |
Returns:
Type | Description |
---|---|
Any
|
Primary key value (simple or composite) |
Example
adapter.get_primary_key_value(user) '123' # Simple key adapter.get_primary_key_value(todo) {'pk': 'USER#alice', 'sk': 'TODO#2025-01-15'} # Composite
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
get_index_keys ¶
Get secondary index keys for a model instance.
Optional method for backends that support secondary indexes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance
|
Model
|
Model instance |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary of index names to key values |
Example (DynamoDB GSIs): >>> adapter.get_index_keys(user) { 'EmailIndex': {'gsi_pk': 'alice@example.com'}, 'TenantIndex': {'gsi_pk': 'TENANT#org-1', 'gsi_sk': '2025-01-15'} }
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
get_entity_type ¶
Get the entity type identifier for a model.
Used for filtering in single-table designs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
Returns:
Type | Description |
---|---|
str
|
Entity type identifier (typically the class name) |
Source code in packages/restmachine-orm/src/restmachine_orm/backends/adapters.py
Exceptions¶
NotFoundError¶
NotFoundError ¶
Bases: BackendError
Record not found in backend.
DuplicateKeyError¶
DuplicateKeyError ¶
Bases: BackendError
Unique constraint violation.