Skip to main content

Async SQL Server Library

A lightweight Python library for managing asynchronous connections to SQL Server databases, designed for modern Python applications requiring robust, reusable, and efficient database management.

This library simplifies SQL Server database interactions using SQLAlchemy's async capabilities and aioodbc. It provides decorators for managing connections, commits, and rollbacks, ensuring clean and consistent database handling across your projects.


Installation

Using Pip

To install the library, run:

pip install async-sqlserver-lib

Using Pipenv

If you're using pipenv for dependency management:

pipenv install async-sqlserver-lib

Purpose

Why Use This Library?

  1. Asynchronous Database Operations: Ideal for APIs and high-performance Python applications.
  2. Simplified Connection Management: Provides reusable decorators for creating and closing database connections.
  3. SQLAlchemy and aioodbc Integration: Leverages SQLAlchemy for ORM and query execution, and aioodbc for async database communication.
  4. Ease of Use: Write clean, readable, and maintainable database interaction code with minimal setup.

Using the Library

The library allows you to manage asynchronous SQL Server connections with ease. Follow the steps below to start using it:

Set Up Your Environment Variables

To configure your SQL Server connection, create a .env file in your project root with the following details:

DB_DRIVER=ODBC Driver 17 for SQL Server
DB_USER=your_username
DB_PASS=your_password
DB_HOST=localhost
DB_PORT=1433
DB_NAME=your_database

Use the Library in Your Code

Here’s an example of fetching data from a SQL Server database using the library:

import asyncio
from async_sqlserver_lib.decorators import create_db_connection, close_db_connection
from sqlalchemy import text

@create_db_connection(
    driver="ODBC Driver 17 for SQL Server",
    user="your_username",
    password="your_password",
    host="localhost",
    port=1433,
    database="your_database",
)
@close_db_connection
async def fetch_data(db_manager=None):
    """
    Fetch data from the SQL Server database.
    """
    async with db_manager.get_session() as session:
        query = text("SELECT TOP 5 * FROM your_table")  # Replace `your_table` with your table name
        result = await session.execute(query)
        return result.fetchall()

# Run your asynchronous function
async def main():
    data = await fetch_data()
    print("Fetched data:", data)

asyncio.run(main())

Running Tests

Prerequisites

Install testing dependencies:

pip install pytest pytest-asyncio

Run the Test Suite

Execute all tests with:

pytest

The test suite validates:

  1. Connection management.
  2. Decorator functionality.
  3. Query execution.

Contact and Support

For questions, issues, or contributions, please open an issue or pull request in the GitHub repository.


Acknowledgments

This library builds upon the powerful features of:

  • SQLAlchemy for its async database management capabilities.
  • aioodbc for asynchronous ODBC driver integration.

Metadata

Release files for async-sqlserver-lib 1.0.3

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

Source distribution (sdist)

Source distribution for async-sqlserver-lib 1.0.3
File Size Uploaded
async_sqlserver_lib-1.0.3.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async-sqlserver-lib 1.0.3
File Interpreter ABI Platform
async_sqlserver_lib-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / async_sqlserver_lib-1.0.3.tar.gz

Download URL async_sqlserver_lib-1.0.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c1501dfddf69e202e846b929638d55ecb6b9fd48e89fd8214b96a535caf817df
BLAKE2b-256 checksum
How to use checksums
93e428d8e78949cc812491bf43d198dbf866ab57669e4c93d2eec93ecf72c0af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release files / async_sqlserver_lib-1.0.3-py3-none-any.whl

Download URL async_sqlserver_lib-1.0.3-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e8b3a3dcd56cb8bbccaf8b827d796761939ac5fbbf2942388b5a3844a4fee94
BLAKE2b-256 checksum
How to use checksums
5e7358dab90e124994c4c99cecb8723097cab8428a9bc9ea199bf55cb008d3c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.6

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

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