Skip to main content

Python library to simplify the creation of CRUD operations in FastAPI applications.

Project description

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:

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:

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:

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:

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

License

This project is licensed under the MIT LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlcrudbase-0.1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlcrudbase-0.1.2-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlcrudbase-0.1.2.tar.gz.

File metadata

  • Download URL: sqlcrudbase-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sqlcrudbase-0.1.2.tar.gz
Algorithm Hash digest
SHA256 45fbcceee5442955463702fc9f90bec8ab34b634e5356b526d12a74b5bddfd0f
MD5 60fa1699f71e1bac47f677cd5193a26e
BLAKE2b-256 3866f0358b55b5c89b1b4b1895a3fbfef39173ce3ca290fcdfef1fb19d8e9dc5

See more details on using hashes here.

File details

Details for the file sqlcrudbase-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: sqlcrudbase-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.20

File hashes

Hashes for sqlcrudbase-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 88c8b9c085cc6c47bcbfdb86a49e756028fed88b92d8f4d85c93be2d329d9c11
MD5 64ad6742a9da57cac6bc9692ed9bb997
BLAKE2b-256 8f4b0fdab8e9548b1f8babd133b77e8f0e2ee1f2709a5516eed899fe60711c95

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page