DynamoDB单表库
dokklib-db的Python项目详细描述
Dokklib数据库
Dokklib DB是DynamoDB单表模式的Python库。在
特点
- 在Boto3之上的简单的python查询接口。不再有嵌套的dict字面值!在
- 主键和索引的类型安全(用于文档和数据完整性)。在
- 简单的错误处理。在
- 完整类型提示和单元测试覆盖率+集成测试。在
Documentation
安装
安装方式:
pip install "boto3>=1.10.34,<2" dokklib-db
需要Python3.6或更高版本。在
注意,Boto3没有被指定为Dokklib DB的安装要求,因此您必须单独安装它(如上面的示例命令中所示)。
这样做的原因是为了使Dokklib DB更易于在AWS Lambda中使用,其中Boto3是默认环境的一部分。
最早支持的Boto3版本是1.10.34
,它与python3awslambda环境中的Boto3包版本相同。在
示例用法
importdokklib_dbasdbclassUser(db.EntityName):"""User entity name. Key value: unique user name, eg. 'alice'. Example key: 'USER#alice'. """classGroup(db.EntityName):"""Group entity name. Key value: unique group name, eg. 'my-group'. Example key: 'GROUP#my-group'. """table=db.Table('SingleTable')# Construct entity keys.pk_alice=db.PartitionKey(User,'alice')pk_bob=db.PartitionKey(User,'bob')sk_group1=db.SortKey(Group,'group1')# Add users to group one.# Insert is a `PutItem` operation that fails if the item already exists.table.insert(pk_alice,sk_group1)table.insert(pk_bob,sk_group1)# Get all users in group one.pk_group=db.PartitionKey(Group,'group1')user_prefix=db.PrefixSortKey(User)group_members=table.query_prefix(pk_group,user_prefix,global_index=db.InversePrimaryIndex())print(group_members)# [{'PK': 'alice'}, {'PK': 'bob'}]# Move users from group one to group two atomically.sk_group2=db.SortKey(Group,'group2')table.transact_write_items([db.DeleteArg(pk_alice,sk_group1),db.DeleteArg(pk_bob,sk_group1),db.InsertArg(pk_alice,sk_group2),db.InsertArg(pk_bob,sk_group2)])
状态
这个库处于beta测试阶段,正在进行大量的开发,因为我正在构建一个依赖它的serverless project。 到目前为止,我只实现了我所需要的dynamodbapi的一部分,但我计划实现完全覆盖。 欢迎提供功能和拉取请求。(请在开始处理拉取请求之前打开问题,以避免浪费精力。)
- 项目
标签: