Skip to main content

base-repository

Project description

Base Repository

Python License Release Coverage

A repository library that wraps SQLAlchemy and provides built-in CRUD and a query DSL.
Once you inherit from BaseRepository, you can use create/read/update/delete right away.

  • No need to re-implement simple CRUD over and over.
  • If you need custom repo methods, you can add them in your project and extend freely.
  • Supports introducing a Mapper for conversions between Pydantic schemas and SQLAlchemy ORM models.

Supported Dependency Versions

The table below lists the minimum required versions for each supported dependency.

To see more about how to test and the result.

python_version sqlalchemy pydantic
3.10 >= 1.4 >= 1.10
>= 3.13 >= 1.4 >= 2.8, 1.7-1.10

Links


Installation

# Before publishing to PyPI
pip install -U "base-repository @ git+https://github.com/4jades/base-repository.git"

# After publishing to PyPI
pip install -U fastapi-base-repository

Quick Start

1) Define Filter and Repo

from dataclasses import dataclass
from pydantic import BaseModel
from base_repository import BaseRepoFilter
from base_repository.repository import BaseRepository

# Example: existing SQLAlchemy ORM model and Pydantic schema in your project
class Base(DeclarativeBase):
    pass

class UserModel(Base):
    __tablename__ = "users"

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(nullable=False)

class UserSchema(BaseModel):
    id: int
    name: str
    model_config = ConfigDict(from_attributes=True)


# What you need to add for Base Repository: Filter + Repo
@dataclass
class UserFilter(BaseRepoFilter):
    id: int | iterable[int] | None = None
    name: str | iterable[str] | None = None

class UserRepo(BaseRepository[UserModel]):
    filter_class = UserFilter
    mapping_schema = UserSchema  # Optional: return Pydantic objects by default

2) Plug in a SessionProvider

Decide how the repository should obtain an AsyncSession.
Recommended: inject a SessionProvider once, so the repo can fetch sessions when needed.

from typing import Protocol
from base_repository import SessionProvider, BaseRepository

# Example SessionProvider implementation
class MysqlSessionProvider(SessionProvider):
    def get_session(self) -> AsyncSession:
        return Mysql.current_session()

BaseRepository.configure_session_provider(MysqlSessionProvider())

Alternatively, you can pass a session directly when creating the repo.

async with AsyncSession(engine) as session:
    repo = UserRepo(session)

3) Use CRUD immediately

repo = UserRepo()

# Create
created = await repo.create({"name": "Alice", "email": "a@test.com"})

# Get one
user = await repo.get(UserFilter(name=["Alice", "Bob"]))
user_orm = await repo.get(UserFilter(name="Alice"), convert_domain=False)

# List (OFFSET paging)
q = (
    repo.list()
        .where(UserFilter(name="A"))
        .order_by(["id"])
        .paging(page=1, size=20)
)
users = await repo.execute(q)

# List (CURSOR paging)
q1 = (
    repo.list()
        .order_by(["id"])
        .with_cursor(None)
        .limit(20)
)
users1 = await repo.execute(q1)

# Update / Delete / Count
cnt = await repo.count(UserFilter(name="Alice"))
updated_rows = await repo.update(UserFilter(name="Bob"), {"email": "bob@new"})
deleted_rows = await repo.delete(UserFilter(name="Alice"))

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

base_repository-1.0.1.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

base_repository-1.0.1-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file base_repository-1.0.1.tar.gz.

File metadata

  • Download URL: base_repository-1.0.1.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for base_repository-1.0.1.tar.gz
Algorithm Hash digest
SHA256 11c7da90b255c299237ec6693a222f031870f98852beff8fdd978578b638e047
MD5 0dce7338a5c93366bb8b6fce1e63cbef
BLAKE2b-256 93b9f8f9a2d6a18f2e368e7cad19a0f997cf344aa422a92b2b83c471af6bbd2a

See more details on using hashes here.

File details

Details for the file base_repository-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for base_repository-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4f32f9db4cce56485613abaac1cc8468e163a4ecb93b380e7464f60c7d3f717
MD5 314246b16a9f579da8c9405e03782ef6
BLAKE2b-256 f49fc5d3de8d08155849124d39e23d5a6ba55517228e12924cd5e0caf4006fe4

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