一个简单的 orm 支持 asyncio,aiopeewee 的分支
项目描述
版本:0.1.6
状态:生产
作者:hsz
以torpeewee为模型的peewee的Asyncio接口
特征
支持mysql和postgresql
使用数据库 URL 的数据库工厂
使用 peewee 的字段
多对多字段支持
快捷方式支持
csv 转储/加载支持
可以使用 playhose.postgres_ext.JSONField
安装
python -m pip install aioorm
示例:GRUD
from aioorm import AioModel, AioMySQLDatabase
from peewee import CharField, TextField, DateTimeField
from peewee import ForeignKeyField, PrimaryKeyField
db = AioMySQLDatabase('test', host='127.0.0.1', port=3306,
user='root', password='')
class User(AioModel):
username = CharField()
class Meta:
database = db
class Blog(AioModel):
user = ForeignKeyField(User)
title = CharField(max_length=25)
content = TextField(default='')
pub_date = DateTimeField(null=True)
pk = PrimaryKeyField()
class Meta:
database = db
# create connection pool
await db.connect(loop)
# count
await User.select().count()
# async iteration on select query
async for user in User.select():
print(user)
# fetch all records as a list from a query in one pass
users = await User.select()
# insert
user = await User.create(username='kszucs')
# modify
user.username = 'krisztian'
await user.save()
# async iteration on blog set
[b.title async for b in user.blog_set.order_by(Blog.title)]
# close connection pool
await db.close()
# see more in the tests
示例:多对多
请注意,必须使用AioManyToManyField而不是ManyToMany。
from aioorm import AioManyToManyField
class User(AioModel):
username = CharField(unique=True)
class Meta:
database = db
class Note(AioModel):
text = TextField()
users = AioManyToManyField(User)
class Meta:
database = db
NoteUserThrough = Note.users.get_through_model()
async for user in note.users:
# do something with the users
目前,我知道实例关系的即时设置的唯一限制必须用方法调用替换:
# original, which is not supported
charlie.notes = [n2, n3]
# use instead
await charlie.notes.set([n2, n3])
序列化
转换为 dict 需要model_to_dict的异步版本
from aioorm import model_to_dict
serialized = await model_to_dict(user)
转储到 csv
表可以转储到 csv 文件。
from aioorm.utils import aiodump_csv
query = User.select().order_by(User_csv.id)
await aiodump_csv(query,str(filepath))
文档
去做
异步数据集支持
更多测试
限制
未经测试的交易
只支持mysql和postgresql
错误修复
修复了get和get_or_create的错误
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
aioorm-0.1.6.tar.gz
(31.9 kB
查看哈希)
内置分布
aioorm-0.1.6-py3-none-any.whl
(27.4 kB
查看哈希)