Package: dynamorm
DynamORMThis package is a work in progress – Feedback / Suggestions / Etc welcomed!Python + DynamoDB ♡DynamORM (pronouncedDynamo-R-M) is a Python object & relation mapping library for Amazon’sDynamoDBservice.The project has two goals:Abstract away the interaction with the underlying DynamoDB libraries.  Python access to the DynamoDB service has evolved quickly, fromDynamo v1 in boto to Dynamo v2 in botoand then thenew resource model in boto3.  By providing a consistent interface that will feel familiar to users of other Python ORMs (SQLAlchemy, Django, Peewee, etc) means that we can always provide best-practices for queries and take advantages of new features without needing to refactor any application logic.Delegate schema validation and serialization to more focused libraries.  Building “ORM” semantics is “easy”, doing data validation and serialization is not.  We support bothMarshmallowandSchematicsfor building your object schemas.  You can take advantage of the full power of these libraries as they are transparently exposed in your code.Supported Schema Validation LibrariesSchematics>= 2.1.0Marshmallow>= 2.15.1ExampleimportdatetimefromdynamormimportDynaModel,GlobalIndex,ProjectAll# In this example we'll use Marshmallow, but you can also use Schematics too!# You can see that you have to import the schema library yourself, it is not abstracted at allfrommarshmallowimportfields# Our objects are defined as DynaModel classesclassBook(DynaModel):# Define our DynamoDB propertiesclassTable:name='prod-books'hash_key='isbn'read=25write=5classByAuthor(GlobalIndex):name='by-author'hash_key='author'read=25write=5projection=ProjectAll()# Define our data schema, each property here will become a property on instances of the Book classclassSchema:isbn=fields.String(validate=validate_isbn)title=fields.String()author=fields.String()publisher=fields.String()# NOTE: Marshmallow uses the `missing` keyword during deserialization, which occurs when we save# an object to Dynamo and the attr has no value, versus the `default` keyword, which is used when# we load a document from Dynamo and the value doesn't exist or is null.year=fields.Number(missing=lambda:datetime.datetime.utcnow().year)# Store new documents directly from dictionariesBook.put({"isbn":"12345678910","title":"Foo","author":"Mr. Bar","publisher":"Publishorama"})# Work with the classes as objects.  You can pass attributes from the schema to the constructorfoo=Book(isbn="12345678910",title="Foo",author="Mr. Bar",publisher="Publishorama")foo.save()# Or assign attributesfoo=Book()foo.isbn="12345678910"foo.title="Foo"foo.author="Mr. Bar"foo.publisher="Publishorama"# In all cases they go through Schema validation, calls to .put or .save can result in ValidationErrorfoo.save()# You can then fetch, query and scan your tables.# Get on the hash key, and/or range keybook=Book.get(isbn="12345678910")# Update items, with conditions# Here our condition ensures we don't have a race condition where someone else updates the title firstbook.update(title='Corrected Foo',conditions=(title=book.title,))# Query based on the keysBook.query(isbn__begins_with="12345")# Scan based on attributesBook.scan(author="Mr. Bar")Book.scan(author__ne="Mr. Bar")# Query based on indexesBook.ByAuthor.query(author="Mr. Bar")DocumentationFull documentation is built from the sources each build and can be found online at:https://nerdwalletoss.github.io/dynamorm/Thetests/also contain the most complete documentation on how to actually use the library, so you are encouraged to read through them to really familiarize yourself with some of the more advanced concepts and use cases.