Dyntastic

0.18.0 · active · verified Fri Apr 17

Dyntastic is a Python library that provides an intuitive Object-Document Mapper (ODM) for Amazon DynamoDB, built on top of Pydantic for data validation and `boto3` for AWS interaction. It simplifies schema definition, data serialization, and common DynamoDB operations. The current version is 0.18.0, with minor releases occurring approximately every 1-2 months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a DyntasticModel, save, retrieve, update, and delete items. It assumes DynamoDB is available at the configured region/host. The `__table_name__` and `__table_hash_key__` define the DynamoDB table structure. `table_create_args` in `Config` can be used to specify creation parameters like `BillingMode`.

import os
from dyntastic import DyntasticModel

# Set environment variable for DynamoDB region (or explicitly define __table_region__)
# For local development, you might set DYNTASTIC_HOST='http://localhost:8000'
os.environ['DYNTASTIC_REGION'] = os.environ.get('DYNTASTIC_REGION', 'us-east-1')

class User(DyntasticModel):
    __table_name__ = "DyntasticExampleUsers"
    __table_hash_key__ = "user_id"

    user_id: str
    name: str
    email: str
    age: int = 0

    class Config:
        table_create_args = {
            "BillingMode": "PAY_PER_REQUEST"
        }

# Create table if it doesn't exist (optional, auto-creates on first write by default)
# User.create_table()

# Create a new user
user_data = {"user_id": "user123", "name": "Alice", "email": "alice@example.com", "age": 30}
alice = User(**user_data)
alice.save() # This will create the table if it doesn't exist

print(f"Saved user: {alice}")

# Retrieve a user by their hash key
retrieved_user = User.get("user123")
print(f"Retrieved user: {retrieved_user}")

# Update user
if retrieved_user:
    retrieved_user.age = 31
    retrieved_user.save()
    print(f"Updated user age: {retrieved_user.age}")

# Delete user
if retrieved_user:
    retrieved_user.delete()
    print(f"Deleted user: {retrieved_user.user_id}")

# Clean up (optional: delete table)
# User.delete_table()

view raw JSON →