| Name | dtpydb JSON |
| Version |
0.1.11
JSON |
| download |
| home_page | None |
| Summary | A Python package for managing database connections, sessions, and ORM operations using SQLAlchemy. This package supports both synchronous and asynchronous database interactions with PostgreSQL. |
| upload_time | 2025-03-11 16:12:05 |
| maintainer | None |
| docs_url | None |
| author | Reza Shirazi |
| requires_python | <4.0,>=3.11 |
| license | MIT |
| keywords |
|
| VCS |
|
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# Database Management Package
A Python package for managing database connections, sessions, and ORM operations using SQLAlchemy. This package supports both synchronous and asynchronous database interactions with PostgreSQL.
## Features
- **Database Configuration:** Set database credentials and connection parameters easily.
- **Synchronous and Asynchronous Support:** Provides both sync (`SQLAlchemy`) and async (`SQLAlchemy + asyncpg`) connections.
- **Connection Pooling & Monitoring:** Efficient resource management with connection pooling and active connection tracking.
- **ORM Base Model:** Provides a structured base model with `id`, `created_at`, and `updated_at` fields.
- **Query and Filtering Utilities:** Simplified filtering, searching, and condition-building for complex queries.
- **Upsert Operations:** Supports bulk insert, update, and upsert operations for optimized database interactions.
## Installation
```bash
pip install dtpydb
```
## Usage
### 1. Configure the Database
```python
from dtpydb.database import DatabaseConfig, DatabaseInstance
config = DatabaseConfig()
(
config
.set_db_user("user")
.set_db_password("password")
.set_db_host("localhost")
.set_db_port(5432)
.set_db_name("my_database")
.set_db_ssl(False)
.set_db_pool_size(10)
.set_db_max_overflow(5)
)
db_instance = DatabaseInstance(config)
```
### 2. Creating Tables
```python
db_instance.create_tables()
```
### 3. Using Synchronous Sessions
```python
with db_instance.get_db_cm() as db:
result = db.execute("SELECT * FROM users").fetchall()
print(result)
```
### 4. Using Asynchronous Sessions
```python
async def fetch_data():
async with db_instance.async_get_db_cm() as db:
result = await db.execute("SELECT * FROM users")
print(result.fetchall())
```
### 5. Defining ORM Models
```python
from sqlalchemy import Column, String
from dtpydb.model import ModelBase
class User(db_instance.base, ModelBase):
__tablename__ = "users"
name = Column(String, nullable=False)
```
### 6. Performing Upsert Operations
```python
from dtpydb.utilities import upsert_data
data = [{"id": 1, "name": "John Doe"}]
upsert_data(data, User, db_instance.session_local())
```
## Database Health Check
```python
if db_instance.check_database_health():
print("Database is healthy!")
else:
print("Database connection failed.")
```
Raw data
{
"_id": null,
"home_page": null,
"name": "dtpydb",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": null,
"author": "Reza Shirazi",
"author_email": "reza@datgate.com",
"download_url": "https://files.pythonhosted.org/packages/90/05/f10a6ca0eb50f2b786931a8fbd2cf107b3fd53e31e813fc680808b0d6b0d/dtpydb-0.1.11.tar.gz",
"platform": null,
"description": "# Database Management Package\n\nA Python package for managing database connections, sessions, and ORM operations using SQLAlchemy. This package supports both synchronous and asynchronous database interactions with PostgreSQL.\n\n## Features\n- **Database Configuration:** Set database credentials and connection parameters easily.\n- **Synchronous and Asynchronous Support:** Provides both sync (`SQLAlchemy`) and async (`SQLAlchemy + asyncpg`) connections.\n- **Connection Pooling & Monitoring:** Efficient resource management with connection pooling and active connection tracking.\n- **ORM Base Model:** Provides a structured base model with `id`, `created_at`, and `updated_at` fields.\n- **Query and Filtering Utilities:** Simplified filtering, searching, and condition-building for complex queries.\n- **Upsert Operations:** Supports bulk insert, update, and upsert operations for optimized database interactions.\n\n## Installation\n```bash\npip install dtpydb\n```\n\n## Usage\n\n### 1. Configure the Database\n```python\nfrom dtpydb.database import DatabaseConfig, DatabaseInstance\n\nconfig = DatabaseConfig()\n(\n config\n .set_db_user(\"user\")\n .set_db_password(\"password\")\n .set_db_host(\"localhost\")\n .set_db_port(5432)\n .set_db_name(\"my_database\")\n .set_db_ssl(False)\n .set_db_pool_size(10)\n .set_db_max_overflow(5)\n)\n\ndb_instance = DatabaseInstance(config)\n```\n\n### 2. Creating Tables\n```python\ndb_instance.create_tables()\n```\n\n### 3. Using Synchronous Sessions\n```python\nwith db_instance.get_db_cm() as db:\n result = db.execute(\"SELECT * FROM users\").fetchall()\n print(result)\n```\n\n### 4. Using Asynchronous Sessions\n```python\nasync def fetch_data():\n async with db_instance.async_get_db_cm() as db:\n result = await db.execute(\"SELECT * FROM users\")\n print(result.fetchall())\n```\n\n### 5. Defining ORM Models\n```python\nfrom sqlalchemy import Column, String\nfrom dtpydb.model import ModelBase\n\nclass User(db_instance.base, ModelBase):\n __tablename__ = \"users\"\n name = Column(String, nullable=False)\n```\n\n### 6. Performing Upsert Operations\n\n```python\nfrom dtpydb.utilities import upsert_data\n\ndata = [{\"id\": 1, \"name\": \"John Doe\"}]\nupsert_data(data, User, db_instance.session_local())\n```\n\n## Database Health Check\n```python\nif db_instance.check_database_health():\n print(\"Database is healthy!\")\nelse:\n print(\"Database connection failed.\")\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python package for managing database connections, sessions, and ORM operations using SQLAlchemy. This package supports both synchronous and asynchronous database interactions with PostgreSQL.",
"version": "0.1.11",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "32368fcd295addce1b615d061b4e2099dadd214f838b3196a848dc7c3e76075e",
"md5": "1683fbf0a586f671c3e91e534396ad53",
"sha256": "46ba5f2bf2e2f6e9a333a35d3e91e46b0a0cab503917411a5112290374f3c5fa"
},
"downloads": -1,
"filename": "dtpydb-0.1.11-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1683fbf0a586f671c3e91e534396ad53",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 10599,
"upload_time": "2025-03-11T16:12:04",
"upload_time_iso_8601": "2025-03-11T16:12:04.870319Z",
"url": "https://files.pythonhosted.org/packages/32/36/8fcd295addce1b615d061b4e2099dadd214f838b3196a848dc7c3e76075e/dtpydb-0.1.11-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "9005f10a6ca0eb50f2b786931a8fbd2cf107b3fd53e31e813fc680808b0d6b0d",
"md5": "6f74f1b082bcf07e705207ed45d334a0",
"sha256": "6ec1ec13b7aad0cba1539d0ec9489aed379b784199e63a55c08471790c81beda"
},
"downloads": -1,
"filename": "dtpydb-0.1.11.tar.gz",
"has_sig": false,
"md5_digest": "6f74f1b082bcf07e705207ed45d334a0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 9540,
"upload_time": "2025-03-11T16:12:05",
"upload_time_iso_8601": "2025-03-11T16:12:05.739558Z",
"url": "https://files.pythonhosted.org/packages/90/05/f10a6ca0eb50f2b786931a8fbd2cf107b3fd53e31e813fc680808b0d6b0d/dtpydb-0.1.11.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-03-11 16:12:05",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "dtpydb"
}