{"id":17254320,"url":"https://github.com/woofz/sqlmodel-basecrud","last_synced_at":"2025-10-30T04:03:23.558Z","repository":{"id":62591844,"uuid":"497462897","full_name":"woofz/sqlmodel-basecrud","owner":"woofz","description":"Simple package that provides base CRUD operations for your models.","archived":false,"fork":false,"pushed_at":"2022-06-07T10:47:34.000Z","size":635,"stargazers_count":47,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-18T12:50:46.832Z","etag":null,"topics":["api","fastapi","fastapi-sqlmodel","python","python3","sql","sqlalchemy","sqlmodel","web"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/woofz.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-29T01:22:04.000Z","updated_at":"2025-05-16T00:41:47.000Z","dependencies_parsed_at":"2022-11-03T22:33:34.360Z","dependency_job_id":null,"html_url":"https://github.com/woofz/sqlmodel-basecrud","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/woofz%2Fsqlmodel-basecrud","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/woofz%2Fsqlmodel-basecrud/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/woofz%2Fsqlmodel-basecrud/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/woofz%2Fsqlmodel-basecrud/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/woofz","download_url":"https://codeload.github.com/woofz/sqlmodel-basecrud/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/woofz%2Fsqlmodel-basecrud/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259184738,"owners_count":22818267,"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","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":["api","fastapi","fastapi-sqlmodel","python","python3","sql","sqlalchemy","sqlmodel","web"],"created_at":"2024-10-15T07:08:23.238Z","updated_at":"2025-10-30T04:03:23.455Z","avatar_url":"https://github.com/woofz.png","language":"Python","readme":"\u003ch2 align=\"center\"\u003eSQLModel BaseCRUD\u003c/h2\u003e\n\n\n\u003cdiv align=\"center\"\u003e\n    \n![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/woofz/sqlmodel-basecrud) [![codecov](https://codecov.io/gh/woofz/sqlmodel-basecrud/branch/main/graph/badge.svg?token=AZW7YBAJBP)](https://codecov.io/gh/woofz/sqlmodel-basecrud) [![PyPI version](https://badge.fury.io/py/sqlmodel-basecrud.svg)](https://badge.fury.io/py/sqlmodel-basecrud) ![PyPI - Python Version](https://img.shields.io/pypi/pyversions/sqlmodel-basecrud)\n\n\n\n_Simple package that provides base CRUD operations for your models._\n\n\u003c/div\u003e\n\n---\n\n_**Documentation:**_ [https://woofz.github.io/sqlmodel-basecrud/latest](https://woofz.github.io/sqlmodel-basecrud/latest)\n\n_**Sources:**_ [https://github.com/woofz/sqlmodel-basecrud](https://github.com/woofz/sqlmodel-basecrud)\n\n---\n\n### What is SQLModel BaseCRUD?\n\nWith SQLModel BaseCRUD, you can implement your CRUD operations easily in your project. It is simple as declaring a variable!  \nThis package consists in two classes: _BaseCRUD_ and _BaseRepository_.  \n**BaseCRUD** is the basic class that implements the basic CRUD operations, while **BaseRepository** is the repository used to execute those operations. You could also write your own repository class and use basic CRUD operation provided by **BaseRepository** class by extending it to your own repository class!\n\n### Installation\n\n##### Using pip\n\n`pip install sqlmodel-basecrud`\n\n##### Using poetry\n\n`poetry add sqlmodel-basecrud`\n\n### Operations\n\n### Usage\n\n##### Basic setup\n\nConsider these two models as example:\n\n```python\nclass Team(SQLModel, table=True):\n    id: Optional[int] = Field(default=None, primary_key=True)\n    name: str = Field(index=True)\n    headquarters: str\n\n    heroes: List[\"Hero\"] = Relationship(back_populates=\"team\")\n    \n    \nclass Hero(SQLModel, table=True):\n    id: Optional[int] = Field(default=None, primary_key=True)\n    name: str = Field(index=True)\n    secret_name: str\n    age: Optional[int] = Field(default=None, index=True)\n\n    team_id: Optional[int] = Field(default=None, foreign_key=\"team.id\")\n    team: Optional[Team] = Relationship(back_populates=\"heroes\")\n```\n\nWe want to perform some operations on these models. First of all we instantiate a _BaseRepository_, specifying the database session and the model that we want to manipulate.\n\n```python\n# other imports..\nfrom sqlmodel_basecrud import BaseRepository\n\nwith Session(engine) as session:\n    hero_repository = BaseRepository(db=session, model=Hero)\n    team_repository = BaseRepository(db=session, model=Team)\n```\n\n##### CREATE operation\n\nPersists an item into the database.\n\n```python\n# CREATE operation\nmy_hero = Hero(name='Github Hero', secret_name='Gitty', age=31)\nhero_repository.create(my_hero)\n# now my_hero is persisting in the database!\n```\n\n##### BULK CREATE operation\nPersists multiple items into the database. You have to pass them in a list.\n```python\n# BULK CREATE operation\nmy_heroes_list = [ Hero(name='Github Hero', secret_name='Gitty', age=31),\n                 Hero(name='Hero 2', secret_name='Hero2', age=21),\n                 Hero(name='Hero 3', secret_name='Hero3', age=29)\n               ]\nhero_repository.bulk_create(my_heroes_list)\n```\n##### GET operation\nGET operation simply gets a single record from the database.\n```python\nresult = hero_repository.get(id=1, name='Github Hero')\n```\nNote that you can also use a BinaryExpression. Here's an example: \n```python\nresult = hero_repository.get(Hero.id \u003e= 3)\n```\n*result* variable will be an instance of Hero, if a result matches the criteria, or None type.\n##### FILTER operation\nGets one or more instances from the database, filtering them by one or more column/s.\n```python\nresults = hero_repository.filter(age=31)\n```\nLike the GET operation, here you can use a BinaryExpression as well. Here's an example:  \n```python\nresult = hero_repository.filter(Hero.age == 31)\n```\n*results*  will be a *List* with zero or more elements.\n\n##### GET ALL operation\n\nGets all instances of given module from the Database\n\n```python\nresults = hero_repository.get_all()\n```\n\n_results_ will be a _List_ with zero or more elements.\n\n##### UPDATE operation\n\nUpdates a record into the database.\n\n```python\ninstance_to_update = hero_repository.get(id=1)\ninstance_to_update.name = 'Super New Name'\ninstance_to_update.age = 27\n\nhero_repository.update(instance_to_update)\n```\n\nThe hero will have his columns *name* and *age* with updated values.\n\n##### DELETE operation\n\nRemoves an instance from the database\n\n```python\ninstance_to_remove = hero_repository.get(id=1)\nhero_repository.delete(instance_to_remove)\n```\n\nThe instance will be removed from the database.\n\n### Custom Repository\n\nIf you want to extend the BaseRepository class with some custom methods, you can write your own repository class. Just extend BaseRepository or BaseCRUD class and call the super class constructor, by passing it two essential parameters:\n\n*   **db**: must be a Session instance;\n*   **model**: must be a Type\\[SQLModel\\].\n\n```python\nfrom sqlmodel_basecrud import BaseRepository\n\n\nclass MyCustomRepository(BaseRepository):\n\n    def __init__(self, db: Session, model: Type[SQLModel]):\n        super().__init__(model=model, db=db)\n```\n\n### What's next\n\nThe first thing that comes to my mind is to extend the features of Async to BaseCRUD class. I will try to enhance the features of the project. Suggestions are appreciated 🤩\n\n### Inspired by\n\n_FastAPI_: framework, high performance, easy to learn, fast to code, ready for production\n\n_SQLModel_, SQL databases in Python, designed for simplicity, compatibility, and robustness.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwoofz%2Fsqlmodel-basecrud","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwoofz%2Fsqlmodel-basecrud","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwoofz%2Fsqlmodel-basecrud/lists"}