跳转到主要内容

DynamORM 是一个针对亚马逊DynamoDB服务的Python对象与关系映射库。

项目描述

DynamORM

https://img.shields.io/travis/NerdWalletOSS/dynamorm.svg https://img.shields.io/codecov/c/github/NerdWalletOSS/dynamorm.svg Latest PyPI version Supported Python Versions

此包是一个进行中的作品 - 欢迎反馈/建议等!

Python + DynamoDB ♡

DynamORM (发音为 Dynamo-R-M) 是一个针对亚马逊 DynamoDB 服务的Python对象与关系映射库。

项目有两个目标

  1. 抽象出与底层DynamoDB库的交互。Python对DynamoDB服务的访问已经迅速发展,从,再到。通过提供一个用户熟悉的、与其他Python ORM (SQLAlchemy, Django, Peewee等) 用户类似的统一接口,我们可以始终提供最佳实践查询并利用新功能,而无需对任何应用程序逻辑进行重构。

  2. 将模式验证和序列化委托给更专注的库。构建“ORM”语义是“简单”的,而数据验证和序列化则不是。我们支持MarshmallowSchematics来构建您的对象模式。您可以利用这些库的完整功能,因为它们在代码中以透明的方式暴露。

支持的Schema验证库

示例

import datetime

from dynamorm import DynaModel, GlobalIndex, ProjectAll

# In this example we'll use Marshmallow, but you can also use Schematics too!
# You can see that you have to import the schema library yourself, it is not abstracted at all
from marshmallow import fields

# Our objects are defined as DynaModel classes
class Book(DynaModel):
    # Define our DynamoDB properties
    class Table:
        name = 'prod-books'
        hash_key = 'isbn'
        read = 25
        write = 5

    class ByAuthor(GlobalIndex):
        name = 'by-author'
        hash_key = 'author'
        read = 25
        write = 5
        projection = ProjectAll()

    # Define our data schema, each property here will become a property on instances of the Book class
    class Schema:
        isbn = fields.String(validate=validate_isbn)
        title = fields.String()
        author = fields.String()
        publisher = fields.String()

        # NOTE: Marshmallow uses the `missing` keyword during deserialization, which occurs when we save
        # an object to Dynamo and the attr has no value, versus the `default` keyword, which is used when
        # we load a document from Dynamo and the value doesn't exist or is null.
        year = fields.Number(missing=lambda: datetime.datetime.utcnow().year)


# Store new documents directly from dictionaries
Book.put({
    "isbn": "12345678910",
    "title": "Foo",
    "author": "Mr. Bar",
    "publisher": "Publishorama"
})

# Work with the classes as objects.  You can pass attributes from the schema to the constructor
foo = Book(isbn="12345678910", title="Foo", author="Mr. Bar",
           publisher="Publishorama")
foo.save()

# Or assign attributes
foo = Book()
foo.isbn = "12345678910"
foo.title = "Foo"
foo.author = "Mr. Bar"
foo.publisher = "Publishorama"

# In all cases they go through Schema validation, calls to .put or .save can result in ValidationError
foo.save()

# You can then fetch, query and scan your tables.
# Get on the hash key, and/or range key
book = Book.get(isbn="12345678910")

# Update items, with conditions
# Here our condition ensures we don't have a race condition where someone else updates the title first
book.update(title='Corrected Foo', conditions=(title=book.title,))

# Query based on the keys
Book.query(isbn__begins_with="12345")

# Scan based on attributes
Book.scan(author="Mr. Bar")
Book.scan(author__ne="Mr. Bar")

# Query based on indexes
Book.ByAuthor.query(author="Mr. Bar")

文档

完整文档是从每个构建的源代码构建的,可以在以下网址找到:

https://nerdwalletoss.github.io/dynamorm/

“tests/”中也包含了如何实际使用此库的最完整文档,因此我们鼓励您阅读这些文档,以便真正熟悉一些更高级的概念和用例。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源代码分发

dynamorm-0.11.0.tar.gz (28.5 kB 查看哈希)

上传时间 源代码

构建分发

dynamorm-0.11.0-py2.py3-none-any.whl (30.9 kB 查看哈希)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面