Fields¶
Field descriptors for defining keys, indexes, TTL, and discriminators.
fields ¶
Field descriptors for Dynantic model definitions.
Provides Key, SortKey, GSIKey, GSISortKey, TTL,
and Discriminator field factories for defining DynamoDB table schemas.
Key ¶
Marks a Pydantic field as a DynamoDB Primary Key (Partition Key).
Usage
email: str = Key() item_id: UUID = Key(auto=True) # Auto-generates UUID4
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
auto
|
bool
|
If True, auto-generates a UUID4 on instantiation. Field should be typed as UUID. Cannot be combined with an explicit default. |
False
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Source code in dynantic/fields.py
SortKey ¶
Marks a Pydantic field as a DynamoDB Sort Key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
auto
|
bool
|
If True, auto-generates a UUID4 on instantiation. Field should be typed as UUID. Cannot be combined with an explicit default. |
False
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Source code in dynantic/fields.py
GSIKey ¶
Marks a Pydantic field as a Global Secondary Index (GSI) Partition Key.
Usage
customer_id: str = GSIKey(index_name="customer-index")
Architectural Note:¶
This function wraps the standard Pydantic Field. It injects a hidden flag ('_dynamo_gsi_pk') into 'json_schema_extra' along with the index name. The DynamoMeta metaclass will inspect this flag at class creation time to identify GSI partition keys and build the GSI metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_name
|
str
|
Name of the Global Secondary Index |
required |
default
|
Any
|
Default value for the field |
...
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Pydantic Field instance with GSI metadata |
Source code in dynantic/fields.py
GSISortKey ¶
Marks a Pydantic field as a Global Secondary Index (GSI) Sort Key.
Usage
order_date: str = GSISortKey(index_name="status-date-index")
Architectural Note:¶
This function wraps the standard Pydantic Field. It injects a hidden flag ('_dynamo_gsi_sk') into 'json_schema_extra' along with the index name. The DynamoMeta metaclass will inspect this flag at class creation time to identify GSI sort keys and build the GSI metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index_name
|
str
|
Name of the Global Secondary Index |
required |
default
|
Any
|
Default value for the field |
...
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Pydantic Field instance with GSI metadata |
Source code in dynantic/fields.py
TTL ¶
Marks a Pydantic field as the DynamoDB TTL (Time To Live) attribute.
The field should be typed as datetime (auto-converted to epoch seconds)
or int (passed through as epoch seconds).
Usage
expires_at: datetime = TTL()
Note
TTL must be enabled on the DynamoDB table separately (via Terraform/CLI). Dynantic only handles correct serialization of the field value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Pydantic Field instance with TTL metadata |
Source code in dynantic/fields.py
Discriminator ¶
Marks a Pydantic field as the discriminator for polymorphic models.
The discriminator field is used in single-table design to differentiate between different entity types stored in the same table.
Usage
entity_type: str = Discriminator()
Architectural Note:¶
This function wraps the standard Pydantic Field. It injects a hidden flag into json_schema_extra. The DynamoMeta metaclass will use this to: 1. Identify which field holds the discriminator value 2. Enable polymorphic deserialization in queries/scans
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
**kwargs
|
Any
|
Additional Pydantic Field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Pydantic Field instance with discriminator metadata |