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.1.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.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlcrudbase-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 93219f25888fa2bdddf55ba65feaba3c6ac2f394fd7d0917f3de81313a15b61d
MD5 48362eac5d1112637e97789cb339806a
BLAKE2b-256 61ed8949f1c3cdcb5379bcad0ec47f34865e39bd13d825f551001ee5032bec1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcrudbase-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e996a7a3f100507fdf455b477a1a94f26f77b39dff988b5128d95ca0548eed9c
MD5 241fc11a645e74373112d77a99c0cd75
BLAKE2b-256 597701183420b96c5a01d0c04f31647f1207230034f44753de4886b87b9e64b5

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