Skip to main content
https://badge.fury.io/py/pydynamodb.svg https://github.com/passren/PyDynamoDB/actions/workflows/run-test.yaml/badge.svg https://pepy.tech/badge/pydynamodb/month https://img.shields.io/badge/code%20style-black-000000.svg

PyDynamoDB

PyDynamoDB is a Python DB API 2.0 (PEP 249) client for Amazon DynamoDB. SQLAlchemy dialect supported as well.

Objectives

PyDynamoDB implement the DB API 2.0 interfaces based on PartiQL supported by AWS DynamoDB. You have to create DDB tables before using pydynamodb, because PartiQL can only support SELECT, INSERT, UPDATE, DELETE operations on the tables. PyDynamodb provide parameters and result_set converter to make you easily manipulate PartiQL operations with Python built-in types. Transaction is also partially supported with DB standard operations, like begin() and commit(). This project is based on laughingman7743’s PyAthena.

Requirements

  • Python

    • CPython 3.7 3.8 3.9 3.10

Dependencies

  • Boto3 (Python SDK for AWS Services)

    • boto3 >= 1.21.0

    • botocore >= 1.24.7

  • Tenacity (Retry Utility for API calling)

    • tenacity >= 4.1.0

  • SQLAlchemy (The ORM Toolkit for Python, only required if using PyDynamoDB Dialect)

    • SQLAlchemy >= 1.0.0, < 2.0.0

Installation

pip install pydynamodb

Getting Started

Usage

Basic usage

from pydynamodb import connect

cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor()
cursor.execute('SELECT * FROM "ddb_table_name"')
print(cursor.fetchall())

Cursor iteration

from pydynamodb import connect

cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor()
cursor.execute('SELECT * FROM "ddb_table_name"')
rows = cursor.fetchall()
for row in rows:
    print(row)

Query with parameters

PyDynamoDB is able to serialize the parameters which passed to DDB and deserialize the response to Python built-in types.

from pydynamodb import connect
cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor()
cursor.execute("""INSERT INTO "ddb_table_name" VALUE {
                    'partition_key' = ?,
                    'sort_key' = ?,
                    'col_str' = ?,
                    'col_num' = ?,
                    'col_byte' = ?,
                    'col_ss' = ?,
                    'col_ns' = ?,
                    'col_bs' = ?,
                    'col_list' = ?,
                    'col_map' = ?,
                    'col_nested' = ?
                }""", ["pkey_value", "skey_value", "str", 100, b"ABC", # String, Number, Bytes
                        {"str", "str"}, {100, 100}, {b"A", b"B"}, # String/Numnber/Bytes Set
                        ["str", 100, b"ABC"],  # List
                        {"key1": "val", "key2": "val"}, # Map
                        ["str", 100, {"key1": "val"}] # Nested Structure
                    ])

cursor.execute('SELECT * FROM "ddb_table_name" WHERE partition_key = ?', ["key_value"])
print(cursor.fetchall())

Description of Result Set

DDB is a NoSQL database. That means except key schema, the data in each row may have flexible columns or types. PyDynamoDB cannot get a completed result set description before fetching all result data. So you have to use fetch* method to iterate the whole result set, then call cursor.description to get the full columns description.

from pydynamodb import connect

cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor()
cursor.execute('SELECT * FROM "ddb_table_name"')
print(cursor.fetchall())
print(cursor.description)

Dict Cursor and Result Set

Using DictCursor, you can get a dict result set with column name and value pair. This type of cursor has better performance and manipulate result data easily. But cursor.description will return empty with this way.

from pydynamodb import connect
from pydynamodb.cursor import DictCursor

cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor(cursor=DictCursor)
cursor.execute('SELECT * FROM "ddb_table_name"')
print(cursor.fetchall())

Transaction

Transaction is partially supported also. connection.rollback() is not implemented. Regarding information and restrictions of DDB transaction, please see the page: Performing transactions with PartiQL for DynamoDB

from pydynamodb import connect

conn = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name")
cursor = conn.cursor()

