Metadata-Version: 2.5
Name: aws-pynamo
Version: 0.1.0b7
Summary: Event-driven applications on DynamoDB and AWS AppSync, built from pydantic models.
Project-URL: Documentation, https://aws-pynamo.readthedocs.io
Project-URL: Repository, https://github.com/Kieran-Bacon/pynamo
Project-URL: Changelog, https://aws-pynamo.readthedocs.io/en/latest/changelog/
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python :: 3.14
Requires-Python: >=3.14
Requires-Dist: boto3>=1.44.3
Requires-Dist: botocore>=1.44.3
Requires-Dist: mypy-boto3-cognito-idp>=1.44.0
Requires-Dist: mypy-boto3-dynamodb>=1.44.0
Requires-Dist: pydantic>=2.14.3
Provides-Extra: graphql
Requires-Dist: graphql-core>=3.3.2; extra == 'graphql'
Description-Content-Type: text/markdown

# pynamo

[Documentation](https://aws-pynamo.readthedocs.io/en/latest/) · [Source](https://github.com/Kieran-Bacon/pynamo) · [Changelog](https://aws-pynamo.readthedocs.io/en/latest/changelog/)

pynamo is a toolkit for building event-driven, serverless applications on DynamoDB and AWS AppSync with pydantic. You describe your data once, as pydantic models, and pynamo uses those same models at every stage of a change's life:

1. **Stored**: [`pynamo.models`](https://aws-pynamo.readthedocs.io/en/latest/models/) reads and writes the models in a DynamoDB table, building every key from the model's own attributes.
2. **Served**: [`pynamo.api`](https://aws-pynamo.readthedocs.io/en/latest/api/) publishes functions that take and return the models as an AppSync GraphQL API, generating the schema from their type hints.
3. **Reacted to**: [`pynamo.streams`](https://aws-pynamo.readthedocs.io/en/latest/streams/) hands every change to an item back to your code as a before and after pair of models, so work can follow on from a write without the writer knowing about it.

A change moves through an application like a tide. It comes in through the API, is written to the table, and then flows back out through the stream to whatever reacts to it:

```
client ── GraphQL ──▶ AppSync ──▶ Lambda: pynamo.api ──▶ DynamoDB: pynamo.models
                                                              │
                                                         item change
                                                              ▼
                       Lambda: pynamo.streams ◀───────── Kinesis data stream
```

At every step the data is validated as the same pydantic class, so a field added to a model is stored, published in the schema and delivered to stream handlers without being declared anywhere else.

## Installation

pynamo needs Python 3.14 or later.

```
pip install aws-pynamo
```

`pynamo.api` also needs `graphql-core`, which the `graphql` extra installs:

```
pip install "aws-pynamo[graphql]"
```

## A model's life

One model, served through the API and reacted to when it changes:

```python
import logging

import boto3

from pynamo.api import Router
from pynamo.models import DynamoModel, single_table_indexes
from pynamo.streams import DynamoStreamHandler

DynamoModel.default_table(boto3.resource("dynamodb").Table("app"))


# Stored: keys are built from the model's attributes
class User(DynamoModel):
    """A user of the app."""

    user_id: str
    name: str

    class Meta:
        indexes = single_table_indexes(base=("USER#{user_id}", "USER"))


# Served: the schema is generated from the resolvers' signatures
router = Router()


@router.query()
def get_user(user_id: str) -> User:
    """Get a user by ID."""
    return User.get({"user_id": user_id})


@router.mutation()
def rename_user(user_id: str, name: str) -> User:
    """Change a user's name."""
    return User.update({"user_id": user_id}, {"name": name})


# Reacted to: each change arrives as the item before and after it
stream = DynamoStreamHandler()


@stream.handle(User, events=["MODIFY"])
def on_user_changed(old: User | None, new: User | None) -> None:
    """Record renames once they've been written."""
    if old is not None and new is not None and old.name != new.name:
        logging.info("User %s renamed from %s to %s", new.user_id, old.name, new.name)


# Each function is deployed as its own Lambda
def api_handler(event, context):
    return router(event, context)


def stream_handler(event, context):
    return stream.lambda_handler(event, context)
```

`rename_user` doesn't know that anything is listening, and `on_user_changed` doesn't know where the change came from. A rename made by an admin script, a migration or another service reaches the same handler.

## Using the parts on their own

The three subpackages share models but don't depend on each other:

- `pynamo.models` is a complete DynamoDB ORM without the API or streams.
- `pynamo.api` serves any pydantic model, not just `DynamoModel`s, so it can front data stored elsewhere.
- `pynamo.streams` validates change images as any pydantic model, so it can consume changes from a table written by other code.

`pynamo.types` holds common field types, such as UTC datetimes and generated IDs, that every part can use.

## Guides

- [Models](https://aws-pynamo.readthedocs.io/en/latest/models/): defining models and their keys, reading and writing them with `get`, `query`, `scan`, `put`, `update`, `delete` and `remove`, and querying several models at once with `DynamoCollection`.
- [GraphQL API](https://aws-pynamo.readthedocs.io/en/latest/api/): registering resolvers, how Python types map to GraphQL, and deploying the router to AppSync.
- [Streams](https://aws-pynamo.readthedocs.io/en/latest/streams/): registering change handlers, how a change finds its handler, and how failures and timeouts are retried.
- [Types](https://aws-pynamo.readthedocs.io/en/latest/types/): shared field types that are safe to store, such as `Datetime`, `UUID`, `UUID4`, `Email` and `UKAddress`.
- [API reference](https://aws-pynamo.readthedocs.io/en/latest/reference/models/): every public class and function, generated from the docstrings.
- [Considerations](https://aws-pynamo.readthedocs.io/en/latest/CONSIDERATIONS/): the limitations and quirks of each part.
