Skip to main content

Calculated/copied attributes to support advanced single-table design in DynamoDB

Project description

PynamoDB Polymorph

This package has utilities that are useful when using PynamoDB with a single-table (polymorphic) design and overloading Global Secondary Indexes.

Take for example a Publisher class that needs to use a compound key of their type prefix and their name. If you rely on callers doing the copy in code there is possibility for them to update the name without also updating the GSI sort key that contains the name. The CompoundTemplateAttribute uses string.Template templating to fill unicode or numeric attributes into a string template to build keys automatically.

class Publisher(Base, discriminator="Publisher"):
    name = UnicodeAttribute()
    description = UnicodeAttribute(default="")
    gsi1_pk = CopiedDiscriminatorAttribute(source="cls")

    _slug = "PUBLISHER"
    gsi1_sk = JoinedUnicodeAttribute(attrs=["_slug", 'name'], sep='#')

    # Alternative way to specify gsi1_sk using a `string.Template`.
    # This is best for cases that aren't simply joining attrs with a character
    # gsi1_sk = CompoundTemplateAttribute(
    #     template="PUBLISHER#$name",
    #     attrs=["name"],
    # )

The template above will take any value in name and fill it in to the template, so name="Random Book Publishing House" will become a gsi1_sk="PUBLISHER#Random Book Publishing House".

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

pynamodb-polymorph-0.4.0.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file pynamodb-polymorph-0.4.0.tar.gz.

File metadata

  • Download URL: pynamodb-polymorph-0.4.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for pynamodb-polymorph-0.4.0.tar.gz
Algorithm Hash digest
SHA256 42942750e68e31cf2a2dc138f980f3ebea012f6be80c68184aa253b6ce5d3ca1
MD5 2551c3d4bcacf69d16a5efbea4c19428
BLAKE2b-256 41cece09295de70b085c1b2ee0b5f58687181e954654256edf686855d4b498fe

See more details on using hashes here.

Supported by

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