actvalue.mongo-client
Singleton client for MongoDB connection
Install
pip install actvalue.mongo-client
Or with uv:
uv add actvalue.mongo-client
Client Usage
The client is used to create and share MongoDB connection pool.
import asyncio
import os
from mongo_client import MongoDbConnection
# Initialize your connection parameters and optionally set pool size
os.environ['MONGO_URL'] = 'mongodb+srv://<your-connection>/database'
os.environ['MONGO_POOL_SIZE'] = '5' # default value
# Create a singleton instance at module level
mongo = MongoDbConnection()
async def main():
# Create connection pool if not existing already
db = await mongo.get_db()
users = db["users"]
await users.insert_one({"username": "test123", "password": "test123"})
# Some other time, some other place in code
# Connection pool is reused
db = await mongo.get_db()
user = await users.find_one({"username": "test123"})
print(user)
asyncio.run(main())
AWS Lambda Usage
Perfect for serverless environments where connection reuse is critical:
import os
from mongo_client import MongoDbConnection
# Set environment variables (typically from Lambda configuration)
os.environ['MONGO_URL'] = 'mongodb+srv://...'
os.environ['MONGO_POOL_SIZE'] = '5'
async def lambda_handler(event, context):
# IMPORTANT: Initialize within handler due to Python asyncio event loop limitations
# Each Lambda invocation may create a new event loop, and AsyncMongoClient
# is bound to the event loop it was created on
mongo = MongoDbConnection()
db = await mongo.get_db()
collection = db["my_collection"]
result = await collection.find_one({"_id": event["id"]})
return {"statusCode": 200, "body": result}
⚠️ Python Lambda Note: Unlike the TypeScript version, the Python client should be initialized inside the handler function. This is because Python's AsyncMongoClient binds to the asyncio event loop at creation time. AWS Lambda may create new event loops between invocations, causing "Cannot use AsyncMongoClient in different event loop" errors if the instance is created at module level.
Features
- 🚀 Async/await support with PyMongo 4.0+
- 🔄 Singleton pattern for connection reuse
- 🔒 Thread-safe connection initialization
- ⚙️ Environment-based configuration
- 📦 Connection pooling optimized for serverless
- ⏱️ Configurable timeouts and pool size
- 🎯 Designed for AWS Lambda and similar platforms
Environment Variables
MONGO_URL: MongoDB connection string (required)MONGO_POOL_SIZE: Connection pool size (optional, default: "5")
License
MIT
Metadata
Release files for actvalue.mongo-client 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| actvalue_mongo_client-2.0.0.tar.gz | 64.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| actvalue_mongo_client-2.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 68.5 kB
Release files / actvalue_mongo_client-2.0.0.tar.gz
| Download URL | actvalue_mongo_client-2.0.0.tar.gz |
|---|---|
| Size | 64.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d07b8eb5278ee246d72b426cb95fd4d255a1ad21c74106c78efa8a3405203775
|
|
BLAKE2b-256 checksum How to use checksums |
eb5d66f0d269c40669c51ef67b4bfaf17e666321d1a5aba54a5f43ffe7eab4a6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.3
|
Release files / actvalue_mongo_client-2.0.0-py3-none-any.whl
| Download URL | actvalue_mongo_client-2.0.0-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
acf77861a39bff35264192321bab6ed3ea5d30ecb19ed5ee2ea6a5c65d074f7b
|
|
BLAKE2b-256 checksum How to use checksums |
239b73654945f52bacd9eac62378efc625a77bce7d581ae5a3fbb2a9589fb9f3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.3
|