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
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
Close
Hashes for locobuzz_python_orm-1.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cdf2587097f4db89558814214ae25e24382075345095136f741e0f962de69a25 |
|
MD5 | 9597c3a2e64c9d24d04dda30225cae59 |
|
BLAKE2b-256 | 677343cd45785dc3aee73ed1fb73a25493b52dbe1cd80227b50851e0ccc7b001 |
Close
Hashes for locobuzz_python_orm-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a39c4c8540ef35293d1680e8787fbf33bac67a31fd4755022aa19a1fc7eaee31 |
|
MD5 | 765c4507815c651ca3e082a3c2d44510 |
|
BLAKE2b-256 | ff8b6b0c1457112f1066b175883ae3d92cd74659891b58cdf128bc7c929bb2f6 |