sqlmodel-repo


Namesqlmodel-repo JSON
Version 0.0.3 PyPI version JSON
download
home_pageNone
SummaryActive record mixin for SQLModel
upload_time2024-10-04 19:44:21
maintainerNone
docs_urlNone
authorNone
requires_python>3.11.0
licenseNone
keywords sqlmodel sqlalchemy orm active record
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 🚀 SQLModelRepo - A simple CRUD util for SQLModel

`SQLModelRepo` is a simple and powerful repository pattern implementation for managing database interactions using the SQLAlchemy-backed SQLModel ORM. It abstracts common database tasks (CRUD - Create, Read, Update, Delete) 🛠️, making data manipulation and access easier, with support for filtering, pagination, and session management 🎯.

## 🎯 Features

- 🏷️ **Generic Repository**: Perform Create, Read (single or multiple), Update, and Delete (CRUD) operations effortlessly.
- 🔄 **Session Management**: Automate session reuse or creation, ensuring efficient transaction handling.
- 🔍 **Filtering & Pagination**: Easily filter and paginate results.
- ♻️ **Partial Updates**: Update records partially using SQL.

## 📦 Installation

```bash
pip install sqlmodel_repo
```

Ensure you have `sqlmodel` and other dependencies installed.

## 🚀 Usage

### Define your SQLModel Repository 🏗️

Create an SQLModel class representing your database table.

```python
...
from sqlmodel_repo import SQLModelRepo

class User(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    username: str
    email: str
    extra_metadata: dict = Field(sa_column=Column(JSON))
    
users_repo = SQLModelRepo(model=User, db_engine=engine)
```

### Basic Operations 🛠️

#### Create a Record ✍️

You can easily create a new record using `create`.

```python
new_user = users_repo.create(username="john_doe", email="john@example.com")
```

#### Retrieve by ID 🆔

Fetch a record by its ID using `get_by_id`.

```python
user = users_repo.get_by_id(new_user.id)
```

#### Update a Record 🔄

Modify a record and call `save` to persist your changes.

```python
user.email = "john_new@example.com"
users_repo.save(user)
```

Or, perform partial updates directly with `update(id, **kwargs)`:

```python
users_repo.update(user.id, email="updated_email@example.com")
```

#### Delete a Record 🗑️

Easily delete a record by passing the instance to the `delete` method.

```python
users_repo.delete(user)
```

#### Reuse session 🔄
```python
with Session(engine) as session:
    assert users_repo(session).all()
```

### Advanced Querying 🔥

#### Filtering Records 🔎

Use the `filter` method to retrieve records meeting specific criteria.

```python
# Filter by username 'john_doe'
users = users_repo.filter(username="john_doe").all()

# Find usernames starting with 'jo'
users = users_repo.filter(User.username.startswith('jo')).all()
```

#### Querying Inside JSON Fields 🗂️

```python
from sqlalchemy import cast, String

users = users_repo.filter(cast(User.extra_metadata['some_num'], String) == '99').all()
```

#### Paginated Results 📄

Fetch paginated results using `paginate` or `paginate_with_total` to retrieve ordered subsets of data.

```python
# Paginate results, sorted by username in descending order
users_paginated, total_count = users_repo.filter().paginate_with_total(
    offset=0, limit=4, order_by="username", desc=True
)
```


## ⚖️ License

This project is licensed under the MIT License.

---

Enjoy coding and happy querying with `SQLModelRepo`! 🎉

