Skip to main content

asyncpg_connector

asyncpg_connector is a lightweight database connector based on asyncpg, providing simple configuration and context management for seamless asynchronous interaction with PostgreSQL databases.

點此閱讀中文版

Features

  • Validates connection configuration using Pydantic.
  • Supports context management (async with) for automatic connection handling.
  • Built-in error handling with clear error messages.

Installation

Install using pip:

pip install asyncpg-connector

Or with Poetry:

poetry add asyncpg-connector

Usage

Here is a simple example:

import asyncio
from asyncpg_connector import AsyncpgConnector
from asyncpg_connector.model import ConnectorConfig

db_config = ConnectorConfig(
    host="localhost",
    port=5432,
    user="your_username",
    password="your_password",
    database="your_database",
    ssl=False
)

db_connector = AsyncpgConnector(config)

async def main():
    async with db_connector as connector:
        conn = connector.conn
        result = await conn.fetch("SELECT * FROM your_table")
        print(result)

asyncio.run(main())

Error Handling

When the database connection fails, AsyncpgConnector raises a DatabaseConnectionError:

from asyncpg_connector.error import DatabaseConnectionError

try:
    async with AsyncpgConnector(config) as connector:
        pass
except DatabaseConnectionError as e:
    print(f"Connection failed: {e}")

Requirements

  • Python version:>=3.9,<4.0
  • Dependencies:
    • asyncpg ^0.30.0
    • Pydantic ^2.11.3

Contributing

Contributions are welcome! Please ensure your code adheres to the project's style guidelines and includes relevant tests.

License

This project is licensed under the MIT License.

asyncpg_connector (中文版)

asyncpg_connector 是一個基於 asyncpg 的輕量級資料庫連接器,提供簡單的配置和上下文管理功能,讓您能夠輕鬆地與 PostgreSQL 資料庫進行非同步操作。

功能特性

  • 使用 Pydantic 驗證連接配置。
  • 支援上下文管理協議 (async with),自動管理連接的建立與關閉。
  • 內建錯誤處理,提供清晰的錯誤訊息。

安裝

您可以使用 pip 安裝:

pip install asyncpg-connector

或使用 Poetry:

poetry add asyncpg-connector

使用方式

以下是一個簡單的使用範例:

import asyncio
from asyncpg_connector import AsyncpgConnector
from asyncpg_connector.model import ConnectorConfig

db_config = ConnectorConfig(
    host="localhost",
    port=5432,
    user="your_username",
    password="your_password",
    database="your_database",
    ssl=False
)

db_connector = AsyncpgConnector(config)

async def main():
    async with db_connector as connector:
        conn = connector.conn
        result = await conn.fetch("SELECT * FROM your_table")
        print(result)

asyncio.run(main())

錯誤處理

當資料庫連接失敗時,AsyncpgConnector 會拋出 DatabaseConnectionError:

from asyncpg_connector.error import DatabaseConnectionError

try:
    async with AsyncpgConnector(config) as connector:
        pass
except DatabaseConnectionError as e:
    print(f"Connection failed: {e}")

系統需求

  • Python 版本:>=3.9,<4.0
  • 依賴套件:
    • asyncpg ^0.30.0
    • Pydantic ^2.11.3

貢獻

歡迎提交問題或拉取請求!請確保您的代碼符合專案的風格指南,並附上相關的測試。

授權

此專案採用 MIT License 授權。

Metadata

Release files for asyncpg-connector 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncpg-connector 0.1.2
File Size Uploaded Provenance
asyncpg_connector-0.1.2.tar.gz 3.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncpg-connector 0.1.2
File Interpreter ABI Platform
asyncpg_connector-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.7 kB

Release files / asyncpg_connector-0.1.2.tar.gz

Download URL asyncpg_connector-0.1.2.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
364ebb1ca43963ec2942b06c236ebd8c6a73a63bf77fc63ffd55a48247a01a9e
BLAKE2b-256 checksum
How to use checksums
08a88147e1a8f28e27b58a970aa2bc98b7e88001283bdb5056a32844fa5ff8d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

Release files / asyncpg_connector-0.1.2-py3-none-any.whl

Download URL asyncpg_connector-0.1.2-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
559dba4d649efc0d2ee66c9937a3deb091b171ccebe829b473b5ef38b7c0fd6b
BLAKE2b-256 checksum
How to use checksums
e68afcd65e7092c6f6d173ef115360c91d464eb2a60136123c8e61cbbe431f92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.12.3 Darwin/23.1.0

Security for asyncpg-connector 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page