codemation / pydbantic

A single model for shaping, creating, accessing, storing data within a Database
https://pydbantic.readthedocs.io/en/latest/
Apache License 2.0
228 stars 15 forks source link

Improved model filtering conditions #22

Closed codemation closed 2 years ago

codemation commented 2 years ago

Description

In this PR, DataBaseModelCondition and DataBaseModelAttribute is created to improved annotation with conditions and to store / store values associated with query conditions needed for query caching. DataBaseModelAttribute is used to easily reference sqlalchemy models associated with model attributes, also provide an interface for storing values associated with sqlalchemy conditions, and enabling attribute based Conditionals in filters. At startup, or post migration, DataBaseModels now have attributes with names matching associated fields used for filter condition. Added new method, allowing for grouping of conditionals which do not ALL need to be True. data attributes now have a .matches() method which allows for IN [value1, value2, value3] querries

Examples


    mid_salary_employees = await Employees.filter(
        Employees.salary >= 30000,
        Employees.salary <= 40000
    )

    second_employees = await Employees.filter(
        Employees.salary.matches([20000, 40000])
    )

    mid_salary_employees = await Employees.filter(
        Employees.OR(
            Employees.salary >= 30000,
            Employees.salary.matches([20000, 40000])
        ),
        Employees.is_employed == True
    )