Async support for Peewee ORM
Project description
Peewee-AIO
Async support for Peewee ORM
Features
- Make Peewee ORM to work async
- Supports PostgresQL, MySQL, SQLite
- Supports asyncio and trio
- Contains types as well
- Drivers supported:
Requirements
- python >= 3.9
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.0.0.tar.gz
(14.8 kB
view details)
Built Distribution
File details
Details for the file peewee_aio-2.0.0.tar.gz
.
File metadata
- Download URL: peewee_aio-2.0.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 408b8d8ee7dc02e7eeafb4f3f0a35dd8b2819bb59ad14c76697ce99e8d2b7832 |
|
MD5 | ec7eada76a6d2ffaf3c1fb2e22b2879f |
|
BLAKE2b-256 | f0958ec32ea9b6b4799d2273f1f9de0d6459b97677f5f99b2cf15d7896bf4822 |
File details
Details for the file peewee_aio-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: peewee_aio-2.0.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e58ec7b414a7cecfe00e3059784c3ed0d90575e0dee630e69bc8644075878f68 |
|
MD5 | a94022d229ceb8fa33799baa8392a95e |
|
BLAKE2b-256 | fa5553e8862372e5f67c34d264df11104fdb563808b2ece69a441e00fe48f89b |