Skip to main content

Python database orm functions for Locobuzz, common code that is required in all projects

Project description

Locobuzz Python ORM

# Database Connection Manager

A Python package for managing database connections in both synchronous and asynchronous contexts using SQLAlchemy. The package provides two main classes: `SyncDatabase` for synchronous operations and `AsyncDatabase` for asynchronous operations with PostgreSQL.

## Features

- Singleton pattern to ensure a single instance of the database connection.
- Support for multiple databases with connection pooling.
- Asynchronous support for improved performance in `AsyncDatabase`.
- Easy switching between different databases.
- Metadata initialization for table management.

## Installation

To install the package, you can use pip:

```bash
pip install locobuzz_python_orm  # Replace with the actual package name

Usage

Synchronous Database Management

To use the SyncDatabase, follow these steps:

from database_helper.database.sync_db import SyncDatabase

# Initialize the SyncDatabase
sync_db = SyncDatabase(connection_string='postgresql://user:password@localhost/dbname')

# Use the SyncDatabase context manager
with sync_db:
    # Execute a query
    results = sync_db.execute_query("SELECT * FROM your_table;")
    print(results)

Asynchronous Database Management

For the AsyncDatabase, you'll use async and await keywords:

import asyncio
from database_helper.database.async_db import AsyncDatabase

async def main():
    # Initialize the AsyncDatabase
    async_db = AsyncDatabase(connection_string='postgresql://user:password@localhost/dbname')

    async with async_db:
        # Execute a query asynchronously
        results = await async_db.execute_query("SELECT * FROM your_table;")
        print(results)

# Run the async main function
asyncio.run(main())

Switching Databases

Both classes support switching databases on the fly:

# For SyncDatabase
sync_db.switch_database('new_dbname')

# For AsyncDatabase
await async_db.switch_database('new_dbname')

Initializing Tables

You can initialize tables metadata using:

# For SyncDatabase
sync_db.initialize_tables(['your_table1', 'your_table2'])

# For AsyncDatabase
await async_db.initialize_tables(['your_table1', 'your_table2'])

Error Handling

Both classes include basic error handling. If an error occurs during database operations, exceptions will be raised. You can implement additional logging as needed.

Dependencies

  • SQLAlchemy
  • Database connectors such as pyodbc, pymysql, psycopg2, aioodbc, aiomysql, asyncpg

Author

Atharva Udavant

GitHub Profile
Email

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

locobuzz_python_orm-1.1.3.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

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

locobuzz_python_orm-1.1.3-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file locobuzz_python_orm-1.1.3.tar.gz.

File metadata

  • Download URL: locobuzz_python_orm-1.1.3.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for locobuzz_python_orm-1.1.3.tar.gz
Algorithm Hash digest
SHA256 4effaf99a4603e357fbd317316d722362f5df2f9e3d09d5f054f41f435c0aba9
MD5 70c18bfd4336e0288aa41970eaad1123
BLAKE2b-256 c4cf1da56661a1e3316d53035e19cbd4b6e32f6493060f9b431cd5c398f1005d

See more details on using hashes here.

File details

Details for the file locobuzz_python_orm-1.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for locobuzz_python_orm-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 333485c96b0184ea7982807afa105162ada390f95db8ab69504aee0f80bfc1ae
MD5 61c71c268fab9dc02f4dacb76fe66aee
BLAKE2b-256 3f31f7a9f939675e5b8f42b32e0d04aef0c6d543e63c486b83b704989ebf091c

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