Skip to main content

Async support for Peewee ORM

Project description

Peewee-AIO

Async support for Peewee ORM

Tests Status PYPI Version Python Versions

Features

Requirements

  • python >= 3.10

Installation

peewee-aio should be installed using pip:

pip install peewee-aio

You can install optional database drivers with:

pip install peewee-aio[aiosqlite]   # for SQLite (asyncio)
pip install peewee-aio[aiomysql]    # for MySQL (asyncio)
pip install peewee-aio[aiopg]       # for Postgresql (asyncio)
pip install peewee-aio[asyncpg]     # for Postgresql (asyncio)
pip install peewee-aio[trio_mysql]  # for MySQL (trio)
pip install peewee-aio[triopg]      # for PostgresQL (trio)

Quickstart

    import peewee
    from peewee_aio import Manager, AIOModel, fields

    manager = Manager('aiosqlite:///:memory:')

    @manager.register
    class Role(AIOModel):
        # Pay attention that we are using fields from Peewee-AIO for better typing support
        id = fields.AutoField()
        name = fields.CharField()

    @manager.register
    class User(AIOModel):

        # Pay attention that we are using fields from Peewee-AIO for better typing support
        id = fields.AutoField()
        name = fields.CharField()
        role = fields.ForeignKeyField(Role)

    async def handler():

        # Initialize the database's pool (optional)
        async with manager:

            # Acquire a connection
            async with manager.connection():

                # Create the tables in database
                await Role.create_table()
                await User.create_table()

                # Create a record
                role = await Role.create(name='user')
                assert role
                assert role.id  # role.id contains correct string type
                user = await User.create(name="Andrey", role=role)
                assert user
                assert user.id
                role = await user.role  # Load role from DB using the foreign key
                assert role  # role has a correct Role Type

                # Iterate through records
                async for user in User.select(User, Role).join(Role):
                    assert user  # user has a corrent User Type
                    assert user.id
                    role = await user.role  # No DB query here, because the fk is preloaded

                # Change records
                user.name = "Dmitry"
                await user.save()

                # Update records
                await User.update({"name": "Anonimous"}).where(User.id == user.id)

                # Delete records
                await User.delete().where(User.id == user.id)

                # Drop the tables in database
                await User.drop_table()
                await Role.drop_table()

    # Run the handler with your async library
    import asyncio

    asyncio.run(handler())

Usage

Supported schemas

  • aiomyql
  • aiomyql+pool
  • aiopg
  • aiopg+pool
  • asyncpg
  • asyncpg+pool
  • aioodbc
  • aioodbc+pool
  • aiosqlite
  • trio-mysql
  • triopg

Sync usage

The library still supports sync mode (use manager.allow_sync):

class Test(peewee.Model):
  data = peewee.CharField()

with manager.allow_sync():
  Test.create_table()
  Test.create(data='test')
  assert Test.select().count()
  Test.update(data='new-test').execute()

Get prefetched relations

TODO

# We prefetched roles here
async for user in User.select(User, Role).join(Role):
  role = user.fetch(User.role)  # get role from user relations cache

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/peewee-aio/issues

Contributing

Development of the project happens at: https://github.com/klen/peewee-aio

License

Licensed under a MIT License

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

peewee_aio-2.2.4.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

peewee_aio-2.2.4-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file peewee_aio-2.2.4.tar.gz.

File metadata

  • Download URL: peewee_aio-2.2.4.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for peewee_aio-2.2.4.tar.gz
Algorithm Hash digest
SHA256 b98d463848e20d910a9d532bff203c62385e5c3dbab17b1b9943d1c29fb477d9
MD5 45369bf731d99946871252183c070367
BLAKE2b-256 1cfd35204a5ebdb786c5819a2929d34a227ad63fe589fd22492131484353f935

See more details on using hashes here.

File details

Details for the file peewee_aio-2.2.4-py3-none-any.whl.

File metadata

  • Download URL: peewee_aio-2.2.4-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for peewee_aio-2.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7d55d69a8fb47ed0b88da11d9925c84bdc3e298ff23b166aedb5e48c39f1e48d
MD5 2ce07995251127f31007655afba2536a
BLAKE2b-256 42bb564fadc0a138da6c96a0730734b2ab1ceb45f63329944af90408cf9d73ed

See more details on using hashes here.

Supported by

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