Skip to main content

openframe-adapters-db-postgres

PostgreSQL database adapter for the OpenFrame Microservice Suite.

Part of the openframe-adapters monorepo. Implements BaseRepository[T] and HealthCheck from openframe-core using asyncpg.


Installation

pip install openframe-adapters-db-postgres

Required env var:

DATABASE_URL=postgresql://user:password@host:5432/dbname

Quick start

Raw dict mode

from openframe.adapters.db.postgres import PostgresSettings, PostgresRepository

settings = PostgresSettings()  # reads DATABASE_URL from env
repo = PostgresRepository(settings, table="items", id_column="id")

item = await repo.get("abc-123")          # dict | None
items, total = await repo.list(10, 0)     # ([dict, ...], int)
created = await repo.create({"name": "x"})
updated = await repo.update({"id": "abc-123", "name": "y"})
deleted = await repo.delete("abc-123")    # bool

Typed domain mode

from dataclasses import dataclass
from openframe.adapters.db.postgres import PostgresSettings, PostgresRepository

@dataclass
class Item:
    id: str
    name: str

class ItemRepository(PostgresRepository[Item]):
    _table = "items"
    _id_column = "id"

    def _row_to_entity(self, row) -> Item:
        return Item(**dict(row))

    def _entity_to_row(self, entity: Item) -> dict:
        return {"id": entity.id, "name": entity.name}

settings = PostgresSettings()
repo = ItemRepository(settings)
item: Item | None = await repo.get("abc-123")

Configuration

All settings are read from environment variables.

Env var Type Default Description
DATABASE_URL str required Full asyncpg DSN
POOL_SIZE int 10 Pool min/max size
POOL_MAX_INACTIVE_CONN_LIFETIME float 300.0 Idle connection TTL (s)
POOL_COMMAND_TIMEOUT float 60.0 Per-statement timeout (s)
POOL_MAX_QUERIES int 50000 Queries per connection before recycle
CONNECTION_TIMEOUT float 30.0 Pool creation timeout (s)
OPERATION_TIMEOUT float 10.0 Per-operation timeout (s)
MAX_RETRIES int 3 Max retry attempts

Health checks

PostgresRepository implements the HealthCheck protocol from openframe-core.

alive = await repo.ping()       # SELECT 1 — fast liveness check
ready = await repo.is_ready()   # pg_tables query — full readiness check

Both methods return False on any failure and never raise.


Exception hierarchy

All exceptions are AdapterError subclasses from openframe.core.exceptions. Raw asyncpg exceptions never escape the adapter.

Situation Exception
Cannot connect to Postgres AdapterConnectionError
Invalid DATABASE_URL catalog AdapterConfigurationError
Query failed (constraint, syntax, etc.) AdapterQueryError
Entity not found AdapterNotFoundError
Operation exceeded timeout AdapterTimeoutError

Development

# from the package directory
pip install -e ".[dev]"
pytest tests/ -v

Protocol conformance

from openframe.core.ports import BaseRepository
from openframe.core.health import HealthCheck

repo = PostgresRepository(settings, table="items", id_column="id")
assert isinstance(repo, BaseRepository)   # True — structural check
assert isinstance(repo, HealthCheck)      # True — structural check

No inheritance from either Protocol is required or used.


License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openframe_adapters_db_postgres-1.2.0.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openframe_adapters_db_postgres-1.2.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file openframe_adapters_db_postgres-1.2.0.tar.gz.

File metadata

File hashes

Hashes for openframe_adapters_db_postgres-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1379d1071d97edd05ecc9f7a8638e1a993e24a9162e9471ab2f24e017e829379
MD5 57c37edd72636ceaccb9cb3e0a679e6d
BLAKE2b-256 a0b27b46197a9863b4cabcc748b8cdafa56097a938b8446afc0a06391f713a85

See more details on using hashes here.

File details

Details for the file openframe_adapters_db_postgres-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openframe_adapters_db_postgres-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1600938af009f6fc832c79826c26b0f5226de55fe0c5c1c15cd8b36964359af
MD5 80d0193f10f393316079c9a9ef070e0a
BLAKE2b-256 e89d2a2a3ecc3b654151be4d69932f3287ee85c8432050ae4dc58950141e7b8f

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.3.0

2 files

This release

1.2.0 This release

2 files

1.1.0

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page