Skip to main content

A dynamic repository library for SQLModel in Python

Project description

Churros Repository 🍩

A lightweight, Spring JPA-style repository for FastAPI & SQLModel.

FastAPI SQLModel Python License


📖 Overview

Churros Repository is a dynamic and lightweight repository system for FastAPI + SQLModel, inspired by Spring Data JPA. It provides:

  • Automatic CRUD methods (no SQL needed)
  • Dynamic queries (find_by_*) based on method names
  • Full type safety & IDE auto-completion
  • Easy integration with FastAPI & Dependency Injection
  • Zero boilerplate repositories

🚀 Installation

1️⃣ Install Churros Repository

pip install fastapi sqlmodel uvicorn

2️⃣ Create a FastAPI Project

mkdir myproject && cd myproject
touch main.py models.py repository.py

⚙️ Usage

1️⃣ Define Your Model

Create a SQLModel entity in models.py:

from sqlmodel import SQLModel, Field

class User(SQLModel, table=True):
    id: int = Field(default=None, primary_key=True)
    name: str
    email: str

2️⃣ Setup Database & Repository

from sqlmodel import create_engine, Session
from churros import ChurrosRepository
from models import User

DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}, echo=True)

session = Session(engine)

user_repo = ChurrosRepository[User](session, User)

3️⃣ Using Default CRUD Methods

# Save a user
user = user_repo.save(User(name="Alice", email="alice@example.com"))

# Find user by ID
user = user_repo.find_by_id(1)

# Get all users
users = user_repo.find_all()

# Update user
user.name = "Alice Updated"
user_repo.update(user)

# Delete a user
user_repo.delete(1)

# Delete all users
user_repo.delete_all()

🔍 Dynamic Query Methods (find_by_*)

Churros Repository automatically builds dynamic queries based on method names.

Examples

Method Query Equivalent
find_by_name("Alice") SELECT * FROM user WHERE name = 'Alice'
find_by_email("test@example.com") SELECT * FROM user WHERE email = 'test@example.com'
find_by_name_and_email("Alice", "test@example.com") SELECT * FROM user WHERE name = 'Alice' AND email = 'test@example.com'

Example Usage

user_repo.find_by_name("Alice")
user_repo.find_by_email("test@example.com")
user_repo.find_by_name_and_email("Alice", "test@example.com")
  • Method names must be case-sensitive
  • Supports _and_ to combine multiple conditions

Integrating with FastAPI

1️⃣ Setup Dependency Injection

from fastapi import FastAPI, Depends
from sqlmodel import Session, SQLModel
from app.repository.base import ChurrosRepository
from models import User
from database import engine

SQLModel.metadata.create_all(engine)

app = FastAPI()

def get_db():
    with Session(engine) as session:
        yield session

user_repo = ChurrosRepository[User](session, User)

2️⃣ Define API Endpoints

from fastapi import FastAPI, Depends
from sqlmodel import Session
from app.repository.base import ChurrosRepository
from models import User

app = FastAPI()

@app.post("/users/")
def create_user(user: User, db: Session = Depends(get_db)):
    return user_repo.save(user)

@app.get("/users/")
def get_all_users(db: Session = Depends(get_db)):
    return user_repo.find_all()

@app.get("/users/name/{name}")
def get_users_by_name(name: str, db: Session = Depends(get_db)):
    return user_repo.find_by_name(name)

@app.get("/users/email/{email}")
def get_users_by_email(email: str, db: Session = Depends(get_db)):
    return user_repo.find_by_email(email)

@app.delete("/users/{id}")
def delete_user(id: int, db: Session = Depends(get_db)):
    user_repo.delete(id)
    return {"message": "User deleted successfully"}

🔥 Why Use Churros Repository?

Zero Boilerplate - No need to write SQL queries
Spring JPA-like Dynamic Queries - find_by_* works automatically ✅ FastAPI & SQLModel Optimized - Best performance & typing support ✅ IDE Auto-Completion - Works with all major IDEs ✅ Lightweight & Simple - No extra dependencies


🤝 Contributing

We welcome contributions! Feel free to:

  • Open an issue 🐛
  • Submit a PR 🚀

📜 License

Churros Repository is open-source under the MIT License.


🌟 Support & Links


🎉 Happy Coding with Churros Repository! 🍩


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

pychurros-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

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

pychurros-0.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pychurros-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pychurros-0.1.0.tar.gz
Algorithm Hash digest
SHA256 be38f1d139ecc22b83ece96dcab4c6ba7bdd8fbeb13982c82eae1cef816ae408
MD5 e5fbb79fe107c3dd5741b0a26206b63b
BLAKE2b-256 05a9c24f5141fd748b2e71fb81547563e61cfa1da49ee39ea90eb3018becea7f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pychurros-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for pychurros-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ce7d3b7500fd153177ed6070e19ddcb244a66eae71539fa7309fa08fb2889158
MD5 af78c2e942abe0b55222df6e12d28b03
BLAKE2b-256 d723e4ca28c8478efada8b36a2a6a09b32ed866af62afb7c9afa6c9c4ab2e66b

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