aws-pynamo 0.1.0b1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aws_pynamo-0.1.0b1/.gitignore +12 -0
- aws_pynamo-0.1.0b1/LICENSE +21 -0
- aws_pynamo-0.1.0b1/PKG-INFO +131 -0
- aws_pynamo-0.1.0b1/docs/README.md +114 -0
- aws_pynamo-0.1.0b1/pyproject.toml +59 -0
- aws_pynamo-0.1.0b1/src/pynamo/__init__.py +19 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/__init__.py +40 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/codec.py +303 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/errors.py +25 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/events.py +87 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/resolvers.py +138 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/router.py +212 -0
- aws_pynamo-0.1.0b1/src/pynamo/api/schema.py +468 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/__init__.py +29 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/base.py +701 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/collection.py +300 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/conditions.py +304 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/indexes.py +319 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/meta.py +140 -0
- aws_pynamo-0.1.0b1/src/pynamo/models/updates.py +279 -0
- aws_pynamo-0.1.0b1/src/pynamo/streams/__init__.py +21 -0
- aws_pynamo-0.1.0b1/src/pynamo/streams/events.py +97 -0
- aws_pynamo-0.1.0b1/src/pynamo/streams/exceptions.py +10 -0
- aws_pynamo-0.1.0b1/src/pynamo/streams/stream_handler.py +298 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kieran Bacon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: aws-pynamo
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Summary: Event-driven applications on DynamoDB and AWS AppSync, built from pydantic models.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
9
|
+
Requires-Python: >=3.14
|
|
10
|
+
Requires-Dist: boto3>=1.44.3
|
|
11
|
+
Requires-Dist: botocore>=1.44.3
|
|
12
|
+
Requires-Dist: mypy-boto3-dynamodb>=1.44.0
|
|
13
|
+
Requires-Dist: pydantic>=2.14.3
|
|
14
|
+
Provides-Extra: graphql
|
|
15
|
+
Requires-Dist: graphql-core>=3.3.2; extra == 'graphql'
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# pynamo
|
|
19
|
+
|
|
20
|
+
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:
|
|
21
|
+
|
|
22
|
+
1. **Stored**: [`pynamo.models`](models.md) reads and writes the models in a DynamoDB table, building every key from the model's own attributes.
|
|
23
|
+
2. **Served**: [`pynamo.api`](api.md) publishes functions that take and return the models as an AppSync GraphQL API, generating the schema from their type hints.
|
|
24
|
+
3. **Reacted to**: [`pynamo.streams`](streams.md) 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.
|
|
25
|
+
|
|
26
|
+
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:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
client ── GraphQL ──▶ AppSync ──▶ Lambda: pynamo.api ──▶ DynamoDB: pynamo.models
|
|
30
|
+
│
|
|
31
|
+
item change
|
|
32
|
+
▼
|
|
33
|
+
Lambda: pynamo.streams ◀───────── Kinesis data stream
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
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.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
pynamo needs Python 3.14 or later.
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
pip install aws-pynamo
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`pynamo.api` also needs `graphql-core`, which the `graphql` extra installs:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
pip install "aws-pynamo[graphql]"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## A model's life
|
|
53
|
+
|
|
54
|
+
One model, served through the API and reacted to when it changes:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
import logging
|
|
58
|
+
|
|
59
|
+
import boto3
|
|
60
|
+
|
|
61
|
+
from pynamo.api import Router
|
|
62
|
+
from pynamo.models import DynamoModel, single_table_indexes
|
|
63
|
+
from pynamo.streams import DynamoStreamHandler
|
|
64
|
+
|
|
65
|
+
DynamoModel.default_table(boto3.resource("dynamodb").Table("app"))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# Stored: keys are built from the model's attributes
|
|
69
|
+
class User(DynamoModel):
|
|
70
|
+
"""A user of the app."""
|
|
71
|
+
|
|
72
|
+
indexes = single_table_indexes(base=("USER#{user_id}", "USER"))
|
|
73
|
+
|
|
74
|
+
user_id: str
|
|
75
|
+
name: str
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# Served: the schema is generated from the resolvers' signatures
|
|
79
|
+
router = Router()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@router.query()
|
|
83
|
+
def get_user(user_id: str) -> User:
|
|
84
|
+
"""Get a user by ID."""
|
|
85
|
+
return User.get({"user_id": user_id})
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@router.mutation()
|
|
89
|
+
def rename_user(user_id: str, name: str) -> User:
|
|
90
|
+
"""Change a user's name."""
|
|
91
|
+
return User.update({"user_id": user_id}, {"name": name})
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Reacted to: each change arrives as the item before and after it
|
|
95
|
+
stream = DynamoStreamHandler()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@stream.handle(User, events=["MODIFY"])
|
|
99
|
+
def on_user_changed(old: User | None, new: User | None) -> None:
|
|
100
|
+
"""Record renames once they've been written."""
|
|
101
|
+
if old is not None and new is not None and old.name != new.name:
|
|
102
|
+
logging.info("User %s renamed from %s to %s", new.user_id, old.name, new.name)
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# Each function is deployed as its own Lambda
|
|
106
|
+
def api_handler(event, context):
|
|
107
|
+
return router(event, context)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def stream_handler(event, context):
|
|
111
|
+
return stream.lambda_handler(event, context)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
`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.
|
|
115
|
+
|
|
116
|
+
## Using the parts on their own
|
|
117
|
+
|
|
118
|
+
The three subpackages share models but don't depend on each other:
|
|
119
|
+
|
|
120
|
+
- `pynamo.models` is a complete DynamoDB ORM without the API or streams.
|
|
121
|
+
- `pynamo.api` serves any pydantic model, not just `DynamoModel`s, so it can front data stored elsewhere.
|
|
122
|
+
- `pynamo.streams` validates change images as any pydantic model, so it can consume changes from a table written by other code.
|
|
123
|
+
|
|
124
|
+
## Guides
|
|
125
|
+
|
|
126
|
+
- [Models](models.md): defining models and their keys, and reading and writing them with `get`, `query`, `scan`, `put`, `update` and `delete`.
|
|
127
|
+
- [GraphQL API](api.md): registering resolvers, how Python types map to GraphQL, and deploying the router to AppSync.
|
|
128
|
+
- [Streams](streams.md): registering change handlers, how a change finds its handler, and how failures and timeouts are retried.
|
|
129
|
+
- [Considerations](CONSIDERATIONS.md): the limitations and quirks of each part.
|
|
130
|
+
|
|
131
|
+
[`new_graphql_router.py`](new_graphql_router.py) is a complete AppSync API for users and their posts.
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# pynamo
|
|
2
|
+
|
|
3
|
+
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:
|
|
4
|
+
|
|
5
|
+
1. **Stored**: [`pynamo.models`](models.md) reads and writes the models in a DynamoDB table, building every key from the model's own attributes.
|
|
6
|
+
2. **Served**: [`pynamo.api`](api.md) publishes functions that take and return the models as an AppSync GraphQL API, generating the schema from their type hints.
|
|
7
|
+
3. **Reacted to**: [`pynamo.streams`](streams.md) 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.
|
|
8
|
+
|
|
9
|
+
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:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
client ── GraphQL ──▶ AppSync ──▶ Lambda: pynamo.api ──▶ DynamoDB: pynamo.models
|
|
13
|
+
│
|
|
14
|
+
item change
|
|
15
|
+
▼
|
|
16
|
+
Lambda: pynamo.streams ◀───────── Kinesis data stream
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
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.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
pynamo needs Python 3.14 or later.
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
pip install aws-pynamo
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
`pynamo.api` also needs `graphql-core`, which the `graphql` extra installs:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
pip install "aws-pynamo[graphql]"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## A model's life
|
|
36
|
+
|
|
37
|
+
One model, served through the API and reacted to when it changes:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import logging
|
|
41
|
+
|
|
42
|
+
import boto3
|
|
43
|
+
|
|
44
|
+
from pynamo.api import Router
|
|
45
|
+
from pynamo.models import DynamoModel, single_table_indexes
|
|
46
|
+
from pynamo.streams import DynamoStreamHandler
|
|
47
|
+
|
|
48
|
+
DynamoModel.default_table(boto3.resource("dynamodb").Table("app"))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
# Stored: keys are built from the model's attributes
|
|
52
|
+
class User(DynamoModel):
|
|
53
|
+
"""A user of the app."""
|
|
54
|
+
|
|
55
|
+
indexes = single_table_indexes(base=("USER#{user_id}", "USER"))
|
|
56
|
+
|
|
57
|
+
user_id: str
|
|
58
|
+
name: str
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
# Served: the schema is generated from the resolvers' signatures
|
|
62
|
+
router = Router()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@router.query()
|
|
66
|
+
def get_user(user_id: str) -> User:
|
|
67
|
+
"""Get a user by ID."""
|
|
68
|
+
return User.get({"user_id": user_id})
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@router.mutation()
|
|
72
|
+
def rename_user(user_id: str, name: str) -> User:
|
|
73
|
+
"""Change a user's name."""
|
|
74
|
+
return User.update({"user_id": user_id}, {"name": name})
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Reacted to: each change arrives as the item before and after it
|
|
78
|
+
stream = DynamoStreamHandler()
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@stream.handle(User, events=["MODIFY"])
|
|
82
|
+
def on_user_changed(old: User | None, new: User | None) -> None:
|
|
83
|
+
"""Record renames once they've been written."""
|
|
84
|
+
if old is not None and new is not None and old.name != new.name:
|
|
85
|
+
logging.info("User %s renamed from %s to %s", new.user_id, old.name, new.name)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
# Each function is deployed as its own Lambda
|
|
89
|
+
def api_handler(event, context):
|
|
90
|
+
return router(event, context)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def stream_handler(event, context):
|
|
94
|
+
return stream.lambda_handler(event, context)
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`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.
|
|
98
|
+
|
|
99
|
+
## Using the parts on their own
|
|
100
|
+
|
|
101
|
+
The three subpackages share models but don't depend on each other:
|
|
102
|
+
|
|
103
|
+
- `pynamo.models` is a complete DynamoDB ORM without the API or streams.
|
|
104
|
+
- `pynamo.api` serves any pydantic model, not just `DynamoModel`s, so it can front data stored elsewhere.
|
|
105
|
+
- `pynamo.streams` validates change images as any pydantic model, so it can consume changes from a table written by other code.
|
|
106
|
+
|
|
107
|
+
## Guides
|
|
108
|
+
|
|
109
|
+
- [Models](models.md): defining models and their keys, and reading and writing them with `get`, `query`, `scan`, `put`, `update` and `delete`.
|
|
110
|
+
- [GraphQL API](api.md): registering resolvers, how Python types map to GraphQL, and deploying the router to AppSync.
|
|
111
|
+
- [Streams](streams.md): registering change handlers, how a change finds its handler, and how failures and timeouts are retried.
|
|
112
|
+
- [Considerations](CONSIDERATIONS.md): the limitations and quirks of each part.
|
|
113
|
+
|
|
114
|
+
[`new_graphql_router.py`](new_graphql_router.py) is a complete AppSync API for users and their posts.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "aws-pynamo"
|
|
3
|
+
version = "0.1.0b1"
|
|
4
|
+
description = "Event-driven applications on DynamoDB and AWS AppSync, built from pydantic models."
|
|
5
|
+
readme = "docs/README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.14"
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 4 - Beta",
|
|
11
|
+
"Programming Language :: Python :: 3.14",
|
|
12
|
+
]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"boto3>=1.44.3",
|
|
15
|
+
"botocore>=1.44.3",
|
|
16
|
+
"mypy-boto3-dynamodb>=1.44.0",
|
|
17
|
+
"pydantic>=2.14.3",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.optional-dependencies]
|
|
21
|
+
graphql = [
|
|
22
|
+
"graphql-core>=3.3.2",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
# Build with hatchling, shipping only the pynamo package (not the stray root __init__.py)
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["hatchling>=1.27"]
|
|
28
|
+
build-backend = "hatchling.build"
|
|
29
|
+
|
|
30
|
+
[tool.hatch.build.targets.wheel]
|
|
31
|
+
packages = ["src/pynamo"]
|
|
32
|
+
|
|
33
|
+
[tool.hatch.build.targets.sdist]
|
|
34
|
+
include = ["src/pynamo", "docs/README.md", "LICENSE"]
|
|
35
|
+
|
|
36
|
+
[dependency-groups]
|
|
37
|
+
dev = [
|
|
38
|
+
"pytest>=9.0.0",
|
|
39
|
+
"pytest-cov>=7.1.0",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[tool.pytest.ini_options]
|
|
43
|
+
testpaths = ["tests"]
|
|
44
|
+
# Measure coverage of the package on every run, reported in the terminal and as HTML
|
|
45
|
+
addopts = [
|
|
46
|
+
"--import-mode=importlib",
|
|
47
|
+
"--cov=pynamo",
|
|
48
|
+
"--cov-report=term-missing:skip-covered",
|
|
49
|
+
"--cov-report=html",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[tool.coverage.run]
|
|
53
|
+
# Count branches taken, not only lines run
|
|
54
|
+
branch = true
|
|
55
|
+
source = ["pynamo"]
|
|
56
|
+
|
|
57
|
+
[tool.coverage.html]
|
|
58
|
+
# Write the HTML report to htmlcov/, which is gitignored
|
|
59
|
+
directory = "htmlcov"
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Event-driven applications on DynamoDB and AWS AppSync, built from pydantic models.
|
|
2
|
+
|
|
3
|
+
A model is defined once and used at every stage of a change's life:
|
|
4
|
+
|
|
5
|
+
- `pynamo.models` stores models in a DynamoDB table as `DynamoModel`s.
|
|
6
|
+
- `pynamo.api` serves them through AppSync, with a `Router` as a direct Lambda resolver.
|
|
7
|
+
- `pynamo.streams` reacts to their changes, delivered through Kinesis, with a
|
|
8
|
+
`DynamoStreamHandler`.
|
|
9
|
+
|
|
10
|
+
Each subpackage is imported on its own; `pynamo.api` needs the `graphql` extra.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .models import (
|
|
14
|
+
DynamoModel,
|
|
15
|
+
GlobalSecondaryIndex,
|
|
16
|
+
LocalIndex,
|
|
17
|
+
PrimaryIndex,
|
|
18
|
+
single_table_indexes,
|
|
19
|
+
)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"""Serve pydantic models through AWS AppSync, with Lambda as a direct resolver.
|
|
2
|
+
|
|
3
|
+
`events` types the AppSync Lambda event, `errors` the exceptions raised, `resolvers`
|
|
4
|
+
the functions resolving root fields, `schema` the GraphQL schema built from them,
|
|
5
|
+
`codec` the conversion of arguments and results, and `router` the `Router` tying them
|
|
6
|
+
together. The public API is re-exported here.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from .errors import ArgumentError, GraphQLApiError, ResolverNotFoundError, SchemaError
|
|
10
|
+
from .events import (
|
|
11
|
+
AppSyncEvent,
|
|
12
|
+
AppSyncInfo,
|
|
13
|
+
AppSyncRequest,
|
|
14
|
+
CognitoIdentity,
|
|
15
|
+
IamIdentity,
|
|
16
|
+
Identity,
|
|
17
|
+
LambdaIdentity,
|
|
18
|
+
OidcIdentity,
|
|
19
|
+
PreviousResult,
|
|
20
|
+
)
|
|
21
|
+
from .router import Router
|
|
22
|
+
from .schema import NameConverter
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"AppSyncEvent",
|
|
26
|
+
"AppSyncInfo",
|
|
27
|
+
"AppSyncRequest",
|
|
28
|
+
"ArgumentError",
|
|
29
|
+
"CognitoIdentity",
|
|
30
|
+
"GraphQLApiError",
|
|
31
|
+
"IamIdentity",
|
|
32
|
+
"Identity",
|
|
33
|
+
"LambdaIdentity",
|
|
34
|
+
"NameConverter",
|
|
35
|
+
"OidcIdentity",
|
|
36
|
+
"PreviousResult",
|
|
37
|
+
"ResolverNotFoundError",
|
|
38
|
+
"Router",
|
|
39
|
+
"SchemaError",
|
|
40
|
+
]
|