Skip to main content

omnixys-database

Async SQLAlchemy toolkit for Omnixys services: session management, declarative base models, pagination, and shared column types, built on SQLAlchemy 2.x with asyncpg.

Installation

pip install omnixys-database

Features

  • DatabaseSessionManager — async engine + session factory with lifecycle management: session_scope() (commit-on-success, rollback+re-raise on error), create_session(), close(), and async with support.
  • Base & naming convention — a declarative base whose metadata applies a consistent naming convention (pk_, fk_, uq_, ix_, ck_) to all generated constraints.
  • Uuid7 — PostgreSQL UUID column type that generates a v7 UUID for new rows when no value is provided.
  • UtcDateTime — timezone-aware DateTime column type.
  • Page — frozen pagination value object with computed total_pages/has_next/has_previous.

Quick start

import asyncio

from database import DatabaseSessionManager

manager = DatabaseSessionManager(
    "postgresql+asyncpg://omnixys:omnixys@localhost:5432/omnixys",
    echo=False,
    pool_size=10,
    max_overflow=20,
    use_pool=True,
)

async def main() -> None:
    async with manager.session_scope() as session:
        # the transaction is committed when the block exits normally
        # and rolled back when an exception propagates
        ...

    async with manager:
        ...  # manager.close() runs on exit

asyncio.run(main())

session_scope closes the session in finally in all cases. Pass use_pool=False to use a NullPool (e.g. for short-lived workers) instead of the default AsyncAdaptedQueuePool.

Base models

import uuid
from datetime import datetime

from sqlalchemy.orm import Mapped, mapped_column

from database import Base, UtcDateTime, Uuid7
from database.types import generate_uuid7

class Account(Base):
    __tablename__ = "accounts"

    id: Mapped[uuid.UUID] = mapped_column(Uuid7(), primary_key=True, default=generate_uuid7)
    created_at: Mapped[datetime] = mapped_column(UtcDateTime(), nullable=False)

Constraints on models inheriting from Base get conventional names automatically (e.g. a unique column email produces uq_accounts_email).

Column types

Uuid7

import uuid

from sqlalchemy.orm import Mapped, mapped_column

from database import Uuid7
from database.types import generate_uuid7

class Account(Base):
    __tablename__ = "accounts"

    id: Mapped[uuid.UUID] = mapped_column(Uuid7(), primary_key=True, default=generate_uuid7)

PostgreSQL UUID column. When the bound value is None, a v7 UUID is generated (v7 is sortable by time, making it suitable for primary keys in large tables). Existing values pass through unchanged.

UtcDateTime

from datetime import datetime

from sqlalchemy.orm import Mapped, mapped_column

from database import UtcDateTime

class Account(Base):
    __tablename__ = "accounts"

    created_at: Mapped[datetime] = mapped_column(UtcDateTime(), nullable=False)

Timezone-aware DateTime column type; values pass through unmodified.

Pagination

from database import Page

page = Page(items=[...], total=137, page=2, size=25)

page.total_pages    # 6 (ceil(total / size))
page.has_next       # True
page.has_previous   # True

total_pages is ceil(total / size) (0 when size == 0), has_next/has_previous derive from the current page index. Page is a frozen dataclass and compares by value.

Development

uv sync
uv run pytest -q
uv run ruff check .
uv run mypy src/

License

GPL-3.0-or-later

Metadata

Release files for omnixys-database 4.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for omnixys-database 4.0.0
File Size Uploaded
omnixys_database-4.0.0.tar.gz 46.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for omnixys-database 4.0.0
File Interpreter ABI Platform
omnixys_database-4.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 52.0 kB

Release files / omnixys_database-4.0.0.tar.gz

Download URL omnixys_database-4.0.0.tar.gz
Size 46.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cd0f9eb133e894f5e9a917ebf8b82ebea493bd5a4d9e507cf8400d632a0a6d1a
BLAKE2b-256 checksum
How to use checksums
38e486e8b41ee1d13842177b251d93dea602016e7caece88d6123003349fbf93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / omnixys_database-4.0.0-py3-none-any.whl

Download URL omnixys_database-4.0.0-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c3be1b603d4da3a25b283021d79d915272cc9ce8d636fcfcfd95f6dae582e84
BLAKE2b-256 checksum
How to use checksums
66d3723a6e988112ae99c880dd002daa150a7a7f278fe9556e4f01437f648dd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.8 {"installer":{"name":"uv","version":"0.12.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"12","id":"bookworm","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

4.0.0 This release

2 release files

3.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page