Skip to main content

A collection of utilities to be used with databases.

Project description

Raccoon Tools DB

A managed PostgreSQL connection pool (via psycopg 3) with opt-in FastAPI and OpenTelemetry integrations.

vet OSS Components

Installation

Base install:

uv add raccoontools-db

With OpenTelemetry instrumentation:

uv add raccoontools-db[otel]

With FastAPI OTEL instrumentation:

uv add raccoontools-db[fastapi]

All extras:

uv add raccoontools-db[all]

Functionalities

Connection Pool (raccoontools_db.pool)

Provides a module-level singleton connection pool with a simple create/get/close lifecycle.

PoolConfig

Configuration dataclass for the connection pool.

Fields:

  • conn_info (str): PostgreSQL connection string.
  • min_size (int): Minimum connections in pool (default: 2).
  • max_size (int): Maximum connections in pool (default: 10).
  • use_otel (bool): Enable OpenTelemetry tracing (default: False).
  • otel_service_name (str): Service name for OTEL (required when use_otel=True).
  • otel_exporter_endpoint (str): OTLP exporter endpoint (required when use_otel=True).
  • otel_use_http (bool): Use HTTP protocol for OTLP (default: True).

create_pool(config: PoolConfig) -> None

Creates and opens the global connection pool. Raises RuntimeError if a pool already exists.

get_pool() -> ConnectionPool

Returns the active pool. Raises RuntimeError if the pool has not been created.

close_pool() -> None

Closes the pool and resets state. Safe to call if already closed or never created.

Example: Plain usage (no FastAPI, no OTEL)

from raccoontools_db.pool import PoolConfig, create_pool, get_pool, close_pool

config = PoolConfig(conn_info="postgresql://user:pass@localhost:5432/mydb")
create_pool(config)

pool = get_pool()
with pool.connection() as conn:
    with conn.cursor() as cur:
        cur.execute("SELECT 1")
        print(cur.fetchone())

close_pool()

Example: With OpenTelemetry

from raccoontools_db.pool import PoolConfig, create_pool, get_pool, close_pool

config = PoolConfig(
    conn_info="postgresql://user:pass@localhost:5432/mydb",
    use_otel=True,
    otel_service_name="my-service",
    otel_exporter_endpoint="http://localhost:4318",
)
create_pool(config)

pool = get_pool()
# Use pool as normal — logging is handled via OTEL tracing

FastAPI Integration (raccoontools_db.fastapi)

create_lifespan(config: PoolConfig)

An async context manager that ties the pool lifecycle to FastAPI's startup/shutdown.

Example: With FastAPI

from fastapi import FastAPI
from raccoontools_db.fastapi import create_lifespan
from raccoontools_db.pool import PoolConfig, get_pool

config = PoolConfig(conn_info="postgresql://user:pass@localhost:5432/mydb")
app = FastAPI(lifespan=create_lifespan(config))

@app.get("/health")
def health():
    pool = get_pool()
    with pool.connection() as conn:
        conn.execute("SELECT 1")
    return {"status": "ok"}

Example: With FastAPI + OpenTelemetry

from fastapi import FastAPI
from raccoontools_db.fastapi import create_lifespan
from raccoontools_db.pool import PoolConfig, get_pool

config = PoolConfig(
    conn_info="postgresql://user:pass@localhost:5432/mydb",
    use_otel=True,
    otel_service_name="my-service",
    otel_exporter_endpoint="http://localhost:4318",
)
app = FastAPI(lifespan=create_lifespan(config))

Changelog

Project details


Download files

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

Source Distribution

raccoontools_db-1.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

raccoontools_db-1.0.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file raccoontools_db-1.0.1.tar.gz.

File metadata

  • Download URL: raccoontools_db-1.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for raccoontools_db-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d28537ee1c2b62b31c7207ffcdb43b40ea6fab49a8c642e0c5fe341609725e6c
MD5 577b91dfd8275598080c921d4169ff00
BLAKE2b-256 05ccbdf3d6d03033ab09c2045a0ee92ced586469cbe8059cece158d1735b65c9

See more details on using hashes here.

File details

Details for the file raccoontools_db-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for raccoontools_db-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5e9f34260d4c2c07dc3f2ceb84902fb61e99883167d7fd73e8cbc505e3f0eef
MD5 0b43fd593be4045ec8cbd680a095547b
BLAKE2b-256 ea2a487b8e2183180059c154d107be39519f7468965db0fa8deabbe39681dcbd

See more details on using hashes here.

Supported by

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