Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/deepmancer/asyncpg-client
Asyncpg Client is a simple and easy-to-use module to interact with PostgreSQL databases
https://github.com/deepmancer/asyncpg-client
async asynchronous asyncio asyncpg database postgres postgresql postgresql-database pytho
Last synced: 3 months ago
JSON representation
Asyncpg Client is a simple and easy-to-use module to interact with PostgreSQL databases
- Host: GitHub
- URL: https://github.com/deepmancer/asyncpg-client
- Owner: deepmancer
- License: apache-2.0
- Created: 2024-07-22T07:32:53.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-08-16T11:35:16.000Z (5 months ago)
- Last Synced: 2024-10-31T08:33:38.398Z (3 months ago)
- Topics: async, asynchronous, asyncio, asyncpg, database, postgres, postgresql, postgresql-database, pytho
- Language: Python
- Homepage:
- Size: 42 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 📚 Async Postgres Client
**`asyncpg-client`** is a powerful Python package designed for seamless asynchronous interactions with PostgreSQL, leveraging SQLAlchemy. It ensures efficient, thread-safe operations with its singleton-based connection pooling mechanism, making database management easier and faster.
---
## ✨ Features
- ⚡ **Asynchronous Operations**: Asynchronous database connections using SQLAlchemy for high performance.
- 🛠️ **Singleton Pattern**: Efficiently manage database connections using a singleton design.
- 🔄 **Context Manager Support**: Simplify database session management with context managers.
- 🔧 **Easy Configuration**: Configure your database effortlessly with `PostgresConfig`.## 📦 Installation
Get started quickly by installing `asyncpg-client` with pip:
```sh
pip install git+https://github.com/deepmancer/asyncpg-client.git
```## 📝 Usage Guide
### 🔧 Configuration
Start by creating a configuration object with `PostgresConfig`:
```python
from asyncpg_client import PostgresConfigconfig = PostgresConfig(
host='localhost',
port=5432,
user='your_user',
password='your_password',
database='your_database',
url=None, # Optional: Direct database URL
enable_db_echo_log=False,
enable_db_expire_on_commit=False
)
```### 🏗️ Creating an AsyncPostgres Instance
Next, create an instance of `AsyncPostgres` using your configuration:
```python
from asyncpg_client import AsyncPostgresasync def main():
pg_client = await AsyncPostgres.create(config=config)
print(pg_client.async_url)
print(pg_client.sync_url)
```### ⚙️ Managing Database Sessions
Interact with your PostgreSQL database using the context manager from `get_or_create_session`:
```python
from asyncpg_client import AsyncPostgresasync def main():
pg_client = await AsyncPostgres.create(config=config)async with pg_client.get_or_create_session() as session:
# Interact with your database here
passawait pg_client.disconnect()
```### 🔍 Example Usage
Here's a basic example to demonstrate how `asyncpg-client` works:
```python
import asyncio
from asyncpg_client import AsyncPostgres, PostgresConfigasync def main():
config = PostgresConfig(
host='localhost',
port=5432,
user='your_user',
password='your_password',
database='your_database'
)
pg_client = await AsyncPostgres.create(config=config)async with pg_client.get_or_create_session() as session:
# Perform your database operations here
passawait pg_client.disconnect()
if __name__ == "__main__":
asyncio.run(main())
```### 🛡️ Error Handling
Handle various database-related errors gracefully with custom exceptions:
- `PGConnectionError`
- `PGSessionCreationError`
- `PGEngineInitializationError`### 🛑 Disconnecting
Ensure a clean disconnect from your PostgreSQL database:
```python
await pg_client.disconnect()
```## 📄 License
This project is licensed under the Apache License 2.0. See the [LICENSE](https://github.com/deepmancer/asyncpg-client/blob/main/LICENSE) file for full details.