Skip to main content

Build Status

AioPeewee

Asyncio interface for peewee modeled after torpeewee

Limitations (hopefully resolved in a couple of weeks):

  • currently just aiomysql supported

  • untested transactions

More thorough testing is on the way.

Any feedback would be greatly appreciated!

Install

pip install aiopeewee

Usage

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

ManyToMany

Note that AioManyToManyField must be used instead of 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

Currently the only limitation I’m aware of immidiate setting of instance relation must be replaced with a method call:

# original, which is not supported
charlie.notes = [n2, n3]

# use instead
await charlie.notes.set([n2, n3])

Serializing

Converting to dict requires the asyncified version of model_to_dict

from aiopeewee import model_to_dict

serialized = await model_to_dict(user)

Release files for aiopeewee 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aiopeewee 0.3.4
File Size Uploaded
aiopeewee-0.3.4.tar.gz 12.6 kB Details

Release files / aiopeewee-0.3.4.tar.gz

Download URL aiopeewee-0.3.4.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
45c238e23ffef0cb5c5c4b43d2fb9145f52d9121b356eff0a73b362f089197ae
BLAKE2b-256 checksum
How to use checksums
c54be5dd09577c23a676a06887e461d835cc355eeb3db0ac4d9642ccda5b554d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.6

1 release file

0.3.5

1 release file

This release

0.3.4 This release

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page