跳转到主要内容

Peewee ORM的异步支持

项目描述

Peewee-AIO

Peewee ORM的异步支持

Tests Status PYPI Version Python Versions

功能

要求

  • python >= 3.8

安装

peewee-aio 应使用pip安装

$ pip install peewee-aio

您可以使用以下命令安装可选的数据库驱动程序

$ 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)

快速入门

    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())

使用方法

待办事项

同步使用

该库仍然支持同步模式(使用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()

获取预取关系

待办事项

# 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

错误跟踪器

如果您有任何建议、错误报告或烦恼,请通过https://github.com/klen/peewee-aio/issues问题跟踪器报告

贡献

项目开发发生在:https://github.com/klen/peewee-aio

许可证

MIT许可证下许可

项目详情


发布历史 发布通知 | RSS订阅

下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

peewee_aio-1.7.4.tar.gz (14.7 kB 查看哈希值)

上传时间 源代码

构建分发

peewee_aio-1.7.4-py3-none-any.whl (15.2 kB 查看哈希值)

上传时间 Python 3

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面