Skip to main content

MongoDB storage plugin for PyConfBox

Project description

PyConfBox MongoDB Plugin

MongoDB document database storage backend for PyConfBox

This plugin provides MongoDB document database storage backend for PyConfBox, enabling flexible schema-less configuration storage with powerful querying capabilities and horizontal scaling support.

한국어 문서: README_ko.md | English Documentation: README.md (current)

🚀 Installation

pip install pyconfbox-mongodb

📋 Requirements

  • Python 3.8+
  • pyconfbox >= 0.1.0
  • pymongo >= 4.0.0

💡 Usage

Basic Usage

from pyconfbox_mongodb import MongoDBStorage
from pyconfbox import Config

# MongoDB storage configuration
mongodb_storage = MongoDBStorage(
    host='localhost',
    port=27017,
    database='config_db',
    collection='configurations'
)

config = Config(default_storage=mongodb_storage)

# Store and retrieve configurations
config.set('app_name', 'MyApp')
config.set('debug', True)
config.set('max_connections', 100)

app_name = config.get('app_name')
debug_mode = config.get('debug')

Connection URI Usage

from pyconfbox_mongodb import MongoDBStorage

# Using MongoDB connection URI
mongodb_storage = MongoDBStorage(
    uri='mongodb://username:password@localhost:27017/config_db'
)

config = Config(default_storage=mongodb_storage)

Advanced Configuration

from pyconfbox_mongodb import MongoDBStorage

mongodb_storage = MongoDBStorage(
    host='localhost',
    port=27017,
    database='app_config',
    collection='configurations',
    username='config_user',
    password='secure_password',
    auth_source='admin',  # Authentication database
    replica_set='rs0',  # Replica set name
    read_preference='secondaryPreferred',  # Read preference
    write_concern={'w': 'majority'},  # Write concern
    ssl=True,  # Enable SSL
    ssl_cert_reqs='CERT_REQUIRED',  # SSL certificate requirements
    connect_timeout=5000,  # Connection timeout in ms
    server_selection_timeout=3000  # Server selection timeout in ms
)

config = Config(default_storage=mongodb_storage)

# Store complex nested data structures
config.set('api_config', {
    'endpoints': {
        'users': {
            'url': '/api/v1/users',
            'methods': ['GET', 'POST'],
            'rate_limit': 1000
        },
        'orders': {
            'url': '/api/v1/orders',
            'methods': ['GET', 'POST', 'PUT'],
            'rate_limit': 500
        }
    },
    'middleware': ['auth', 'cors', 'rate_limit'],
    'features': {
        'caching': True,
        'logging': {
            'level': 'INFO',
            'handlers': ['console', 'file']
        }
    }
})

Replica Set and Sharding Support

from pyconfbox_mongodb import MongoDBStorage

# Replica set configuration
mongodb_storage = MongoDBStorage(
    uri='mongodb://user:pass@host1:27017,host2:27017,host3:27017/config_db?replicaSet=rs0',
    read_preference='secondaryPreferred',
    write_concern={'w': 'majority', 'j': True}
)

# Sharded cluster configuration
mongodb_storage = MongoDBStorage(
    uri='mongodb://user:pass@mongos1:27017,mongos2:27017/config_db',
    read_preference='nearest'
)

config = Config(default_storage=mongodb_storage)

🎯 Features

  • 📄 Document Storage: Native document-based configuration storage
  • 🔍 Rich Querying: Powerful MongoDB query capabilities
  • 📈 Horizontal Scaling: Support for replica sets and sharding
  • 🔒 Authentication: Multiple authentication mechanisms
  • 🛡️ SSL/TLS Support: Secure connections with SSL/TLS
  • ⚡ Connection Pooling: Efficient connection management
  • 🎯 Flexible Schema: Schema-less document structure
  • 📊 Aggregation: Advanced data aggregation pipelines
  • 🔄 Change Streams: Real-time configuration change monitoring

🏗️ Document Structure

Configurations are stored as MongoDB documents with the following structure:

