Skip to main content

A Python package providing a unified interface for database operations, supporting Parquet, SQLite, and PostgreSQL backends.

Project description

DBEngine

A unified, production-ready database interface for Python that provides seamless access to SQLite, Parquet, and PostgreSQL databases through a consistent pandas-based API.

Features

  • Multi-Database Support: Work with SQLite, Parquet files, and PostgreSQL databases using the same API
  • Production-Ready: Built-in configuration management and error handling
  • Pandas Integration: Native pandas DataFrame/Series support for all operations
  • High Performance: Connection pooling, batching, and optimised data handling
  • PostgreSQL Server Management: Programmatically start/stop PostgreSQL servers using Docker for development and testing
  • Comprehensive Testing: Full test suite with validation and integration tests
  • Easy Setup: Simple installation and configuration with sensible defaults

Quick Start

Installation

pip install dbengine

Basic Usage

from dbengine import create_database
import pandas as pd

# Create a SQLite database
db = create_database('sqlite', database='my_data')

# Create sample data
data = pd.DataFrame({
    'id': [1, 2, 3],
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35]
})

# Write data
db.write(table_name='users', item=data)

# Read data
users = db.query(table_name='users')
print(users)

# Query data
young_users = db.query(criteria=[('age', '=', 25)], table_name='users')
print(young_users)

PostgreSQL Server Management

import pandas as pd
from dbengine import create_postgres_server, PostgreSQLDatabase, DatabaseConfig

# Start a PostgreSQL server for development/testing
with create_postgres_server(port=5433) as server:
    # Create database configuration from server connection params
    config = DatabaseConfig(
        database_config={
            "db_type": "postgresql",
            "params": server.get_connection_params()
        }
    )

    # Create database connection
    db = PostgreSQLDatabase(config)

    # Use the database normally
    data = pd.DataFrame({'id': [1, 2], 'name': ['Alice', 'Bob']})
    db.write(table_name='users', item=data)

    # Database and server automatically cleaned up

Supported Databases

SQLite

  • Use Case: Local development, testing, lightweight applications
  • Features: File-based, serverless, ACID transactions
  • Configuration: Simple file path specification

Parquet

  • Use Case: Data analytics, archival, big data processing
  • Features: Columnar storage, compression, fast analytics
  • Configuration: Directory path with compression options

PostgreSQL

  • Use Case: Production applications, multi-user systems
  • Features: Full ACID compliance, connection pooling, advanced SQL
  • Configuration: Host, port, credentials, SSL support
  • Server Management: Docker-based server lifecycle management for development/testing

Configuration

DBEngine supports configuration files for different environments with YAML format.

Sample Configuration Files

There are sample configuration files in the sample_configs folder.

SQLite Configuration:

database:
  db_type: sqlite
  params:
    path: data/database.db

logging:
  level: INFO
  format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  handlers: null

PostgreSQL Configuration:

database:
  db_type: postgresql
  params:
    host: localhost
    port: 5432
    database: dbengine
    user: dbengine
    password: your_password_here

logging:
  level: INFO
  format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  handlers: null

Parquet Configuration:

database:
  db_type: parquet
  params:
    path: data/parquet

logging:
  level: INFO
  format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
  handlers: null

Note: All configuration values must be set in the YAML configuration files.

Creating Configuration Files

DBEngine provides a convenient function to generate sample configuration files for any supported database type:

from dbengine import create_config_file

# Create a SQLite configuration file
create_config_file('config/sqlite_config.yaml', 'sqlite', load_config=True)

# Create a PostgreSQL configuration file
create_config_file('config/postgres_config.yaml', 'postgresql', load_config=False)

# Create a Parquet configuration file
create_config_file('config/parquet_config.yaml', 'parquet', load_config=True)

