Module for working with PostgreSQL via asyncpg


Keywords
aioworkers, asyncio, asyncpg, postgresql, python
License
Apache-2.0
Install
pip install aioworkers-pg==0.4.0a1

Documentation

aioworkers-pg

Coverage Code style: ruff Code style: black Code style: Mypy Documentation Status Python versions Hatch project

Asyncpg plugin for aioworkers.

Usage

Connection

Add this to aioworkers config.yaml:

db:
  cls: aioworkers_pg.base.Connector
  dsn: postgresql:///test

You can get access to postgres anywhere via context:

await context.db.execute("CREATE TABLE users(id serial PRIMARY KEY, name text)")
await context.db.execute(users.insert().values(name="Bob"))

Connection additional

db:
  cls: aioworkers_pg.base.Connector
  dsn: postgresql:///test
  pool:
    min_size: 1
    max_size: 100
  connection:  # optional
    init: mymodule.connection_init
    setup: mymodule.connection_setup
    class: mymodule.Connection

Storage

storage:
  cls: aioworkers_pg.storage.RoStorage
  dsn: postgresql:///test
  table: mytable  # optional instead custom sql
  key: id
  get: SELECT * FROM mytable WHERE id = :id  # optional custom sql
  format: dict  # or row

Development

Check code:

hatch run lint:all

Format code:

hatch run lint:fmt

Run postgres:

docker run --rm -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=test -d postgres

Run tests:

hatch run pytest

Run tests with coverage:

hatch run cov