Registry / database / nsj-multi-database-lib

nsj-multi-database-lib

JSON →
library2.1.0pypypiunverified

The NSJ Multi-Database Library is a Python module designed to enable applications, particularly those built with FastAPI, to efficiently manage and interact with multiple database connections within the same application. It provides an abstraction layer for various database engines (e.g., PostgreSQL, Oracle, SQL Server), simplifying connection pooling, session management, and transactional data access objects (DAOs). The current version is 2.1.0, and the library maintains a steady release cadence for improvements and bug fixes, supporting Python versions 3.6 up to 3.9.

pip install nsj-multi-database-lib
INSTALL
IMPORT
SIG · NSJ-MULTI-DATABASE
N
nsj-multi-database-lib
databasepythonv2.1.0
Install
12.0s avg
Import
Disk
107MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 109.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 12.0s · import 0.000s · 102MB
107MB installed
● package 107MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ConnectionPool
from nsj_multi_database_lib.connection.connection_pool import ConnectionPool
DatabaseManager
from nsj_multi_database_lib.connection.database_manager import DatabaseManager
DaoBase
from nsj_multi_database_lib.dao.dao_base import DaoBase
from nsj_multi_database_lib.dao_base import DaoBase
DaoBase is within the 'dao' submodule, not directly under the root package.
TransactionalDao
from nsj_multi_database_lib.dao.transactional_dao import TransactionalDao

This quickstart demonstrates how to configure multiple in-memory SQLite databases using `nsj-multi-database-lib`. It shows how to initialize the `ConnectionPool` with a dictionary of database configurations, obtain sessions for different databases using `DatabaseManager.get_session()`, and perform basic SQL operations within a transactional context. Remember to replace the SQLite in-memory setup with your actual database connection strings and credentials.

import os from nsj_multi_database_lib.connection.connection_pool import ConnectionPool from nsj_multi_database_lib.connection.database_manager import DatabaseManager from sqlalchemy import text # Mock Database setup for example (using SQLite in-memory) DB_NAME_DEFAULT = "default_db" DB_NAME_AUDIT = "audit_db" # NOTE: In a real application, connection_string would use actual DB credentials. # Example for PostgreSQL: "postgresql://user:password@host:port/database" DATABASE_CONFIGS = { DB_NAME_DEFAULT: { "engine": "sqlite", "db": ":memory:", "connection_string": "sqlite:///:memory:", "schema": None, "label": DB_NAME_DEFAULT }, DB_NAME_AUDIT: { "engine": "sqlite", "db": ":memory:", "connection_string": "sqlite:///:memory:", "schema": None, "label": DB_NAME_AUDIT } } # 1. Initialize the connection pool with your database configurations ConnectionPool.configure(DATABASE_CONFIGS) try: print("\n--- Using Default Database ---") # 2. Get a database session using the context manager with DatabaseManager.get_session(DB_NAME_DEFAULT) as session: # Example operations with SQLAlchemy 1.x compatible syntax session.execute(text("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")) session.execute(text("INSERT INTO users (id, name) VALUES (1, 'Alice')")) session.execute(text("INSERT INTO users (id, name) VALUES (2, 'Bob')")) session.commit() result = session.execute(text("SELECT * FROM users")).fetchall() print(f"Retrieved from {DB_NAME_DEFAULT}: {result}") print("\n--- Using Audit Database ---") with DatabaseManager.get_session(DB_NAME_AUDIT) as session: session.execute(text("CREATE TABLE logs (id INTEGER PRIMARY KEY, event TEXT)")) session.execute(text("INSERT INTO logs (id, event) VALUES (1, 'User Alice created')")) session.commit() result = session.execute(text("SELECT * FROM logs")).fetchall() print(f"Retrieved from {DB_NAME_AUDIT}: {result}") except Exception as e: print(f"An error occurred: {e}") finally: # 3. Always close connections when done to release resources ConnectionPool.close_all_connections() print("\nAll database connections closed.")
Debug
Known issues
breakingThe `ConnectionPool.configure()` method signature changed significantly in version 2.0.0. It now expects a dictionary where keys are database labels (strings) and values are configuration dictionaries, instead of a list of `DatabaseConnectionConfigDTO` objects.
fix
Update your `ConnectionPool.configure()` call to pass a dictionary of configurations. For example, `ConnectionPool.configure({'my_db': {'engine': 'pg', 'connection_string': '...'}})`.
affects: >=2.0.0
gotchaThis library explicitly depends on `SQLAlchemy` versions less than 2.0.0 (e.g., `SQLAlchemy>=1.4.0,<2.0.0`). Attempting to use `SQLAlchemy 2.x` will lead to dependency conflicts or runtime errors, as its API has breaking changes.
fix
Ensure your project's `requirements.txt` or `pyproject.toml` pins `SQLAlchemy` to a `1.x` version (e.g., `SQLAlchemy~=1.4.0`). Do not install `SQLAlchemy 2.x` if you are using `nsj-multi-database-lib`.
affects: All versions
gotchaFailure to call `ConnectionPool.configure()` before attempting to retrieve a session via `DatabaseManager.get_session()` will result in an `AttributeError` or `TypeError`.
fix
Always initialize the connection pool at application startup using `ConnectionPool.configure(your_configs_dict)` before any database operations.
affects: All versions
Upgrade
Version history
2.1.0latest on PyPI · released Dec 17, 2025
Audit
Dependencies
SQLAlchemyrequiredCore dependency for ORM and database interactions. The library specifically requires SQLAlchemy < 2.0.0.
Agent activity
5 hits · last 30 days
node
4
Resources
nsj-multi-database-lib — pip install nsj-multi-database-lib · libregistry