跳转到主要内容

异步Peewee

项目描述

Build Status Gitential Active Contributors Gitential Coding Hours Gitential Efficiency

AioPeewee

基于peeweetorpeewee的异步接口

实现数据库适配器

  • [x] aiomysql

  • [ ] aiopg

  • [ ] sqlite

目前已有125个测试案例从peewee迁移,不是全部,但数量持续增加。

简单的原子操作(事务)也得到支持,但现在还没有经过充分测试。

安装

pip install aiopeewee

用法

from aiopeewee 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 aiopeewee 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])

序列化

将对象转换为字典需要使用model_to_dict的异步版本

from aiopeewee import model_to_dict

serialized = await model_to_dict(user)

项目详情


下载文件

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

源代码分发

aiopeewee-0.4.2.tar.gz (68.9 kB 查看散列值)

上传时间:

支持