{
    "_id": ObjectId("..."),
    "config_key": "app_name",
    "config_value": "MyApp",
    "data_type": "str",
    "scope": "global",
    "is_immutable": false,
    "metadata": {
        "created_at": ISODate("2024-01-01T00:00:00Z"),
        "updated_at": ISODate("2024-01-01T00:00:00Z"),
        "version": 1
    }
}

Indexes

The plugin automatically creates the following indexes for optimal performance:

// Unique index on config_key
db.configurations.createIndex({ "config_key": 1 }, { unique: true })

// Compound index for scope-based queries
db.configurations.createIndex({ "scope": 1, "config_key": 1 })

// Index for metadata queries
db.configurations.createIndex({ "metadata.updated_at": -1 })

🔧 Configuration Options

Parameter Type Default Description
host str 'localhost' MongoDB server host
port int 27017 MongoDB server port
database str Required Database name
collection str 'configurations' Collection name
uri str None Complete MongoDB URI
username str None Authentication username
password str None Authentication password
auth_source str None Authentication database
replica_set str None Replica set name
read_preference str 'primary' Read preference
write_concern dict None Write concern options
ssl bool False Enable SSL/TLS
connect_timeout int 20000 Connection timeout (ms)
server_selection_timeout int 30000 Server selection timeout (ms)

🔍 Advanced Querying

from pyconfbox_mongodb import MongoDBStorage
from pymongo import ASCENDING, DESCENDING

# Custom storage with advanced querying
class AdvancedMongoDBStorage(MongoDBStorage):
    def find_by_pattern(self, pattern: str):
        """Find configurations matching a pattern."""
        return self.collection.find({
            "config_key": {"$regex": pattern, "$options": "i"}
        })
    
    def find_recent_changes(self, hours: int = 24):
        """Find configurations changed in the last N hours."""
        from datetime import datetime, timedelta
        cutoff = datetime.utcnow() - timedelta(hours=hours)
        return self.collection.find({
            "metadata.updated_at": {"$gte": cutoff}
        }).sort("metadata.updated_at", DESCENDING)
    
    def aggregate_by_scope(self):
        """Aggregate configurations by scope."""
        pipeline = [
            {"$group": {
                "_id": "$scope",
                "count": {"$sum": 1},
                "keys": {"$push": "$config_key"}
            }}
        ]
        return list(self.collection.aggregate(pipeline))

# Usage
storage = AdvancedMongoDBStorage(
    host='localhost',
    database='config_db'
)

# Find all API-related configurations
api_configs = storage.find_by_pattern("api_.*")

# Find recent changes
recent_changes = storage.find_recent_changes(hours=1)

# Get configuration statistics by scope
scope_stats = storage.aggregate_by_scope()

📖 Documentation

🔗 Related Packages

🤝 Contributing

Contributions are welcome! Please see our Contributing Guide for details.

📄 License

MIT License - See the LICENSE file for details.


Scale your configurations with MongoDB flexibility! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyconfbox_mongodb-0.1.2.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyconfbox_mongodb-0.1.2-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file pyconfbox_mongodb-0.1.2.tar.gz.

File metadata

  • Download URL: pyconfbox_mongodb-0.1.2.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for pyconfbox_mongodb-0.1.2.tar.gz
Algorithm Hash digest
SHA256 0a7c24aaf3d6539820f4d3bfb003c4364e4f173d24fa1a19d811021d374e17eb
MD5 61ca07e8a96a0bd121feeacdf657821f
BLAKE2b-256 e43ef9a52bba2d1a0fa0b4c2fbb37ffc2c8d123fb8e826d019e3a5fe228fb279

See more details on using hashes here.

File details

Details for the file pyconfbox_mongodb-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pyconfbox_mongodb-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 764ae00656ad3002221fb43fdf27d870a94eba2cd38fa8035ed52811542f6a3b
MD5 d99bd068ddee76724dbf56f8f0e1ae46
BLAKE2b-256 0033143b5c9aa463b05227397b65850abc56b6eee55eea619ac57f685bb6644d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page