conn.begin()
cursor.execute("""INSERT INTO "ddb_table_name" VALUE {'key_partition': ?, 'key_sort': ?, 'col1': ?}""",
                ["pk1", "sk1", "test"])
cursor.execute("""INSERT INTO "ddb_table_name" VALUE {'key_partition': ?, 'key_sort': ?, 'col1': ?}""",
                ["pk2", "sk2", "test"])
conn.commit()

Limit Expression

DynamoDB doesn’t support LIMIT expression in PartiQL. This is inconvenient in many scenarios. PyDynamoDB is able to support writing LIMIT expression in PartiQL.

from pydynamodb import connect

cursor = connect(aws_access_key_id="aws_access_key_id",
                aws_secret_access_key="aws_secret_access_key"
                 region_name="region_name").cursor()
cursor.execute('SELECT * FROM "ddb_table_name" WHERE key_partition = ? LIMIT 10', ["pk1"])
print(cursor.fetchall())

SQLAlchemy

Install SQLAlchemy with pip install "SQLAlchemy>=1.0.0, <2.0.0". Supported SQLAlchemy is 1.0.0 or higher and less than 2.0.0.

The connection string has the following format:

dynamodb://{aws_access_key_id}:{aws_secret_access_key}@dynamodb.{region_name}.amazonaws.com:443?verify=false&...
from pydynamodb import sqlalchemy_dynamodb
from sqlalchemy.engine import create_engine
from sqlalchemy.sql.schema import Column, MetaData, Table

conn_str = (
        "dynamodb://{aws_access_key_id}:{aws_secret_access_key}@dynamodb.{region_name}.amazonaws.com:443"
        + "?verify=false"
    )
conn_str = conn_str.format(
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key,
        region_name=region_name,
    )
engine = create_engine(conn_str)
with engine.connect() as connection:
    many_rows = Table("many_rows", MetaData(),
                    Column('key_partition', String, nullable=False),
                    Column('key_sort', Integer),
                    Column('col_str', String),
                    Column('col_num', Numeric)
            )
    rows = conn.execute(many_rows.select()).fetchall()
    print(rows)

Test with local DynamoDB

Install Local DDB, please see: Deploying DynamoDB locally on your computer. If you want to run tests with local DDB, please make sure environment variables are set properly.

USE_LOCAL_DDB=true
LOCAL_DDB_ENDPOINT_URL=http://localhost:8000

License

PyDynamoDB is distributed under the MIT license.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyDynamoDB-0.3.4.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

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

PyDynamoDB-0.3.4-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file PyDynamoDB-0.3.4.tar.gz.

File metadata

  • Download URL: PyDynamoDB-0.3.4.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for PyDynamoDB-0.3.4.tar.gz
Algorithm Hash digest
SHA256 eae13c97646d6dc23df8a3e3e8f3b3cee2ec6c60ab2273df2e68b00ab2e4b82f
MD5 1c21e2dcddfff298eb66cc01295b5f79
BLAKE2b-256 ee275d4c3fed52ea93e38b4bcfcdb09cbc30c0562ad24b5a7dc2e7107dcb2a0b

See more details on using hashes here.

File details

Details for the file PyDynamoDB-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: PyDynamoDB-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 20.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for PyDynamoDB-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4f63b2cdd0e99579eaa1d20e1d0a0e0b5abd8a90b9463fae916e2da8ea80973a
MD5 bf2d4f2ac0d573c144e4c190a01cf741
BLAKE2b-256 e677423c168ae7f9a6fd2e0f9f634576f25e53af7c7f87abf8cc787c2562403e

See more details on using hashes here.

Release history Release notifications | RSS feed

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.6

2 files

0.7.5

2 files

0.7.4

2 files

0.7.3

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.3

2 files

0.6.2

2 files

0.6.1

2 files

0.6.0

2 files

0.5.8

2 files

0.5.7

2 files

0.5.5

2 files

0.5.4

2 files

0.5.3

2 files

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.8

2 files

0.4.7

2 files

0.4.6

2 files

0.4.5

2 files

0.4.4

2 files

0.4.3

2 files

0.4.2

2 files

0.4.1

2 files

This release

0.3.4 This release

2 files

0.3.3

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page