a pymongo dbClient for multiple clusters
Project description
db-client-python
⚠️ ATTENTION: Recent Module Structure Changes
This package has undergone significant structural changes. If you are upgrading from a previous version, please check the Breaking Changes section below.
A Python client for managing database connections and account-based cluster routing, designed for MongoDB and similar backends.
Features
- Dynamically route database connections based on account information
- Cache and reuse database clients for efficiency
- Support for main and cluster-specific connection strings
- Async API for account cluster lookup
Installation
pip install db-client-smacondi
Usage
1. Prepare your connection string map
This is a dictionary mapping cluster names to MongoDB connection strings. You must provide at least a MAIN entry:
connection_string_map = {
'MAIN': 'mongodb://main.example.com:27017',
'CLUSTER1': 'mongodb://cluster1.example.com:27017',
# ... add more clusters as needed
}
2. Set your Account API URL
This should be an HTTP endpoint that returns account cluster info as JSON. The API must support a GET request with a query parameter accountId and return a response like:
{
"clusterId": "CLUSTER1",
"dbName": "telemetry_db",
"_id": "account123"
}
Example:
account_api_url = "https://api.example.com/account-info"
3. Using DbClientSupport
from db_client_smacondi.db_client_smacondi import get_db_client_smacondi
# Initialize
support = get_db_client_smacondi(apiURL=account_api_url, conn_string_map=connection_string_map)
# Get DB client for a specific cluster
cluster_client = support.get_db_client_by_cluster_id("CLUSTER1")
# Get DB client for a specific account (async)
import asyncio
async def get_account_client():
result = await support.get_db_client_of_account("account123")
db_client = result["db_client"]
db_name = result["db_name"]
# Use db_client as needed
asyncio.run(get_account_client())
4. Closing connections
To properly close all database connections:
from db_client_smacondi.db_client_smacondi import close_all_db_clients
# Close all database connections
close_all_db_clients()
Breaking Changes
If you're upgrading from a previous version, be aware of these breaking changes:
Module Structure
- Module renamed from
dbClientSmaconditodb_client_smacondi - File names changed from camelCase to snake_case:
dbClientSmacondi.py→db_client_smacondi.pydbClient.py→db_client.py
API Changes
- The
DbClientSupportclass is no longer directly exposed - Use
get_db_client_smacondi()function to get a client support instance - The class
_DbClientSupportis now considered an implementation detail get_main_db_client()method has been removed, useget_db_client_by_cluster_id('MAIN')instead
Import Changes
# Old import
from dbClientSmacondi.dbClientSmacondi import DbClientSupport
# New import
from db_client_smacondi.db_client_smacondi import get_db_client_smacondi
Instantiation Changes
# Old instantiation
support = DbClientSupport(apiURL=account_api_url, conn_string_map=connection_string_map)
# New instantiation
support = get_db_client_smacondi(apiURL=account_api_url, conn_string_map=connection_string_map)
Testing
To run tests and check coverage:
pytest --cov=src/db_client_smacondi --cov-report=html
License
AGPL-3.0-or-later
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 db_client_smacondi-1.0.0.tar.gz.
File metadata
- Download URL: db_client_smacondi-1.0.0.tar.gz
- Upload date:
- Size: 42.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
567ebfe05bcf61150732592c5f77368090e27a513096146f92c38e88abdb5b48
|
|
| MD5 |
55481ae1999756606ea2225fa6a2d688
|
|
| BLAKE2b-256 |
72d9d0bda4e76c63bf6b2df659dfa06135a08c4cd0a2179503d7f4a2ec875aeb
|
File details
Details for the file db_client_smacondi-1.0.0-py3-none-any.whl.
File metadata
- Download URL: db_client_smacondi-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e830559ba6949a9535f04e7ede568587fa30b2dabafe2524c77448ef82df90c
|
|
| MD5 |
8f7ef0c983370f9e9ed538f7215162e8
|
|
| BLAKE2b-256 |
accd7dd5315c10fe365c9bb0b1eebb6cc93d2579ea83860e8594b2ffbf41442c
|