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.8

Installation

peewee-aio should be installed using pip:

$ pip install peewee-aio

You can install optional database drivers with:

$ pip install peewee-aio[aiosqlite]
$ pip install peewee-aio[aiomysql]
$ pip install peewee-aio[aiopg]
$ pip install peewee-aio[asyncpg]
$ pip install peewee-aio[trio_mysql]
$ pip install peewee-aio[triopg]

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

TODO

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-1.5.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

peewee_aio-1.5.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: peewee_aio-1.5.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for peewee_aio-1.5.0.tar.gz
Algorithm Hash digest
SHA256 dcdce1decfc926fd01eba8241b865ccbec4e9889d795ea03e9f6a4cb22fc3b52
MD5 d6f1839b460ed17315c38bff81beefe5
BLAKE2b-256 9ce9ec2e98ba50a71002f023e2d73b88242ea945f6c8b283f25225749b9abb28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: peewee_aio-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for peewee_aio-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b90034b2b969e99f0795a8268b2ea2d935b4bc0ee25a79c1113f6706facf05dc
MD5 30de5252dc07c18e257c240866ce88c7
BLAKE2b-256 a37ce99d0373330ff2c5df426c0f313b7b0f70f6c1ecbd09b0aa7f51fe32bc08

See more details on using hashes here.

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