# sqlcrudbase (Python Library)
A lightweight, flexible, and reusable Python library designed to simplify the creation of CRUD (Create, Read, Update, Delete) operations in FastAPI applications. It leverages **Pydantic** for request and response validation and **Peewee** as the ORM for managing database interactions.
## Features
- **Pre-built CRUD Operations**: Simplifies the process of setting up standard CRUD routes.
- **FastAPI Integration**: Easily create FastAPI routes with minimal code.
- **Pydantic & Peewee**: Handles data validation with Pydantic and works with Peewee for database operations.
- **Modular Structure**: Decouples controller, service, and repository layers for better maintainability.
## Installation
Install the package directly from PyPI using pip:
```bash
pip install sqlcrudbase
```
## Usage
### Setting up a Controller
The `BaseController` provides a set of standard CRUD operations for your FastAPI application. Here’s how you can use it:
```python
from my_models import MyPydanticModel, MyPeeweeModel
from sqlcrudbase import BaseController, BaseService
# Create a service
service = BaseService(MyPeeweeModel)
# Create a controller
controller = BaseController(MyPydanticModel, MyPeeweeModel, service)
# Add routes to your FastAPI app
app = FastAPI()
app.include_router(controller.get_router(), prefix="/items")
```
### Defining the Models
You need to define your **Pydantic** model (for validation) and your **Peewee** model (for database interactions). For example:
```python
from peewee import Model, CharField, IntegerField
from pydantic import BaseModel
class MyPeeweeModel(Model):
name = CharField()
quantity = IntegerField()
class MyPydanticModel(BaseModel):
name: str
quantity: int
```
### Example Routes
- **Create an Item**: `POST /items`
- **Get All Items**: `GET /items`
- **Get Item by ID**: `GET /items/{id}`
- **Update Item by ID**: `PUT /items/{id}`
- **Delete Item by ID**: `DELETE /items/{id}`
## How it Works
### BaseController
The `BaseController` class defines the FastAPI routes and links them to the appropriate service methods:
- `POST /`: Create a new item.
- `GET /`: Retrieve all items.
- `GET /{id}`: Retrieve an item by its ID.
- `PUT /{id}`: Update an existing item by its ID.
- `DELETE /{id}`: Delete an item by its ID.
### BaseService
The `BaseService` contains the business logic and interacts with the repository for database operations. It includes methods for:
- Retrieving all records.
- Retrieving a record by ID.
- Creating a new record.
- Updating an existing record.
- Deleting a record by ID.
### BaseRepository
The `BaseRepository` interacts directly with the database using Peewee. It provides methods for:
- Retrieving all records.
- Retrieving a record by ID.
- Creating a new record.
- Deleting a record by ID.
## Extending the Library
You can easily extend this library by adding more specific logic to the service or overriding any of the CRUD methods.
For example, to add custom validation or processing logic before creating a model:
```python
class CustomService(BaseService):
def create(self, model: dict):
# Custom validation logic here
return super().create(model)
```
## Contributing
Contributions are welcome! Feel free to open issues or submit pull requests to improve this library.
### References
- **Peewee (ORM for Python)**
GitHub: [https://github.com/coleifer/peewee](https://github.com/coleifer/peewee)
Documentation: [http://docs.peewee-orm.com/](http://docs.peewee-orm.com/)
- **Pydantic (Data validation and settings management)**
GitHub: [https://github.com/pydantic/pydantic](https://github.com/pydantic/pydantic)
Documentation: [https://docs.pydantic.dev/](https://docs.pydantic.dev/)
## License
This project is licensed under the [MIT LICENSE](https://github.com/N3VERS4YDIE/sqlcrudbase-py-lib/blob/main/LICENSE).
Raw data
{
"_id": null,
"home_page": "https://github.com/N3VERS4YDIE/sqlcrudbase-py-lib",
"name": "sqlcrudbase",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "sql crud base peewee fastapi pydantic",
"author": "Santiago Palacio V\u00e1squez",
"author_email": "keycode.santiago@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/38/66/f0358b55b5c89b1b4b1895a3fbfef39173ce3ca290fcdfef1fb19d8e9dc5/sqlcrudbase-0.1.2.tar.gz",
"platform": null,
"description": "# sqlcrudbase (Python Library)\n\nA lightweight, flexible, and reusable Python library designed to simplify the creation of CRUD (Create, Read, Update, Delete) operations in FastAPI applications. It leverages **Pydantic** for request and response validation and **Peewee** as the ORM for managing database interactions.\n\n## Features\n\n- **Pre-built CRUD Operations**: Simplifies the process of setting up standard CRUD routes.\n- **FastAPI Integration**: Easily create FastAPI routes with minimal code.\n- **Pydantic & Peewee**: Handles data validation with Pydantic and works with Peewee for database operations.\n- **Modular Structure**: Decouples controller, service, and repository layers for better maintainability.\n\n## Installation\n\nInstall the package directly from PyPI using pip:\n\n```bash\npip install sqlcrudbase\n```\n\n## Usage\n\n### Setting up a Controller\n\nThe `BaseController` provides a set of standard CRUD operations for your FastAPI application. Here\u2019s how you can use it:\n\n```python\nfrom my_models import MyPydanticModel, MyPeeweeModel\nfrom sqlcrudbase import BaseController, BaseService\n\n# Create a service\nservice = BaseService(MyPeeweeModel)\n\n# Create a controller\ncontroller = BaseController(MyPydanticModel, MyPeeweeModel, service)\n\n# Add routes to your FastAPI app\napp = FastAPI()\napp.include_router(controller.get_router(), prefix=\"/items\")\n```\n\n### Defining the Models\n\nYou need to define your **Pydantic** model (for validation) and your **Peewee** model (for database interactions). For example:\n\n```python\nfrom peewee import Model, CharField, IntegerField\nfrom pydantic import BaseModel\n\nclass MyPeeweeModel(Model):\n name = CharField()\n quantity = IntegerField()\n\nclass MyPydanticModel(BaseModel):\n name: str\n quantity: int\n```\n\n### Example Routes\n\n- **Create an Item**: `POST /items`\n- **Get All Items**: `GET /items`\n- **Get Item by ID**: `GET /items/{id}`\n- **Update Item by ID**: `PUT /items/{id}`\n- **Delete Item by ID**: `DELETE /items/{id}`\n\n## How it Works\n\n### BaseController\n\nThe `BaseController` class defines the FastAPI routes and links them to the appropriate service methods:\n\n- `POST /`: Create a new item.\n- `GET /`: Retrieve all items.\n- `GET /{id}`: Retrieve an item by its ID.\n- `PUT /{id}`: Update an existing item by its ID.\n- `DELETE /{id}`: Delete an item by its ID.\n\n### BaseService\n\nThe `BaseService` contains the business logic and interacts with the repository for database operations. It includes methods for:\n\n- Retrieving all records.\n- Retrieving a record by ID.\n- Creating a new record.\n- Updating an existing record.\n- Deleting a record by ID.\n\n### BaseRepository\n\nThe `BaseRepository` interacts directly with the database using Peewee. It provides methods for:\n\n- Retrieving all records.\n- Retrieving a record by ID.\n- Creating a new record.\n- Deleting a record by ID.\n\n## Extending the Library\n\nYou can easily extend this library by adding more specific logic to the service or overriding any of the CRUD methods.\n\nFor example, to add custom validation or processing logic before creating a model:\n\n```python\nclass CustomService(BaseService):\n def create(self, model: dict):\n # Custom validation logic here\n return super().create(model)\n```\n\n## Contributing\n\nContributions are welcome! Feel free to open issues or submit pull requests to improve this library.\n\n### References\n\n- **Peewee (ORM for Python)** \n GitHub: [https://github.com/coleifer/peewee](https://github.com/coleifer/peewee) \n Documentation: [http://docs.peewee-orm.com/](http://docs.peewee-orm.com/)\n\n- **Pydantic (Data validation and settings management)** \n GitHub: [https://github.com/pydantic/pydantic](https://github.com/pydantic/pydantic) \n Documentation: [https://docs.pydantic.dev/](https://docs.pydantic.dev/)\n\n## License\n\nThis project is licensed under the [MIT LICENSE](https://github.com/N3VERS4YDIE/sqlcrudbase-py-lib/blob/main/LICENSE).\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python library to simplify the creation of CRUD operations in FastAPI applications.",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/N3VERS4YDIE/sqlcrudbase-py-lib"
},
"split_keywords": [
"sql",
"crud",
"base",
"peewee",
"fastapi",
"pydantic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "8f4b0fdab8e9548b1f8babd133b77e8f0e2ee1f2709a5516eed899fe60711c95",
"md5": "64ad6742a9da57cac6bc9692ed9bb997",
"sha256": "88c8b9c085cc6c47bcbfdb86a49e756028fed88b92d8f4d85c93be2d329d9c11"
},
"downloads": -1,
"filename": "sqlcrudbase-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "64ad6742a9da57cac6bc9692ed9bb997",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5715,
"upload_time": "2024-10-11T10:58:19",
"upload_time_iso_8601": "2024-10-11T10:58:19.340415Z",
"url": "https://files.pythonhosted.org/packages/8f/4b/0fdab8e9548b1f8babd133b77e8f0e2ee1f2709a5516eed899fe60711c95/sqlcrudbase-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3866f0358b55b5c89b1b4b1895a3fbfef39173ce3ca290fcdfef1fb19d8e9dc5",
"md5": "60fa1699f71e1bac47f677cd5193a26e",
"sha256": "45fbcceee5442955463702fc9f90bec8ab34b634e5356b526d12a74b5bddfd0f"
},
"downloads": -1,
"filename": "sqlcrudbase-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "60fa1699f71e1bac47f677cd5193a26e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4504,
"upload_time": "2024-10-11T10:58:20",
"upload_time_iso_8601": "2024-10-11T10:58:20.149447Z",
"url": "https://files.pythonhosted.org/packages/38/66/f0358b55b5c89b1b4b1895a3fbfef39173ce3ca290fcdfef1fb19d8e9dc5/sqlcrudbase-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-11 10:58:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "N3VERS4YDIE",
"github_project": "sqlcrudbase-py-lib",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "annotated-types",
"specs": [
[
"==",
"0.7.0"
]
]
},
{
"name": "anyio",
"specs": [
[
"==",
"4.6.0"
]
]
},
{
"name": "exceptiongroup",
"specs": [
[
"==",
"1.2.2"
]
]
},
{
"name": "fastapi",
"specs": [
[
"==",
"0.115.0"
]
]
},
{
"name": "idna",
"specs": [
[
"==",
"3.10"
]
]
},
{
"name": "peewee",
"specs": [
[
"==",
"3.17.6"
]
]
},
{
"name": "pydantic",
"specs": [
[
"==",
"2.9.2"
]
]
},
{
"name": "pydantic_core",
"specs": [
[
"==",
"2.23.4"
]
]
},
{
"name": "sniffio",
"specs": [
[
"==",
"1.3.1"
]
]
},
{
"name": "starlette",
"specs": [
[
"==",
"0.38.6"
]
]
},
{
"name": "typing_extensions",
"specs": [
[
"==",
"4.12.2"
]
]
}
],
"lcname": "sqlcrudbase"
}