Author is a lazy person, so this README.md was generated by AI.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sqlmodel-repo",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">3.11.0",
    "maintainer_email": null,
    "keywords": "sqlmodel, sqlalchemy, orm, active record",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/b5/6b/ca689fc1a4ac3d1c532f81b3ed72f166db1e2dc2c3f1703a4e5a329d0414/sqlmodel_repo-0.0.3.tar.gz",
    "platform": null,
    "description": "# \ud83d\ude80 SQLModelRepo - A simple CRUD util for SQLModel\n\n`SQLModelRepo` is a simple and powerful repository pattern implementation for managing database interactions using the SQLAlchemy-backed SQLModel ORM. It abstracts common database tasks (CRUD - Create, Read, Update, Delete) \ud83d\udee0\ufe0f, making data manipulation and access easier, with support for filtering, pagination, and session management \ud83c\udfaf.\n\n## \ud83c\udfaf Features\n\n- \ud83c\udff7\ufe0f **Generic Repository**: Perform Create, Read (single or multiple), Update, and Delete (CRUD) operations effortlessly.\n- \ud83d\udd04 **Session Management**: Automate session reuse or creation, ensuring efficient transaction handling.\n- \ud83d\udd0d **Filtering & Pagination**: Easily filter and paginate results.\n- \u267b\ufe0f **Partial Updates**: Update records partially using SQL.\n\n## \ud83d\udce6 Installation\n\n```bash\npip install sqlmodel_repo\n```\n\nEnsure you have `sqlmodel` and other dependencies installed.\n\n## \ud83d\ude80 Usage\n\n### Define your SQLModel Repository \ud83c\udfd7\ufe0f\n\nCreate an SQLModel class representing your database table.\n\n```python\n...\nfrom sqlmodel_repo import SQLModelRepo\n\nclass User(SQLModel, table=True):\n    id: int | None = Field(default=None, primary_key=True)\n    username: str\n    email: str\n    extra_metadata: dict = Field(sa_column=Column(JSON))\n    \nusers_repo = SQLModelRepo(model=User, db_engine=engine)\n```\n\n### Basic Operations \ud83d\udee0\ufe0f\n\n#### Create a Record \u270d\ufe0f\n\nYou can easily create a new record using `create`.\n\n```python\nnew_user = users_repo.create(username=\"john_doe\", email=\"john@example.com\")\n```\n\n#### Retrieve by ID \ud83c\udd94\n\nFetch a record by its ID using `get_by_id`.\n\n```python\nuser = users_repo.get_by_id(new_user.id)\n```\n\n#### Update a Record \ud83d\udd04\n\nModify a record and call `save` to persist your changes.\n\n```python\nuser.email = \"john_new@example.com\"\nusers_repo.save(user)\n```\n\nOr, perform partial updates directly with `update(id, **kwargs)`:\n\n```python\nusers_repo.update(user.id, email=\"updated_email@example.com\")\n```\n\n#### Delete a Record \ud83d\uddd1\ufe0f\n\nEasily delete a record by passing the instance to the `delete` method.\n\n```python\nusers_repo.delete(user)\n```\n\n#### Reuse session \ud83d\udd04\n```python\nwith Session(engine) as session:\n    assert users_repo(session).all()\n```\n\n### Advanced Querying \ud83d\udd25\n\n#### Filtering Records \ud83d\udd0e\n\nUse the `filter` method to retrieve records meeting specific criteria.\n\n```python\n# Filter by username 'john_doe'\nusers = users_repo.filter(username=\"john_doe\").all()\n\n# Find usernames starting with 'jo'\nusers = users_repo.filter(User.username.startswith('jo')).all()\n```\n\n#### Querying Inside JSON Fields \ud83d\uddc2\ufe0f\n\n```python\nfrom sqlalchemy import cast, String\n\nusers = users_repo.filter(cast(User.extra_metadata['some_num'], String) == '99').all()\n```\n\n#### Paginated Results \ud83d\udcc4\n\nFetch paginated results using `paginate` or `paginate_with_total` to retrieve ordered subsets of data.\n\n```python\n# Paginate results, sorted by username in descending order\nusers_paginated, total_count = users_repo.filter().paginate_with_total(\n    offset=0, limit=4, order_by=\"username\", desc=True\n)\n```\n\n\n## \u2696\ufe0f License\n\nThis project is licensed under the MIT License.\n\n---\n\nEnjoy coding and happy querying with `SQLModelRepo`! \ud83c\udf89\n\nAuthor is a lazy person, so this README.md was generated by AI.\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Active record mixin for SQLModel",
    "version": "0.0.3",
    "project_urls": null,
    "split_keywords": [
        "sqlmodel",
        " sqlalchemy",
        " orm",
        " active record"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "12c4ae4e04a30accbe84712165a838bb5d1c58753d7c6f84f16e3dd3544cc167",
                "md5": "3ce49d2310655b952fa0c97ee628e1a4",
                "sha256": "a9f651b9e54addc62192b1d902404ff875aab972909aa14ca825e249afd723e6"
            },
            "downloads": -1,
            "filename": "sqlmodel_repo-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3ce49d2310655b952fa0c97ee628e1a4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">3.11.0",
            "size": 9030,
            "upload_time": "2024-10-04T19:44:19",
            "upload_time_iso_8601": "2024-10-04T19:44:19.739106Z",
            "url": "https://files.pythonhosted.org/packages/12/c4/ae4e04a30accbe84712165a838bb5d1c58753d7c6f84f16e3dd3544cc167/sqlmodel_repo-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b56bca689fc1a4ac3d1c532f81b3ed72f166db1e2dc2c3f1703a4e5a329d0414",
                "md5": "172ab130ca8325bf05315ea8385e5552",
                "sha256": "3bc59c665628563495a9f4d1fc8b453eecba80ac2724f25881aa680a7c2db3d5"
            },
            "downloads": -1,
            "filename": "sqlmodel_repo-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "172ab130ca8325bf05315ea8385e5552",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">3.11.0",
            "size": 8636,
            "upload_time": "2024-10-04T19:44:21",
            "upload_time_iso_8601": "2024-10-04T19:44:21.918925Z",
            "url": "https://files.pythonhosted.org/packages/b5/6b/ca689fc1a4ac3d1c532f81b3ed72f166db1e2dc2c3f1703a4e5a329d0414/sqlmodel_repo-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-04 19:44:21",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "sqlmodel-repo"
}
        
Elapsed time: 0.34680s