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.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 preloaded relations
TODO
from peewee_aio import getrel
# We preloaded roles here
async for user in User.select(User, Role).join(Role):
role = getrel(user, 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.3.2.tar.gz
(13.5 kB
view details)
Built Distribution
File details
Details for the file peewee_aio-1.3.2.tar.gz
.
File metadata
- Download URL: peewee_aio-1.3.2.tar.gz
- Upload date:
- Size: 13.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d171e33aec4d0ea175b9da48d10c5820098a764f81fca9bba32ec45a8f1e823 |
|
MD5 | c062e773f2eb2d1debd4b7866f29c427 |
|
BLAKE2b-256 | 0f13fbb38e9953e984050c27ff951cb06b403bf4eeee5bf176dd37b44e28e19a |
File details
Details for the file peewee_aio-1.3.2-py3-none-any.whl
.
File metadata
- Download URL: peewee_aio-1.3.2-py3-none-any.whl
- Upload date:
- Size: 13.9 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42736e6384feb9068189073857bf69bfbe4e0a0203a4b4c8a8a6291d1ea8d5bc |
|
MD5 | ce4efdaa478f5e583afa47ad4e234442 |
|
BLAKE2b-256 | ea84a9e7980ec0ebb8795da9d04faa2e859d1e4c1f6f6f4452dc016e74b06f60 |