Deprecated API

Note: all query methods are coroutines.

Select, update, delete

async peewee_async.execute(query)
async peewee_async.prefetch(sq, *subqueries, prefetch_type)

Asynchronous version of the prefetch() from peewee.

Transactions

Transactions required Python 3.5+ to work, because their syntax is based on async context managers.

Important note transactions rely on data isolation on asyncio per-task basis. That means, all queries for single transaction should be performed within same task.

peewee_async.atomic(db)

Asynchronous context manager (async with), similar to peewee.atomic().

peewee_async.savepoint(db, sid=None)
peewee_async.transaction(db)

Asynchronous context manager (async with), similar to peewee.transaction(). Will start new asyncio task for transaction if not started already.

Aggregation

async peewee_async.count(query, clear_limit=False)

Perform COUNT aggregated query asynchronously.

Returns:

number of objects in select() query

async peewee_async.scalar(query, as_tuple=False)

Databases

class peewee_async.PostgresqlDatabase(database, thread_safe=True, autorollback=False, field_types=None, operations=None, autocommit=None, autoconnect=True, **kwargs)

PosgreSQL database driver providing single drop-in sync connection and single async connection interface.

Example:

database = PostgresqlDatabase('test')

See also: http://peewee.readthedocs.io/en/latest/peewee/api.html#PostgresqlDatabase

atomic_async() Any

Similar to peewee Database.atomic() method, but returns asynchronous context manager.

savepoint_async(sid=None) Any

Similar to peewee Database.savepoint() method, but returns asynchronous context manager.

transaction_async() Any

Similar to peewee Database.transaction() method, but returns asynchronous context manager.

class peewee_asyncext.PostgresqlExtDatabase(*args, **kwargs)

PosgreSQL database extended driver providing single drop-in sync connection and single async connection interface.

JSON fields support is always enabled, HStore supports is enabled by default, but can be disabled with register_hstore=False argument.

Example:

database = PostgresqlExtDatabase('test', register_hstore=False)

See also: https://peewee.readthedocs.io/en/latest/peewee/playhouse.html#PostgresqlExtDatabase

atomic_async() Any

Similar to peewee Database.atomic() method, but returns asynchronous context manager.

savepoint_async(sid=None) Any

Similar to peewee Database.savepoint() method, but returns asynchronous context manager.

transaction_async() Any

Similar to peewee Database.transaction() method, but returns asynchronous context manager.

class peewee_async.MySQLDatabase(database, thread_safe=True, autorollback=False, field_types=None, operations=None, autocommit=None, autoconnect=True, **kwargs)

MySQL database driver providing single drop-in sync connection and single async connection interface.

Example:

database = MySQLDatabase('test')

See also: http://peewee.readthedocs.io/en/latest/peewee/api.html#MySQLDatabase

atomic_async() Any

Similar to peewee Database.atomic() method, but returns asynchronous context manager.

savepoint_async(sid=None) Any

Similar to peewee Database.savepoint() method, but returns asynchronous context manager.

transaction_async() Any

Similar to peewee Database.transaction() method, but returns asynchronous context manager.

Manager

class peewee_async.Manager(database=None)

Async peewee model’s manager.

Parameters:

database – (optional) async database driver

Example:

class User(peewee.Model):
    username = peewee.CharField(max_length=40, unique=True)

objects = Manager(PostgresqlDatabase('test'))

async def my_async_func():
    user0 = await objects.create(User, username='test')
    user1 = await objects.get(User, id=user0.id)
    user2 = await objects.get(User, username='test')
    # All should be the same
    print(user1.id, user2.id, user3.id)

If you don’t pass database to constructor, you should define database as a class member like that:

database = PostgresqlDatabase('test')

class MyManager(Manager):
    database = database

objects = MyManager()
Manager.database = None

Async database driver for manager. Must be provided in constructor or as a class member.

Manager.allow_sync()

Allow sync queries within context. Close the sync database connection on exit if connected.

Example:

with objects.allow_sync():
    PageBlock.create_table(True)
async Manager.get(source_, *args, **kwargs)

Get the model instance.

Parameters:

source – model or base query for lookup

Example:

async def my_async_func():
    obj1 = await objects.get(MyModel, id=1)
    obj2 = await objects.get(MyModel, MyModel.id==1)
    obj3 = await objects.get(MyModel.select().where(MyModel.id==1))

All will return MyModel instance with id = 1

async Manager.create(model_, **data)

Create a new object saved to database.

async Manager.update(obj, only=None)

Update the object in the database. Optionally, update only the specified fields. For creating a new object use create()

Parameters:

only – (optional) the list/tuple of fields or field names to update

async Manager.delete(obj, recursive=False, delete_nullable=False)

Delete object from database.

async Manager.get_or_create(model_, defaults=None, **kwargs)

Try to get an object or create it with the specified defaults.

Return 2-tuple containing the model instance and a boolean indicating whether the instance was created.

async Manager.create_or_get(model_, **kwargs)

Try to create new object with specified data. If object already exists, then try to get it by unique fields.

async Manager.execute(query)

Execute query asyncronously.

async Manager.prefetch(query, *subqueries, prefetch_type=2)

Asynchronous version of the prefetch() from peewee.

Returns:

Query that has already cached data for subqueries

async Manager.count(query, clear_limit=False)

Perform COUNT aggregated query asynchronously.

Returns:

number of objects in select() query

async Manager.scalar(query, as_tuple=False)

Get single value from select() query, i.e. for aggregation.

Returns:

result is the same as after sync query.scalar() call

async Manager.connect()

Open database async connection if not connected.

async Manager.close()

Close database async connection if connected.

Manager.atomic()

Similar to peewee.Database.atomic() method, but returns asynchronous context manager.

Example:

async with objects.atomic():
    await objects.create(
        PageBlock, key='intro',
        text="There are more things in heaven and earth, "
             "Horatio, than are dreamt of in your philosophy.")
    await objects.create(
        PageBlock, key='signature', text="William Shakespeare")
Manager.transaction()

Similar to peewee.Database.transaction() method, but returns asynchronous context manager.

Manager.savepoint(sid=None)

Similar to peewee.Database.savepoint() method, but returns asynchronous context manager.