{"id":22802578,"url":"https://github.com/code-specialist/sqlmodel-repository","last_synced_at":"2025-09-01T06:39:23.090Z","repository":{"id":105069005,"uuid":"564024088","full_name":"code-specialist/sqlmodel-repository","owner":"code-specialist","description":"Repository pattern implementation for SQLModel (SQLAlchemy). Easily separate business logic from the data access layer","archived":false,"fork":false,"pushed_at":"2023-06-23T15:04:50.000Z","size":225,"stargazers_count":10,"open_issues_count":4,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-08T00:05:46.595Z","etag":null,"topics":["python","repository","repository-pattern","sqlalchemy","sqlmodel"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/code-specialist.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-11-09T20:41:00.000Z","updated_at":"2024-10-08T10:58:53.000Z","dependencies_parsed_at":"2024-01-08T19:25:41.378Z","dependency_job_id":"4aed76f2-fcb2-4122-89ec-eaad9bdda8c0","html_url":"https://github.com/code-specialist/sqlmodel-repository","commit_stats":null,"previous_names":["code-specialist/python-repository"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/code-specialist/sqlmodel-repository","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Fsqlmodel-repository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Fsqlmodel-repository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Fsqlmodel-repository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Fsqlmodel-repository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/code-specialist","download_url":"https://codeload.github.com/code-specialist/sqlmodel-repository/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/code-specialist%2Fsqlmodel-repository/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273082444,"owners_count":25042284,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-01T02:00:09.058Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python","repository","repository-pattern","sqlalchemy","sqlmodel"],"created_at":"2024-12-12T09:06:36.987Z","updated_at":"2025-09-01T06:39:23.054Z","avatar_url":"https://github.com/code-specialist.png","language":"Python","readme":"# SQLModel Repository - Python Repository Pattern Implementation for SQLModel\n\n[![CodeQL](https://github.com/code-specialist/python-repository/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/code-specialist/python-repository/actions/workflows/github-code-scanning/codeql) [![Tests](https://github.com/code-specialist/python-repository/actions/workflows/test.yaml/badge.svg)](https://github.com/code-specialist/python-repository/actions/workflows/test.yaml)\n\nSQLModel Repository implements the repository pattern and provides simple, robust and reliable CRUD operations for [SQLModels](https://sqlmodel.tiangolo.com/). The repository pattern is a great way to encapsulate the logic of your application and to separate the business logic from the data access layer.\n\nAny contributions are welcome. But we do not accept any pull requests that do not come with tests.\n\n## Installation\n\n```bash\npip install sqlmodel-repository\n```\n\n## Usage\n\n### 1. Create a Repository\n\nTo create a repository you need to inherit from the `Repository` class and pass the entity type as a generic type argument.\n\n```python\nfrom typing import TypeVar\nfrom sqlalchemy.orm import Session\nfrom sqlmodel_repository import SQLModelEntity, Repository\n\nExampleEntity = TypeVar(\"ExampleEntity\", bound=SQLModelEntity)\n\n\nclass AbstractRepository(Repository[ExampleEntity]):\n    \"\"\"Example base class for all repositories\"\"\"\n\n    def get_session(self) -\u003e Session:\n        \"\"\"Provides a session to work with\"\"\"\n        # TODO: Implement a method to provide a session here\n```\n\nIn this example we use a `TypeVar` to pass the generic type downwards. You have to implement the `get_session` method to provide a session to the repository.\n\n### 2. Create Entities and Relationships\n\n```python\nfrom enum import Enum\nfrom sqlmodel import Relationship, Field\nfrom sqlmodel_repository import SQLModelEntity\n\n\nclass PetType(Enum):\n    \"\"\"Enum that describes the type of pet\"\"\"\n\n    DOG = \"dog\"\n    CAT = \"cat\"\n    FISH = \"fish\"\n\n\nclass Pet(SQLModelEntity, table=True):\n    \"\"\"Pet model\"\"\"\n\n    id: int = Field(index=True, default=None, primary_key=True)\n\n    name: str\n    age: int\n    type: PetType\n    shelter_id: int = Field(foreign_key=\"shelter.id\")\n    shelter: \"Shelter\" = Relationship(back_populates=\"pets\")\n\nclass Shelter(SQLModelEntity, table=True):\n    \"\"\"Shelter model\"\"\"\n\n    id: int = Field(index=True, default=None, primary_key=True)\n\n    name: str\n    pets: list[Pet] = Relationship(back_populates=\"shelter\", sa_relationship_kwargs={\"cascade\": \"all, delete-orphan\"})\n```\n\n### 3. Inherit from the Repository\n\nNow you can inherit from your `AbstractRepository` and tell it to manage the a specific entity. e.g. `Pet` and `Shelter`:\n\n```python\nclass PetRepository(AbstractRepository[Pet]):\n    \"\"\"Repository to manage pets\"\"\"\n\nclass ShelterRepository(AbstractRepository[Shelter]):\n    \"\"\"Repository to manage shelters\"\"\"\n```\n\nOptionally, you may pass a `logger` keyword argument to the repository to log the operations. The logger should be a `structlog` logger with enabled `JSONRenderer`. If no logger is provided the repository will use its default logger (`SQLModelRepositoryLogger`).\n\nDone 🚀 You can now use the repository to perform the operations on your entities. e.g.:\n\n```python\nfrom sqlmodel import col\n\n# Create a new shelter\nshelter = ShelterRepository().create(Shelter(name=\"Shelter 1\"))\n\n# Create some pets\nfido = PetRepository().create(Pet(name=\"Fido\", age=3, type=PetType.DOG, shelter_id=1))\nfifi = PetRepository().create(Pet(name=\"Fifi\", age=2, type=PetType.CAT, shelter_id=1))\n\n# Find all pets that belong to the shelter\nPetRepository().find(shelter=shelter)\n```\n\nNo more session passing, no more boilerplate code. Just use the repository to perform the operations on your entities 🎉\n\n## Methods\n\n### Repository\n\nEach `Repository` comes with a set of **typed methods** to perform common CRUD operations on your entities:\n\n- `create`: Create a new record of an entity\n- `create_batch`: Create a batch of records of an entity\n\n______________________________________________________________________\n\n- `get`: Get a single record by its ID\n- `get_batch`: Get all records of an entity that match the given filters\n- `get_batch_by_ids`: Get a batch of records by their IDs\n- `get_all`: Get all records of an entity\n\n______________________________________________________________________\n\n- `update`: Update an entity instance\n- `update_by_id`: Update an entity by its ID\n- `update_batch`: Update a batch of entity instances with the same values\n- `update_batch_by_ids`: Update a batch of entities by their IDs\n\n______________________________________________________________________\n\n- `delete`: Delete an entity instance\n- `delete_by_id`: Delete an entity by its ID\n- `delete_batch`: Delete a batch of entity instances\n- `delete_batch_by_ids`: Delete a batch of entities by their IDs\n\n### BaseRepository\n\nIf you require more flexibility, you may also use the `BaseRepository` which provides more granular operations. The `BaseRepository` provides the following methods:\n\n- `create`: Create a new record of an entity\n- `create_batch`: Create a batch of records of an entity\n- `update`: Update an entity instance\n- `update_batch`: Update a batch of entity instances with the same values\n- `get`: Get a single record by its ID\n- `get_batch`: Get all records of an entity that match the given filters\n- `find`: Find all records of an entity that match the given filters\n- `delete`: Delete an entity instance\n- `delete_batch`: Delete a batch of entity instances\n\n## Examples\n\nFor a more detailed example, check out our [tests/integration/scenarios](tests/integration/scenarios) directory. We do not currently offer a full example application.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-specialist%2Fsqlmodel-repository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-specialist%2Fsqlmodel-repository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-specialist%2Fsqlmodel-repository/lists"}