Skip to main content

A base repository for FastAPI projects, inspired by Ruby on Rails' Active Record and Ransack.

Project description

FastAPI Repository

A base repository for FastAPI projects, inspired by Ruby on Rails' Active Record and Ransack. It provides a simple, intuitive, and asynchronous interface for data access using SQLAlchemy.

Key Features

  • Async-first: Designed for modern asynchronous Python.
  • Simple CRUD: find, create, update, destroy methods out of the box.
  • Powerful Filtering: Use Ransack-style operators (__icontains, __gt, etc.) for complex queries.
  • Relationship Loading: Control eager (joinedload) and lazy (lazyload) loading.
  • Default Scoping: Apply default conditions to all queries.

Installation

pip install fastapi-repository

Quick Start

  1. Define a repository for your SQLAlchemy model:

    # repositories.py
    from fastapi_repository import BaseRepository
    from sqlalchemy.ext.asyncio import AsyncSession
    from .models import User  # Your SQLAlchemy model
    
    class UserRepository(BaseRepository):
        def __init__(self, session: AsyncSession):
            super().__init__(session, model=User)
    
  2. Use it in your FastAPI application:

    # main.py
    from fastapi import FastAPI, Depends
    from sqlalchemy.ext.asyncio import AsyncSession
    from .database import get_session
    from .repositories import UserRepository
    
    app = FastAPI()
    
    @app.get("/users/{user_id}")
    async def get_user(user_id: int, session: AsyncSession = Depends(get_session)):
        repo = UserRepository(session)
        user = await repo.find(user_id)
        return user
    

Documentation

For a complete guide, including a full tutorial, API reference, and advanced usage, please see the Full Documentation.

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

fastapi_repository-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

fastapi_repository-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_repository-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f8c65d912174eaca705160987e36493af56020fba0df0527fbbf1275fdd73eec
MD5 356fd51dcfc4225593deec1b772d53c6
BLAKE2b-256 07cd52218b873ec16d10b6dcafeefc0a7cc3dc2b9fab50e02bcc221804baeec8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_repository-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3dac381c07a7cc112e6fb338fcd5c59c83f1e9d27bfcee7765a0c026a511a83f
MD5 05d6603359a585a0b8ac68088e4897b4
BLAKE2b-256 811b7cd956abc124cc84232ced441aa181d056acdcff3ebfceb26ff20c3b7f76

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