WinterFramework / winter

Web Framework with focus on python typing, dataclasses and modular design
MIT License
17 stars 18 forks source link

Custom methods in CRUDRepository #179

Closed pristupa closed 1 year ago

pristupa commented 4 years ago

Is your feature request related to a problem? Please describe. Now, CRUDRepository supports only standard auto-generated methods (find_by_id, save, etc.). It's difficult to use it widely without a way to define custom methods like find_one_by_passport_number.

Describe the solution you'd like This is an example of a custom method I'd like to use in my repository:

class PersonRepository(CRUDRepository[Person, UUID]):
    def find_one_by_passport_number(self, passport_number: str) -> Optional[Person]:
        result = self._get_query().filter(Person.passport_number == passport_number).values(Person.id)
        id_ = next(result, None)
        if id_ is None:
            return None
        return self.find_by_id(id_)

Describe alternatives you've considered Would be also cool to auto-generate this kind of method just by its name.

class PersonRepository(CRUDRepository[Person, UUID]):
    def find_one_by_passport_number(self, passport_number: str) -> Optional[Person]:
        pass

It allows avoiding to do two SQL queries instead of one. Or avoiding to do one SQL query but complex logic with session factory etc.

Additional context No.

mofr commented 1 year ago

Implemented since 9.0.0