Skip to main content

A Python library for managing asynchronous SQL Server connections.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_sqlserver_lib-1.0.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_sqlserver_lib-1.0.3-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file async_sqlserver_lib-1.0.3.tar.gz.

File metadata

  • Download URL: async_sqlserver_lib-1.0.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.6

File hashes

Hashes for async_sqlserver_lib-1.0.3.tar.gz
Algorithm Hash digest
SHA256 c1501dfddf69e202e846b929638d55ecb6b9fd48e89fd8214b96a535caf817df
MD5 3639e01935e0cde27619a1880df87c4e
BLAKE2b-256 93e428d8e78949cc812491bf43d198dbf866ab57669e4c93d2eec93ecf72c0af

See more details on using hashes here.

File details

Details for the file async_sqlserver_lib-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for async_sqlserver_lib-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8e8b3a3dcd56cb8bbccaf8b827d796761939ac5fbbf2942388b5a3844a4fee94
MD5 ff639b68236f6b68540c717badf4b896
BLAKE2b-256 5e7358dab90e124994c4c99cecb8723097cab8428a9bc9ea199bf55cb008d3c4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page