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.
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 whenuse_otel=True).otel_exporter_endpoint(str): OTLP exporter endpoint (required whenuse_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file raccoontools_db-1.0.0.tar.gz.
File metadata
- Download URL: raccoontools_db-1.0.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5230634e5c7cc4d6a6028e7042013cfa3c9ad4ccb80a70cee4834eacac40052
|
|
| MD5 |
49a06ab15e81660104906b3f698b39bd
|
|
| BLAKE2b-256 |
696c11008174697c31cc319abf48d43f14bd0acfcb7ccdf695e1e4e10fe05dc8
|
File details
Details for the file raccoontools_db-1.0.0-py3-none-any.whl.
File metadata
- Download URL: raccoontools_db-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bfd820e1bc693d936fe360bf4f7412ac1daa5cc451fac0f83b8eb1766ff23ac
|
|
| MD5 |
cdba2878f7733b43160cd8dbb12631f1
|
|
| BLAKE2b-256 |
0ba1eaecdfcce09a798d37e94742438b7f19a4b96503338375d1770c21b64da3
|