Skip to main content

A lightweight PostgreSQL operations manager for AWS Lambda

Project description

dbops-manager

A lightweight PostgreSQL operations manager optimized for AWS Lambda environments. This package provides a simple, efficient interface for PostgreSQL database operations with proper connection management and error handling.

Features

  • Lightweight PostgreSQL operations with minimal dependencies
  • Environment variable and dictionary-based configuration
  • Connection pooling optimization for AWS Lambda
  • Comprehensive error handling and logging
  • Support for parameterized queries
  • Dictionary result format support
  • Stateless operation model

Installation

pip install dbops-manager

Quick Start

Using Environment Variables

from dbops_manager import PostgresOps

# Initialize with environment variables
# Required env vars: DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD
db = PostgresOps.from_env(logging_enabled=True)

# Execute a query
results = db.fetch("SELECT * FROM users WHERE active = %s", [True])

# Don't forget to close the connection
db.close()

Using Configuration Dictionary

from dbops_manager import PostgresOps

config = {
    'host': 'localhost',
    'port': '5432',
    'dbname': 'example',
    'user': 'postgres',
    'password': 'postgres'
}

db = PostgresOps.from_config(config, logging_enabled=True)

Complete Example

Here's a complete example demonstrating table creation, data insertion, and querying:

import logging
from dbops_manager import PostgresOps, QueryError

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

def create_test_table(db: PostgresOps) -> None:
    """Create a test table if it doesn't exist."""
    create_table_sql = """
    CREATE TABLE IF NOT EXISTS test_users (
        id SERIAL PRIMARY KEY,
        name VARCHAR(100) NOT NULL,
        email VARCHAR(255) UNIQUE NOT NULL,
        created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
    )
    """
    db.execute(create_table_sql)

def insert_test_data(db: PostgresOps) -> None:
    """Insert test data into the table."""
    users = [
        ("John Doe", "john@example.com"),
        ("Jane Smith", "jane@example.com"),
        ("Bob Wilson", "bob@example.com")
    ]
    
    insert_sql = """
    INSERT INTO test_users (name, email)
    VALUES (%s, %s)
    ON CONFLICT (email) DO NOTHING
    RETURNING id, name, email
    """
    
    for user in users:
        try:
            result = db.fetch(insert_sql, list(user))
            if result:
                print(f"Inserted user: {result[0]}")
            else:
                print(f"User with email {user[1]} already exists")
        except QueryError as e:
            print(f"Error inserting user {user}: {e}")

def main():
    config = {
        'host': 'localhost',
        'port': '5432',
        'dbname': 'example',
        'user': 'postgres',
        'password': 'postgres'
    }
    
    try:
        # Initialize database connection
        db = PostgresOps.from_config(config, logging_enabled=True)
        
        # Create table
        create_test_table(db)
        
        # Insert data
        insert_test_data(db)
        
        # Query all users
        all_users = db.fetch("SELECT * FROM test_users ORDER BY id")
        for user in all_users:
            print(f"User {user['id']}: {user['name']} ({user['email']})")
    
    finally:
        if 'db' in locals():
            db.close()

if __name__ == "__main__":
    main()

API Reference

PostgresOps Class

Class Methods

  • from_env(env_prefix="DB_", logging_enabled=False): Create instance from environment variables
  • from_config(config: Dict[str, str], logging_enabled=False): Create instance from configuration dictionary

Instance Methods

  • fetch(query: str, params: Optional[List[Any]] = None, as_dict: bool = True): Execute SELECT query and return results
  • execute(query: str, params: Optional[List[Any]] = None): Execute modification query (INSERT/UPDATE/DELETE)
  • close(): Close database connection

Environment Variables

When using from_env(), the following environment variables are required:

  • DB_HOST: Database host
  • DB_PORT: Database port (default: 5432)
  • DB_NAME: Database name
  • DB_USER: Database user
  • DB_PASSWORD: Database password
  • DB_SSLMODE: SSL mode (optional, default: prefer)

Error Handling

The package provides custom exceptions for better error handling:

from dbops_manager import PostgresError, ConnectionError, QueryError, ConfigurationError

try:
    db = PostgresOps.from_env()
    results = db.fetch("SELECT * FROM users")
except ConnectionError as e:
    print(f"Failed to connect: {e}")
except QueryError as e:
    print(f"Query failed: {e}")
except ConfigurationError as e:
    print(f"Invalid configuration: {e}")
except PostgresError as e:
    print(f"General database error: {e}")

AWS Lambda Usage

The package is optimized for AWS Lambda environments. Example Lambda function:

from dbops_manager import PostgresOps

def lambda_handler(event, context):
    try:
        db = PostgresOps.from_env()
        results = db.fetch("SELECT * FROM users")
        return {
            'statusCode': 200,
            'body': results
        }
    finally:
        db.close()

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

dbops_manager-0.1.3.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

dbops_manager-0.1.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file dbops_manager-0.1.3.tar.gz.

File metadata

  • Download URL: dbops_manager-0.1.3.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dbops_manager-0.1.3.tar.gz
Algorithm Hash digest
SHA256 501d1f9e94919049d915fbd1852c2370675f5deda7bfa5d3c4982fbfdfe83660
MD5 43828bcefc109760fd54d45bb57772f3
BLAKE2b-256 0992b4ff5980abf20e65150be58ee545bedd6d96a5a286d13e3c7664bcf1b91d

See more details on using hashes here.

File details

Details for the file dbops_manager-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: dbops_manager-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for dbops_manager-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a892a6b64d8a9b19091b01cc8fdb23434a29ccadf2cb23a49e5fb9305510d9
MD5 6cd458f9c0f65f552c9548fd721d5570
BLAKE2b-256 fa3da4a4c0e52335dfa6f2253c85da2189ac6e27794e48563417188ff6794e7f

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