Skip to main content

An Async Pythonic Interface to DynamoDB

Project description

Async fork of PynamoDB powered by aiobotocore. Requires Python 3.10+.

Installation

From PyPI:

$ pip install aiopynamodb

From GitHub:

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

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)

AioPynamoDB 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

aiopynamodb-1.0.1.tar.gz (94.7 kB view details)

Uploaded Source

Built Distribution

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

aiopynamodb-1.0.1-py3-none-any.whl (60.7 kB view details)

Uploaded Python 3

File details

Details for the file aiopynamodb-1.0.1.tar.gz.

File metadata

  • Download URL: aiopynamodb-1.0.1.tar.gz
  • Upload date:
  • Size: 94.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for aiopynamodb-1.0.1.tar.gz
Algorithm Hash digest
SHA256 85b4c85205caa1c49bd90e366d01f76b91b5cb38af131e1db89bc20dc9c34f63
MD5 c83abef5621c5cb08252c2a64b04d8b7
BLAKE2b-256 31ea2a8b95a5093b780cfc68086664a0af115e4221824dbbb80a7eeba4c2d204

See more details on using hashes here.

File details

Details for the file aiopynamodb-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: aiopynamodb-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 60.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for aiopynamodb-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 150905f2508661f5c43a6cf6a9bae1ce6b1d927a812cdc276e1098ad5bc17416
MD5 a5b4e113b427115829feb27e9bafb9a3
BLAKE2b-256 ab47328db67c33a7d63e5d1463c4d0480c1481751c81262ae7133e2da5ef05a1

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