Singleton MongoDB client for AWS Lambda and serverless environments
Project description
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
from mongo_client import mongo
# Initialize your connection parameters and optionally set pool size
import os
os.environ['MONGO_URL'] = 'mongodb+srv://<your-connection>/database'
os.environ['MONGO_POOL_SIZE'] = '5' # default value
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 mongo
# 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):
# Connection is reused across Lambda invocations
db = await mongo.get_db()
collection = db["my_collection"]
result = await collection.find_one({"_id": event["id"]})
return {"statusCode": 200, "body": result}
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
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 actvalue_mongo_client-0.1.0.tar.gz.
File metadata
- Download URL: actvalue_mongo_client-0.1.0.tar.gz
- Upload date:
- Size: 41.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b968cce3ede58d4b72128f1919bd979198bb0fc2262266146ed42267b75ccdf
|
|
| MD5 |
1c60317aae4e6bd40a3362f2d3fcf6dd
|
|
| BLAKE2b-256 |
2c5c2127d47e363c1cb8cb7b91e46a4e33e6fc286dd61d125f74aa4c05c539fa
|
File details
Details for the file actvalue_mongo_client-0.1.0-py3-none-any.whl.
File metadata
- Download URL: actvalue_mongo_client-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41f83e72eabc42ca443d963733874492dc2fbdb9f653f697f8f74fde4092bf89
|
|
| MD5 |
826741c2b45b8d3284383dbb2359f8f6
|
|
| BLAKE2b-256 |
56349f3f9f8449ca24464ee95eb84ebd9d53dabc80de60c2822ae320ad8925c2
|