Skip to main content

Extension for BlackSheep to use SQLAlchemy.

Project description

Build pypi versions license

BlackSheep-SQLAlchemy

Extension for BlackSheep that simplifies the use of SQLAlchemy in the web framework.

pip install blacksheep-sqlalchemy

Important: this library only supports rodi dependency injection container. However, the implementation can be used for reference to configure other DI containers to work with SQLAlchemy.

How to use

from blacksheep.server import Application
from blacksheepsqlalchemy import use_sqlalchemy

app = Application()

use_sqlalchemy(app, connection_string="<CONNECTION_STRING>")

After registering SQLAlchemy, services are configured in the application, so they are automatically resolved in any request handler requiring a SQLAlchemy db connection or db session; for example:

@get("/api/countries")
async def get_countries(db_connection) -> List[CountryData]:
    """
    Fetches the countries using a database connection.
    """
    result = []
    async with db_connection:
        items = await db_connection.execute(text("SELECT * FROM country"))
        for item in items.fetchall():
            result.append(CountryData(item["id"], item["name"]))
    return result

Services can be injected at any level of the resolution graph, so BlackSheep and rodi support out of the box the scenario of db connections or db sessions referenced in the business logic but not directly by the front-end layer (depending on programmers' preference and their notion of best practices when building web apps).

Services can be injected in the following ways:

By alias By type annotation Value
db_connection AsyncConnection instance of AsyncConnection (scoped to web request)
db_session AsyncSession instance of AsyncSession (scoped to web request)
db_engine AsyncEngine instance of AsyncEngine (singleton)

For example, using SQLite:

  • requires driver: pip install aiosqlite
  • connection string: sqlite+aiosqlite:///example.db

See the tests folder for a working example using database migrations applied with Alembic, and a documented API that offers methods to fetch, create, delete countries objects.


Note

BlackSheep is designed to be used in async way, therefore this library requires the use of an asynchronous driver.

References

Documentation

Please refer to the documentation website.

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

blacksheep_sqlalchemy-0.0.3.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

blacksheep_sqlalchemy-0.0.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file blacksheep_sqlalchemy-0.0.3.tar.gz.

File metadata

  • Download URL: blacksheep_sqlalchemy-0.0.3.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for blacksheep_sqlalchemy-0.0.3.tar.gz
Algorithm Hash digest
SHA256 90a39a7a64a62100c4b610c8afe8ef8d1871724b56566adc055499aa3ce3ff48
MD5 58eb913ca7abe086593e01ce82ae603f
BLAKE2b-256 31ebcae5841695e4a61ffb6263de0dfe8153f2b89705c96780d27d5eaa52969e

See more details on using hashes here.

File details

Details for the file blacksheep_sqlalchemy-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for blacksheep_sqlalchemy-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 965aa9a8de84b779c55c433bfbd0a1653f3728504489da7771d422522e861597
MD5 346073505e21e3d8d5e1abac4cf66b79
BLAKE2b-256 7b3a478ada00330a4c8e4238ec9a0cfcc6eb147d4d91a08e18841739e2b7d4df

See more details on using hashes here.

Supported by

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