猎鹰
falcano的Python项目详细描述
法尔卡诺
Amazon DynamoDB的Python接口,支持基于PynamoDB的Python 3和single-table design。在
安装
pip install falcano
基本用途
基本用法与PynamoDB
几乎相同。Meta
必须从Model.Meta
继承,并且必须为每个模型定义{
创建一个描述表中模型的模型。在
^{pr2}$如果需要,请创建表:
User.create_table(billing_mode='PAY_PER_REQUEST')
创建新用户:
user=User('John','Denver')user.email='djohn@company.org'user.save()
现在,在您的表中搜索姓氏为“Denver”、名字以“J”开头的所有用户:
foruserinUser.query('Denver',User.first_name.startswith('J')):print(user.first_name)
使用筛选条件查询表的方法示例:
foruserinUser.query('Denver',User.email.eq('djohn@company.org')):print(user.first_name)foruserinUser.query('Denver',User.email=='djohn@company.org'):print(user.first_name)
检索现有用户:
try:user=User.get('John','Denver')print(user)exceptUser.DoesNotExist:print('User does not exist')
高级用法
索引?没问题:
fromfalcano.modelimportModelfromfalcano.indexesimportGlobalSecondaryIndex,AllProjectionfromfalcano.attributesimportNumberAttribute,UnicodeAttributeclassViewIndex(GlobalSecondaryIndex):classMeta:billing_mode='PAY_PER_REQUEST'projection=AllProjection()view=NumberAttribute(default=0,hash_key=True)classTestModel(Model):classMeta(Model.Meta):table_name='TestModel'forum=UnicodeAttribute(hash_key=True)thread=UnicodeAttribute(range_key=True)view=NumberAttribute(default=0)Type=UnicodeAttribute(default='test')view_index=ViewIndex()
现在查询索引中包含0个视图的所有项:
foriteminTestModel.view_index.query(0):print(f'Item queried from index: {item}')
很简单!在
想在本地使用DynamoDB吗?添加host
name属性并指定本地服务器。在
fromfalcano.modelsimportModelfromfalcano.attributesimportUnicodeAttributeclassUser(Model):''' A DynamoDB User '''classMeta(Model.Meta):table_name='dynamodb-user'host='http://localhost:8000'email=UnicodeAttribute(null=True)first_name=UnicodeAttribute(range_key=True)last_name=UnicodeAttribute(hash_key=True)Type=UnicodeAttribute(default='user')
单表设计用途
想要使用索引遵循单表设计并重命名Type
属性吗?没问题:
fromfalcano.modelimportModelfromfalcano.indexesimportGlobalSecondaryIndex,AllProjectionfromfalcano.attributesimportNumberAttribute,UnicodeAttributeclassTypeIndex(GlobalSecondaryIndex):classMeta:index_name='Type'billing_mode='PAY_PER_REQUEST'projection=AllProjection()Kind=UnicodeAttribute(default=0,hash_key=True)classBaseModel(Model):classMeta(Model.Meta):table_name='single_table'# use the Kind attribute in place of Type for deserializationmodel_type='Kind'PK=UnicodeAttribute(hash_key=True)SK=UnicodeAttribute(range_key=True)TypeIndex=TypeIndex()classUser(BaseModel):email=UnicodeAttribute(null=True)Kind=UnicodeAttribute(default='user')classTeam(BaseModel):owner=UnicodeAttribute(null=True)Kind=UnicodeAttribute(default='team')
特点
- Python>;=3.8支持
Table
boto3资源的利用- DynamoDB API
conditions
对象 - 自动键入
- DynamoDB API
- 具有查询和扫描过滤器的类似ORM的接口
- 与发电机本地兼容
- 支持Unicode、Binary、JSON、Number、Set和UTC Datetime属性
- 支持全局和局部二级索引
- 批量操作的自动分页(?)在
- 复杂查询
- 每个表有多个模型并反序列化为对象
尚未实现的功能
- 提供迭代器,用于处理自动分页的查询、扫描
- 用于处理查询和扫描操作的迭代器
- 支持整个DynamoDB API
- 全表备份/恢复
- 具有自动分页功能的批处理操作
- 项目
标签: