Skip to main content

Pydantic base models for DynamoDB, with easy to use save/delete/query/get.

Project description

PythonSupport PyPI version

(documentation below is a work in progress)

Quick Start

[!WARNING] This is pre-release Beta software, based on another code base and the needed changes to make a final release version are not yet completed. Everything is subject to change!

uv add dynamo-pydantic

or

pip install dynamo-pydantic

Very basic example:

from dynamo_pydantic import DynamoModel, HashField, SortField

class MyModel(DynamoModel):
    some_attr: HashField[str]
    another_attr: str

obj = MyModel(some_attr='a-value', another_attr='b-value')

# Saves into a dynamo table called `myModel`.
# (See DynObjManager, accessed via DynamoModel.dyn_objs; for more docs around table naming options)
obj.dyn_save()

# Query for object
assert MyModel.dyn_objs.get_first({'some_attr': 'a-value'}).another_attr == 'b-value'

# Delete Object
obj.dyn_delete()

# Not There Anymore.
assert next(MyModel.dyn_objs.get({'some_attr': 'a-value'}), None) is None

Detail Docs

📄 Detailed Documentation | 🐍 PyPi

Overview

This is a port of my old xdynamo library to use pydantic.

Allows easy use of Pydantic models with DynamoDB using a Django-like query syntax.

Right now you use a dict to communicate query filtering:

MyModel.dyn_objs.get({'some_hash_field': 'some-value'})

In the future, I'll have a simple QuerySet like object/class for filtering, modeled somewhat on Django's QuerySet class.

But for now, I am starting with .get(...), with the objective being you can directly communicate url-query params into this get to grab desired objects.

The dyn_objs manager object will figure out the best way to query DynamoDB based on the query attributes/values you provide. Sort of a much simplified version of a relational-database query planner.

This allows for one to focus on querying your model and dynamo in various ways without having to figure out exactly the best way in every possible case when you have the potential for both simple and more complex queries.

Right now, secondary/global indexes are not supported. They will be added in a future update. When indexes get added, the plan is for the simplified query planner to automatically use the index as needed.

The get method also by default won't do scans when given a query. Unless you tell it via a parameter allow_scan=True to tell query-panner if it's ok to automatically drop down into a scan if it's the only way to do a query.

In addition to the general get method that does query-planning, there are also a set of explicit methods for ensuring you always do a query, scan, etc.; as other manager methods .query(...), .scan(...), etc.

You can bulk-save objects via MyModel.dyn_objs.save([obj1, obj2, ...]), or single-save them via my_obj.dyn_save() or MyModel.dyn_objs.save(my_obj).

Quick high-level overview of features

  • Supports multiple hash/sort fields

    • Hash/Sort fields are defined on class, the hash/sort-key is defined on table.
    • If there are multiple hash or sort fields, they are combined into their respective hash/sort key via a deliminator.
    • The deliminator does not have to be unique/unused in field data because it won't need to be parsed later, it is only used to make the key properly unique to object.
    • By default, these key-fields are inherited, so you can add more hash/sort fields in subclasses, and these will be combined with any defined in the parent-class.
    • The individual hahs/sort fields are still stored separately into their own attribute in the dynamo table item.
    • When querying for an item, provide a value for each hash field (and optionally any sort fields).
      • They will automatically be combined and used for the final hash/sort key for table query.
  • Use dynamo_pydantic.UtcDateTime type to force a datetime to always be and serialize into utc.

    • You can use this with hash and/or sort keys with datetime in them, so they can reliably be queried for.
    • Because we must use a string for a datetime, so if format and timezone are always the same it can be reliably sorted and queried for.
    • Use UtcDateTimeNow to default value to now in utc.
    • Example Field: created_at: UtcDateTimeNow
  • Can Auto-create tables lazy, on first use if desired.

    • By default, this won't happen.
    • If you set dynamo_pydantic.dyn_settings.create_tables_if_needed = True, it will create the tables lazily.

More Docs Soon To Come

I'll have more, detailed documentation soon, along with examples.

I need to rewrite my old docs from the old library for the new way of doing things as I took this opportunity to simplify and rearranged things (compared to old library).

For now, I have a basic outline above, and some refrence documentation here:

📄 Detailed Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamo_pydantic-0.1.1.tar.gz (40.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamo_pydantic-0.1.1-py3-none-any.whl (44.1 kB view details)

Uploaded Python 3

File details

Details for the file dynamo_pydantic-0.1.1.tar.gz.

File metadata

  • Download URL: dynamo_pydantic-0.1.1.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dynamo_pydantic-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aad83414a3d4ad58658511eb2e18835e0e215e9cba858b0c7d8d92ab6258241f
MD5 550f8acbd3a6e882b0535031a641ea15
BLAKE2b-256 69e12f947484241d4f82d96464866c52e10d38e54d850d1541cac047e57977b4

See more details on using hashes here.

File details

Details for the file dynamo_pydantic-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: dynamo_pydantic-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 44.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dynamo_pydantic-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 23f0acfdb5c20b4ca09446cd7dd8d94a965ee5ea26e29b0dde42451c28eac0ad
MD5 d48450ec51adefcea6864610dab49815
BLAKE2b-256 6f1e128ef5d2fed27848c56f0231cd5cd6382f9296e2c37de3f6cd89c6ef1106

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page