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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiopynamodb-1.0.0.tar.gz.
File metadata
- Download URL: aiopynamodb-1.0.0.tar.gz
- Upload date:
- Size: 94.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac98be0c19b7c8a1f1a7d527603ec67ed8a6eb95dbed8ba298a5fa1c1c71c8ea
|
|
| MD5 |
09b450fb3755f1571d9c6255666668c1
|
|
| BLAKE2b-256 |
35c9fc35bd91caa8e30c18014a45c557645b813768238d35ffe92a441fce817f
|
File details
Details for the file aiopynamodb-1.0.0-py3-none-any.whl.
File metadata
- Download URL: aiopynamodb-1.0.0-py3-none-any.whl
- Upload date:
- Size: 60.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61cedf1aa74d79c47d6c97cd9a899101f86fd255675ca0e606c3da26360b40d9
|
|
| MD5 |
66b413d2afdf7e7a3d4eb2acf194bc97
|
|
| BLAKE2b-256 |
708d415473c7e25c90d3107b423a144a246ab85c8cfa81a8131445862a335f9b
|