Skip to main content

Async Peewee

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiopeewee-0.4.0.tar.gz (13.3 kB view details)

Uploaded Source

File details

Details for the file aiopeewee-0.4.0.tar.gz.

File metadata

  • Download URL: aiopeewee-0.4.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiopeewee-0.4.0.tar.gz
Algorithm Hash digest
SHA256 934fe4692d6a885b7318ef0e1764d3ee523800d135f10d119c7e7c7ea6faa24a
MD5 7a11a86ae15f1288b11df92ad24cda07
BLAKE2b-256 0e9bdd347eb588d61e5e71dc116b833e4f0003f3f4a308629e6e108d729c6be8

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page