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.

Here are the official reference repositories for Peewee and Pydantic:

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: sqlcrudbase-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 9b1bd7804605e38f17130e43087706e7aad23c88098d482f95da0573d6745d07
MD5 449cde92da9defa175459faf5f183340
BLAKE2b-256 7f31b1a73a39b3b572a288033cffd8983c5ad61159fd99871a12b125aeee901e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sqlcrudbase-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8711b359f35988046b89742e5a9e1ebec7f6a39ca78c32e8c968eb350697e5b1
MD5 4a9fe59e57534ee72c987750e7d927cc
BLAKE2b-256 7dc067cde91f7708d982fee328deb0f2b4a6926d5d516d4a1a2b1b90f28cddc2

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