Function Parameters:

  • path: Where to create the configuration file
  • db_type: Database type ('sqlite', 'postgresql', or 'parquet')
  • load_config: If True, returns a DatabaseConfig object; if False, only creates the file
  • overwrite: If True, overwrites existing files; if False, loads existing files when load_config=True

Advanced Usage

PostgreSQL Server Management

DBEngine includes utilities to programmatically start and stop PostgreSQL servers using Docker, making it perfect for development workflows and testing:

import pandas as pd
from dbengine import PostgreSQLServerManager, PostgreSQLDatabase, DatabaseConfig

# Manual server lifecycle management
server = PostgreSQLServerManager(port=5433, database='my_test_db')
server.start()

try:
    # Create database configuration from server connection params
    config = DatabaseConfig(
        database_config={
            "db_type": "postgresql",
            "params": server.get_connection_params()
        }
    )

    # Create database connection
    db = PostgreSQLDatabase(config)

    # Perform database operations
    data = pd.DataFrame({'id': [1, 2, 3], 'value': ['a', 'b', 'c']})
    db.write(table_name='test_table', item=data)

    result = db.query(table_name='test_table')
    print(result)

finally:
    server.stop()

# Context manager (recommended)
with PostgreSQLServerManager(port=5434) as server:
    config = DatabaseConfig(
        database_config={
            "db_type": "postgresql",
            "params": server.get_connection_params()
        }
    )
    db = PostgreSQLDatabase(config)
    # Server automatically stopped when exiting context

Server Management Features:

  • Docker Integration: Automatic container lifecycle management
  • Port Configuration: Avoid conflicts with existing PostgreSQL instances
  • Custom Databases: Create servers with specific database names and credentials
  • Health Checks: Automatic server readiness detection
  • Multiple Servers: Run multiple isolated PostgreSQL instances simultaneously

Examples

See the notebooks/ directory for comprehensive usage examples.

Testing

Run the comprehensive test suite:

# Run all tests
pytest

# Run with coverage
pytest --cov=src --cov-report=html

Note: PostgreSQL server management tests require Docker to be running. Tests will be automatically skipped if Docker is not available.

Development

Setup Development Environment

git clone https://github.com/tomemgouveia/dbengine.git
cd dbengine
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install -e .

Prerequisites

For PostgreSQL server management features, you'll need:

  • Docker: Required for PostgreSQL server management utilities
# Install Docker (macOS)
brew install docker

# Install Docker (Ubuntu/Debian)
sudo apt-get update && sudo apt-get install docker.io

# Start Docker daemon
sudo systemctl start docker  # Linux
# or use Docker Desktop on macOS/Windows

Code Quality

The project uses automated code quality tools:

# Format code
black src/ tests/

# Check imports
isort src/ tests/

# Lint code
flake8 src/ tests/

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

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

dbengine-1.1.0.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

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

dbengine-1.1.0-py2.py3-none-any.whl (24.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file dbengine-1.1.0.tar.gz.

File metadata

  • Download URL: dbengine-1.1.0.tar.gz
  • Upload date:
  • Size: 34.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dbengine-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d4b0201e2dbbca8a3811f9a5e57bb0db8c31ff8d8faa8f9749523c73901b5586
MD5 08cac7cd724ff7f89086509a480165fb
BLAKE2b-256 e3fb1b74e1c83399b245725f32cf4c7c03bb5ffa5f93d01b07ee13dd09c7f594

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbengine-1.1.0.tar.gz:

Publisher: python-publish.yml on tomemgouveia/dbengine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dbengine-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: dbengine-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dbengine-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1670a510d1c9de1402f876adbb6c2d62744400d67a549d8cae0eda45d11f79dd
MD5 fb6c2db1b80b22a260199108bc38b9d0
BLAKE2b-256 b28e47b6935f240cc869cffdf81022d7ac6094d7b13d871da9d880bdb76a182c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dbengine-1.1.0-py2.py3-none-any.whl:

Publisher: python-publish.yml on tomemgouveia/dbengine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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