Skip to main content

An Async Pythonic Interface to DynamoDB

Project description

Work in progress. Careful in production.

This library is as fork of PynamoDB to add async support.

Basic functionality is working, help to improve it is welcome.

** Known Issues **
  • Python type hints needs migration. MyPy testing implementation is pending and contributions in this area are welcome.

Installation

From GitHub:

$ pip install git+https://github.com/brunobelloni/AioPynamoDB#egg=aiopynamodb

From PyPI:

$ pip install as-aiopynamodb -U

Basic Usage

Create a model that describes your DynamoDB table.

from aiopynamodb.models import Model
from aiopynamodb.attributes import UnicodeAttribute

class UserModel(Model):
    """
    A DynamoDB User
    """
    class Meta:
        table_name = "dynamodb-user"
    email = UnicodeAttribute(null=True)
    first_name = UnicodeAttribute(range_key=True)
    last_name = UnicodeAttribute(hash_key=True)

PynamoDB allows you to create the table if needed (it must exist before you can use it!):

await UserModel.create_table(read_capacity_units=1, write_capacity_units=1)

Create a new user:

user = UserModel("John", "Denver")
user.email = "djohn@company.org"
await user.save()

Now, search your table for all users with a last name of ‘Denver’ and whose first name begins with ‘J’:

async for user in UserModel.query("Denver", UserModel.first_name.startswith("J")):
    print(user.first_name)

Examples of ways to query your table with filter conditions:

async for user in UserModel.query("Denver", UserModel.email=="djohn@company.org"):
    print(user.first_name)

Retrieve an existing user:

try:
    user = await UserModel.get("John", "Denver")
    print(user)
except UserModel.DoesNotExist:
    print("User does not exist")

Advanced Usage

Want to use indexes? No problem:

from aiopynamodb.models import Model
from aiopynamodb.indexes import GlobalSecondaryIndex, AllProjection
from aiopynamodb.attributes import NumberAttribute, UnicodeAttribute

class ViewIndex(GlobalSecondaryIndex):
    class Meta:
        read_capacity_units = 2
        write_capacity_units = 1
        projection = AllProjection()
    view = NumberAttribute(default=0, hash_key=True)

class TestModel(Model):
    class Meta:
        table_name = "TestModel"
    forum = UnicodeAttribute(hash_key=True)
    thread = UnicodeAttribute(range_key=True)
    view = NumberAttribute(default=0)
    view_index = ViewIndex()

Now query the index for all items with 0 views:

async for item in TestModel.view_index.query(0):
    print("Item queried from index: {0}".format(item))

It’s really that simple.

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

as_aiopynamodb-1.0.2.tar.gz (95.4 kB view details)

Uploaded Source

Built Distribution

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

as_aiopynamodb-1.0.2-py3-none-any.whl (62.1 kB view details)

Uploaded Python 3

File details

Details for the file as_aiopynamodb-1.0.2.tar.gz.

File metadata

  • Download URL: as_aiopynamodb-1.0.2.tar.gz
  • Upload date:
  • Size: 95.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for as_aiopynamodb-1.0.2.tar.gz
Algorithm Hash digest
SHA256 825f02f7e264a6f7618f7f92c4171eb511a7fdb7da74bfd6da818ba9f749619b
MD5 a9b9177468b201720eb115c14c3f993b
BLAKE2b-256 8006f3d760d1d3ed1235f556ad2a57f71e3ee31d12c43ee6cdf6245c2c11bb63

See more details on using hashes here.

Provenance

The following attestation bundles were made for as_aiopynamodb-1.0.2.tar.gz:

Publisher: release.yaml on AppSolves/AioPynamoDB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file as_aiopynamodb-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: as_aiopynamodb-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 62.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for as_aiopynamodb-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f9864e6031d12c001c649a7940c846fb885c0b9295083a920e6bef7941722e03
MD5 118f7cd59adbe0b41efcc7198c0242eb
BLAKE2b-256 409d7364e9c52ca7e19e4cf431a94246f5f5990243d36691bae475895782b87f

See more details on using hashes here.

Provenance

The following attestation bundles were made for as_aiopynamodb-1.0.2-py3-none-any.whl:

Publisher: release.yaml on AppSolves/AioPynamoDB

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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