blacksheep-sqlalchemy


Nameblacksheep-sqlalchemy JSON
Version 0.0.3 PyPI version JSON
download
home_page
SummaryExtension for BlackSheep to use SQLAlchemy.
upload_time2023-12-29 11:12:53
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords blacksheep database orm sqlalchemy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/actions/workflows/build.yml/badge.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/actions/workflows/build.yml)
[![pypi](https://img.shields.io/pypi/v/BlackSheep-SQLAlchemy.svg?color=blue)](https://pypi.org/project/BlackSheep-SQLAlchemy/)
[![versions](https://img.shields.io/pypi/pyversions/blacksheep-sqlalchemy.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/)
[![license](https://img.shields.io/github/license/Neoteroi/blacksheep-sqlalchemy.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/blob/main/LICENSE)

# BlackSheep-SQLAlchemy
Extension for [BlackSheep](https://github.com/Neoteroi/BlackSheep) that
simplifies the use of SQLAlchemy in the web framework.

```bash
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

```python
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:

```python

@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](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/blob/main/tests/app.py)
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

* [SQLAlchemy - support for asyncio](https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html)

## Documentation
Please refer to the [documentation website](https://www.neoteroi.dev/blacksheep/).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "blacksheep-sqlalchemy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "blacksheep,database,orm,sqlalchemy",
    "author": "",
    "author_email": "Roberto Prevato <roberto.prevato@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/31/eb/cae5841695e4a61ffb6263de0dfe8153f2b89705c96780d27d5eaa52969e/blacksheep_sqlalchemy-0.0.3.tar.gz",
    "platform": null,
    "description": "[![Build](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/actions/workflows/build.yml/badge.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/actions/workflows/build.yml)\n[![pypi](https://img.shields.io/pypi/v/BlackSheep-SQLAlchemy.svg?color=blue)](https://pypi.org/project/BlackSheep-SQLAlchemy/)\n[![versions](https://img.shields.io/pypi/pyversions/blacksheep-sqlalchemy.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/)\n[![license](https://img.shields.io/github/license/Neoteroi/blacksheep-sqlalchemy.svg)](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/blob/main/LICENSE)\n\n# BlackSheep-SQLAlchemy\nExtension for [BlackSheep](https://github.com/Neoteroi/BlackSheep) that\nsimplifies the use of SQLAlchemy in the web framework.\n\n```bash\npip install blacksheep-sqlalchemy\n```\n\n**Important:** this library only supports `rodi` dependency injection\ncontainer. However, the implementation can be used for reference to configure\nother DI containers to work with SQLAlchemy.\n\n## How to use\n\n```python\nfrom blacksheep.server import Application\nfrom blacksheepsqlalchemy import use_sqlalchemy\n\napp = Application()\n\nuse_sqlalchemy(app, connection_string=\"<CONNECTION_STRING>\")\n\n```\n\nAfter registering SQLAlchemy, services are configured in the application, so\nthey are automatically resolved in any request handler requiring a SQLAlchemy\ndb connection or db session; for example:\n\n```python\n\n@get(\"/api/countries\")\nasync def get_countries(db_connection) -> List[CountryData]:\n    \"\"\"\n    Fetches the countries using a database connection.\n    \"\"\"\n    result = []\n    async with db_connection:\n        items = await db_connection.execute(text(\"SELECT * FROM country\"))\n        for item in items.fetchall():\n            result.append(CountryData(item[\"id\"], item[\"name\"]))\n    return result\n\n```\n\nServices can be injected at any level of the resolution graph, so `BlackSheep`\nand `rodi` support out of the box the scenario of db connections or db sessions\nreferenced in the business logic but not directly by the front-end layer\n(depending on programmers' preference and their notion of best practices when\nbuilding web apps).\n\nServices can be injected in the following ways:\n\n| By alias      | By type annotation | Value                                               |\n| ------------- | ------------------ | --------------------------------------------------- |\n| db_connection | AsyncConnection    | instance of AsyncConnection (scoped to web request) |\n| db_session    | AsyncSession       | instance of AsyncSession (scoped to web request)    |\n| db_engine     | AsyncEngine        | instance of AsyncEngine (singleton)                 |\n\n---\n\nFor example, using SQLite:\n\n* requires driver: `pip install aiosqlite`\n* connection string: `sqlite+aiosqlite:///example.db`\n\nSee the `tests` folder for a [working example](https://github.com/Neoteroi/BlackSheep-SQLAlchemy/blob/main/tests/app.py)\nusing database migrations applied with `Alembic`, and a documented API that offers methods to fetch, create,\ndelete countries objects.\n\n---\n\n### Note\nBlackSheep is designed to be used in `async` way, therefore this library\nrequires the use of an asynchronous driver.\n\n## References\n\n* [SQLAlchemy - support for asyncio](https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html)\n\n## Documentation\nPlease refer to the [documentation website](https://www.neoteroi.dev/blacksheep/).\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Extension for BlackSheep to use SQLAlchemy.",
    "version": "0.0.3",
    "project_urls": {
        "Bug Tracker": "https://github.com/Neoteroi/BlackSheep-SQLAlchemy/issues",
        "Homepage": "https://github.com/Neoteroi/BlackSheep-SQLAlchemy"
    },
    "split_keywords": [
        "blacksheep",
        "database",
        "orm",
        "sqlalchemy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b3a478ada00330a4c8e4238ec9a0cfcc6eb147d4d91a08e18841739e2b7d4df",
                "md5": "346073505e21e3d8d5e1abac4cf66b79",
                "sha256": "965aa9a8de84b779c55c433bfbd0a1653f3728504489da7771d422522e861597"
            },
            "downloads": -1,
            "filename": "blacksheep_sqlalchemy-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "346073505e21e3d8d5e1abac4cf66b79",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 4343,
            "upload_time": "2023-12-29T11:12:51",
            "upload_time_iso_8601": "2023-12-29T11:12:51.619198Z",
            "url": "https://files.pythonhosted.org/packages/7b/3a/478ada00330a4c8e4238ec9a0cfcc6eb147d4d91a08e18841739e2b7d4df/blacksheep_sqlalchemy-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "31ebcae5841695e4a61ffb6263de0dfe8153f2b89705c96780d27d5eaa52969e",
                "md5": "58eb913ca7abe086593e01ce82ae603f",
                "sha256": "90a39a7a64a62100c4b610c8afe8ef8d1871724b56566adc055499aa3ce3ff48"
            },
            "downloads": -1,
            "filename": "blacksheep_sqlalchemy-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "58eb913ca7abe086593e01ce82ae603f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 4493,
            "upload_time": "2023-12-29T11:12:53",
            "upload_time_iso_8601": "2023-12-29T11:12:53.121198Z",
            "url": "https://files.pythonhosted.org/packages/31/eb/cae5841695e4a61ffb6263de0dfe8153f2b89705c96780d27d5eaa52969e/blacksheep_sqlalchemy-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-29 11:12:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Neoteroi",
    "github_project": "BlackSheep-SQLAlchemy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "blacksheep-sqlalchemy"
}
        
Elapsed time: 0.15443s