sqla-utils


Namesqla-utils JSON
Version 0.6.1 PyPI version JSON
download
home_pagehttps://github.com/srittau/sqla-utils
SummaryOpinionated utilities for working with SQLAlchemy
upload_time2024-05-06 11:17:59
maintainerNone
docs_urlNone
authorSebastian Rittau
requires_python<4,>=3.8
licenseMIT
keywords sqlalchemy orm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sqla-utils

Opinionated utilities for working with SQLAlchemy

[![MIT License](https://img.shields.io/pypi/l/sqla-utils.svg)](https://pypi.python.org/pypi/sqla-utils/)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqla-utils)](https://pypi.python.org/pypi/sqla-utils/)
[![GitHub](https://img.shields.io/github/release/srittau/sqla-utils/all.svg)](https://github.com/srittau/sqla-utils/releases/)
[![pypi](https://img.shields.io/pypi/v/sqla-utils.svg)](https://pypi.python.org/pypi/sqla-utils/)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/srittau/sqla-utils/test-and-lint.yml?branch=main)](https://github.com/srittau/sqla-utils/actions/workflows/test-and-lint.yml)

## Contents

### Transaction Wrapper

**FIXME**

### `DBObjectBase`

`DBObjectBase` is a base class for mapped classes.

Example:

```python
from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String
from sqla_utils import DBObjectBase, Transaction

class DBAppointment(DBObjectBase):
    __tablename__ = "appointments"

    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False)
    description = Column(String(1000), nullable=False, default="")
```

Appointment items can then be queried like this:

```python
from sqla_utils import begin_transaction

with begin_transaction() as t:
    app123 = DBAppointment.fetch_by_id(t, 123)
    great_apps = DBAppointment.fetch_all(t, DBAppointment.description.like("%great%"))
```

It is recommended to add custom query, creation, and update methods:

```python
class DBAppointment(DBObjectBase):
    ...

    @classmethod
    def create(cls, t: Transaction, date: datetime, description: str) -> DBAppointment:
        o = cls()
        o.date = date
        o.description = description
        t.add(o)
        return o

    @classmethod
    def fetch_all_after(cls, t: Transaction, date: datetime) -> List[DBAppointment]:
        return cls.fetch_all(t, cls.start >= dates.start)

    def update_description(self, t: Transaction, new_description: str) -> None:
        self.description = new_description
        t.changed(self)
```

### Database Builder

**FIXME**

### pytest Utilities

The `sqla_utils.test` module contains a few utilities for working with pytest and SQLAlchemy.

**FIXME**

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/srittau/sqla-utils",
    "name": "sqla-utils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "sqlalchemy, orm",
    "author": "Sebastian Rittau",
    "author_email": "srittau@rittau.biz",
    "download_url": "https://files.pythonhosted.org/packages/fe/8e/9f660c038454cb667689c896d731cd24e6e5c106f405ff7cf4663759efb8/sqla_utils-0.6.1.tar.gz",
    "platform": null,
    "description": "# sqla-utils\n\nOpinionated utilities for working with SQLAlchemy\n\n[![MIT License](https://img.shields.io/pypi/l/sqla-utils.svg)](https://pypi.python.org/pypi/sqla-utils/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqla-utils)](https://pypi.python.org/pypi/sqla-utils/)\n[![GitHub](https://img.shields.io/github/release/srittau/sqla-utils/all.svg)](https://github.com/srittau/sqla-utils/releases/)\n[![pypi](https://img.shields.io/pypi/v/sqla-utils.svg)](https://pypi.python.org/pypi/sqla-utils/)\n[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/srittau/sqla-utils/test-and-lint.yml?branch=main)](https://github.com/srittau/sqla-utils/actions/workflows/test-and-lint.yml)\n\n## Contents\n\n### Transaction Wrapper\n\n**FIXME**\n\n### `DBObjectBase`\n\n`DBObjectBase` is a base class for mapped classes.\n\nExample:\n\n```python\nfrom datetime import datetime\nfrom sqlalchemy import Column, DateTime, Integer, String\nfrom sqla_utils import DBObjectBase, Transaction\n\nclass DBAppointment(DBObjectBase):\n    __tablename__ = \"appointments\"\n\n    id = Column(Integer, primary_key=True)\n    date = Column(DateTime, nullable=False)\n    description = Column(String(1000), nullable=False, default=\"\")\n```\n\nAppointment items can then be queried like this:\n\n```python\nfrom sqla_utils import begin_transaction\n\nwith begin_transaction() as t:\n    app123 = DBAppointment.fetch_by_id(t, 123)\n    great_apps = DBAppointment.fetch_all(t, DBAppointment.description.like(\"%great%\"))\n```\n\nIt is recommended to add custom query, creation, and update methods:\n\n```python\nclass DBAppointment(DBObjectBase):\n    ...\n\n    @classmethod\n    def create(cls, t: Transaction, date: datetime, description: str) -> DBAppointment:\n        o = cls()\n        o.date = date\n        o.description = description\n        t.add(o)\n        return o\n\n    @classmethod\n    def fetch_all_after(cls, t: Transaction, date: datetime) -> List[DBAppointment]:\n        return cls.fetch_all(t, cls.start >= dates.start)\n\n    def update_description(self, t: Transaction, new_description: str) -> None:\n        self.description = new_description\n        t.changed(self)\n```\n\n### Database Builder\n\n**FIXME**\n\n### pytest Utilities\n\nThe `sqla_utils.test` module contains a few utilities for working with pytest and SQLAlchemy.\n\n**FIXME**\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Opinionated utilities for working with SQLAlchemy",
    "version": "0.6.1",
    "project_urls": {
        "Homepage": "https://github.com/srittau/sqla-utils",
        "Repository": "https://github.com/srittau/sqla-utils"
    },
    "split_keywords": [
        "sqlalchemy",
        " orm"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "75bbb52d3a5e4c7a12f149b6b309ca57d62982b607e587ff6ffb5d508952775c",
                "md5": "8cea3785f714ca3b90a591ccabd18ca4",
                "sha256": "cb818cfb1e73201900734cd04982fc2ae3117097731c015a3791557e0dc2df19"
            },
            "downloads": -1,
            "filename": "sqla_utils-0.6.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8cea3785f714ca3b90a591ccabd18ca4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 14600,
            "upload_time": "2024-05-06T11:17:58",
            "upload_time_iso_8601": "2024-05-06T11:17:58.009881Z",
            "url": "https://files.pythonhosted.org/packages/75/bb/b52d3a5e4c7a12f149b6b309ca57d62982b607e587ff6ffb5d508952775c/sqla_utils-0.6.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe8e9f660c038454cb667689c896d731cd24e6e5c106f405ff7cf4663759efb8",
                "md5": "e5f42d3c3c6fcbc10c54aee4c6b1e424",
                "sha256": "5b214451141560169e5ce1da45263255708615dce03bdaa18bb29a3529df4c2c"
            },
            "downloads": -1,
            "filename": "sqla_utils-0.6.1.tar.gz",
            "has_sig": false,
            "md5_digest": "e5f42d3c3c6fcbc10c54aee4c6b1e424",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 12538,
            "upload_time": "2024-05-06T11:17:59",
            "upload_time_iso_8601": "2024-05-06T11:17:59.930855Z",
            "url": "https://files.pythonhosted.org/packages/fe/8e/9f660c038454cb667689c896d731cd24e6e5c106f405ff7cf4663759efb8/sqla_utils-0.6.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-06 11:17:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "srittau",
    "github_project": "sqla-utils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqla-utils"
}
        
Elapsed time: 4.26802s