What problem does it solve?
This pattern enables building reusable Python libraries that can run standalone with their own database pool or be embedded into a larger application using a shared pool, avoiding code duplication across services.
Core Features & Use Cases
- Accepts either a connection_string or a db_manager to flexibly manage lifecycles in both standalone and embedded modes.
- Always runs migrations into a per-schema namespace to ensure isolation while enabling library reuse.
- Supports clean up; libraries only close connections they created, leaving external pools intact.
- Suited for PyPI distribution or internal tooling where multiple libraries share a single pool or require isolated schemas.
Quick Start
Standalone mode: provide a connection_string and call initialize, then use library methods and finally close.
lib = MyLibrary(connection_string="postgresql://localhost/mydb")
await lib.initialize()
use lib
await lib.close()