peewee-async

peewee-async is a library providing asynchronous interface powered by asyncio for peewee ORM.

  • Works on Python 3.8+

  • Has support for PostgreSQL via aiopg

  • Has support for MySQL via aiomysql

  • Asynchronous analogues of peewee sync methods with prefix aio_

  • Drop-in replacement for sync code, sync will remain sync

  • Basic operations are supported

  • Transactions support is present

The source code is hosted on GitHub.

Quickstart

import asyncio
import peewee
import peewee_async

# Nothing special, just define model and database:

database = peewee_async.PooledPostgresqlExtDatabase('test')


class TestModel(peewee_async.AioModel):
    text = peewee.CharField()

    class Meta:
        database = database


# Look, sync code is working!

TestModel.create_table(True)
TestModel.create(text="Yo, I can do it sync!")
database.close()

# No need for sync anymore!

database.set_allow_sync(False)


async def handler():
    await TestModel.aio_create(text="Not bad. Watch this, I'm async!")
    all_objects = await TestModel.select().aio_execute()
    for obj in all_objects:
        print(obj.text)


loop = asyncio.get_event_loop()
loop.run_until_complete(handler())
loop.close()

# Clean up, can do it sync again:
with database.allow_sync():
    TestModel.drop_table(True)

Install

Install latest version from PyPI.

For PostgreSQL:

pip install peewee-async[postgresql]

For MySQL:

pip install peewee-async[mysql]

Install from sources

git clone https://github.com/05bit/peewee-async.git
cd peewee-async
pip install .

Running tests

Prepare environment for tests:

  • Clone source code from GitHub as shown above

  • Run docker environment with PostgreSQL database for testing

Then run tests:

pytest -s -v

Report bugs and discuss

You are welcome to add discussion topics or bug reports to tracker on GitHub!

Contents

Indices and tables