dual-mode-library

Build reusable Python libraries with pgdbm for standalone or shared-pool deployments.

Updated Jul 28, 2025
One-click install
npx skills add https://github.com/juanre/pgdbm --skill dual-mode-library
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dual-mode-library
Source: https://github.com/juanre/pgdbm/tree/main/skills/dual-mode-library
Command: npx skills add https://github.com/juanre/pgdbm --skill dual-mode-library

SYSTEM DOCUMENTATION & REQUIREMENTS

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()

Frequently Asked Questions about dual-mode-library

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a Python library that works with pgdbm in both standalone and shared-pool modes?

Build dual-mode libraries by accepting either a connection_string or db_manager parameter. This lets your library run standalone with its own pool or embed in larger applications using shared pools, with per-schema migrations ensuring isolation in both deployment patterns.

What's the best way to handle database migrations when distributing Python libraries across multiple services?

Use per-schema migrations scoped to your library's namespace. This isolation pattern allows multiple libraries to safely share a single pgdbm pool without schema conflicts while maintaining clean, reusable code for PyPI distribution or internal tooling.

Can I use the same library code for standalone deployments and embedded shared-pool scenarios?

Yes. The dual-mode pattern eliminates code duplication by designing libraries to accept connection management from either mode—standalone initialization or external pool injection—while always running migrations and handling cleanup only for pools the library creates.

How do I ensure database cleanup doesn't break shared pools when using library code?

Libraries should only close connections they own. By tracking whether the pool was created internally (from connection_string) or passed externally (db_manager), cleanup remains safe in shared environments without terminating pools other services depend on.

What's the entry barrier for distributing reusable database libraries across teams or PyPI?

The dual-mode approach removes the barrier: design libraries to work standalone or embedded without code changes. Accept flexible connection parameters, enforce schema isolation via migrations, and implement ownership-aware cleanup—enabling distribution without forcing deployment patterns on consumers.