DynamoDB API Reference¶
DynamoDBBackend¶
DynamoDBBackend ¶
DynamoDBBackend(table_name: str, adapter: Optional[DynamoDBAdapter] = None, region_name: Optional[str] = None, endpoint_url: Optional[str] = None, **boto3_kwargs: Any)
Bases: Backend
DynamoDB storage backend.
Handles connection management, CRUD operations, and query building for AWS DynamoDB tables.
Example
from restmachine_orm.backends.dynamodb import DynamoDBBackend from restmachine_orm.backends.adapters import DynamoDBAdapter
backend = DynamoDBBackend( ... table_name="my-table", ... adapter=DynamoDBAdapter() ... )
class TodoItem(Model): ... class Meta: ... backend = backend ... user_id: str ... todo_id: str ... title: str ... @partition_key ... def pk(self) -> str: ... return f"USER#{self.user_id}" ... @sort_key ... def sk(self) -> str: ... return f"TODO#{self.todo_id}"
Initialize DynamoDB backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name
|
str
|
Name of the DynamoDB table |
required |
adapter
|
Optional[DynamoDBAdapter]
|
DynamoDBAdapter instance (uses default if not provided) |
None
|
region_name
|
Optional[str]
|
AWS region (e.g., 'us-east-1') |
None
|
endpoint_url
|
Optional[str]
|
Custom endpoint URL (for local DynamoDB) |
None
|
**boto3_kwargs
|
Any
|
Additional arguments for boto3.resource() |
{}
|
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
Functions¶
create ¶
Create a new record in DynamoDB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
data
|
dict[str, Any]
|
Record data |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Created record data |
Raises:
Type | Description |
---|---|
DuplicateKeyError
|
If item with same key already exists |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
upsert ¶
Create or update a record (upsert).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
data
|
dict[str, Any]
|
Record data |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Upserted record data |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
get ¶
Get a single record by key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
**filters
|
Any
|
Filter conditions (must include enough to build key) |
{}
|
Returns:
Type | Description |
---|---|
Optional[dict[str, Any]]
|
Record data, or None if not found |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
update ¶
Update an existing record.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
instance
|
Model
|
Model instance with updated data |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Updated record data |
Raises:
Type | Description |
---|---|
NotFoundError
|
If record not found |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
delete ¶
Delete a record.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
instance
|
Model
|
Model instance to delete |
required |
Returns:
Type | Description |
---|---|
bool
|
True if deleted successfully |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
query ¶
Create a query builder for complex queries.
Returns:
Type | Description |
---|---|
QueryBuilder
|
DynamoDBQueryBuilder instance |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
batch_create ¶
Create multiple records in batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
records
|
list[dict[str, Any]]
|
List of record data |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of created record data |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
batch_get ¶
Get multiple items by their keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_class
|
type[Model]
|
Model class |
required |
keys
|
list[dict[str, Any]]
|
List of key dicts (e.g., [{"id": "1"}, {"id": "2"}]) |
required |
Returns:
Type | Description |
---|---|
list[dict[str, Any]]
|
List of record data |
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/backend.py
DynamoDBAdapter¶
DynamoDBAdapter ¶
DynamoDBAdapter(*, pk_attribute: str = 'pk', sk_attribute: str = 'sk', entity_type_attribute: str = 'entity_type', include_type_in_sk: bool = False)
Bases: ModelAdapter
Adapter for DynamoDB single-table design.
Maps models to DynamoDB items with: - pk (partition key) - from @partition_key method or primary key field - sk (sort key) - from @sort_key method or default value - entity_type - for filtering different entities in same table - All model fields as attributes - GSI keys from @gsi_partition_key and @gsi_sort_key methods
Initialize DynamoDB adapter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pk_attribute
|
str
|
DynamoDB attribute name for partition key |
'pk'
|
sk_attribute
|
str
|
DynamoDB attribute name for sort key |
'sk'
|
entity_type_attribute
|
str
|
Attribute name for entity type |
'entity_type'
|
include_type_in_sk
|
bool
|
Whether to include entity type in sort key |
False
|
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/adapter.py
Functions¶
model_to_storage ¶
Transform model instance to DynamoDB item format.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/adapter.py
storage_to_model ¶
Transform DynamoDB item to model data.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/adapter.py
get_primary_key_value ¶
Get composite key for DynamoDB.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/adapter.py
Testing¶
DynamoDBDriver¶
DynamoDBDriver ¶
Bases: DriverInterface
Driver that tests against the DynamoDB backend.
This driver uses moto to mock DynamoDB for testing.
Initialize with DynamoDB backend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name
|
str
|
DynamoDB table name for testing |
'test-table'
|
region_name
|
str
|
AWS region name |
'us-east-1'
|
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
Functions¶
execute_create ¶
Execute a create operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
execute_get ¶
Execute a get operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
execute_update ¶
Execute an update operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
execute_delete ¶
Execute a delete operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
execute_upsert ¶
Execute an upsert operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
execute_query ¶
Execute a query operation.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
count ¶
Count instances matching filters.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
exists ¶
Check if instances exist matching filters.
Source code in packages/restmachine-orm-dynamodb/src/restmachine_orm_dynamodb/testing/drivers.py
clear ¶
Clear storage for testing - DynamoDB requires table scan and delete.