Skip to content

Gasper3/sa-repository

Repository files navigation

SQLAlchemy Repository for models

tests workflow

This project contains simple Repository pattern for SQLAlchemy models.
All you need to do is:

  1. Install this package python -m pip install sa-repository
  2. Use it in your project
    from sa_repository import BaseRepository
    from models import YourSAModel
    
    class SomeModelRepository(BaseRepository[YourSAModel]):
        pass

Base class contains some general methods to simplify your work with sqlalchemy models e.x

var = SomeModelRepository(session).get(YourSAModel.attr == 'some_value')

If you don't want to create new repository classes, you can use get_repository_from_model method

repository = BaseRepository.get_repository_from_model(db_session, SomeModel)