Skip to main content

Library for transforming query parameters into SQLAlchemy filters in a structured and organised manner.

Project description

sqlaf

PyPI pyversions pypi

sqlaf is a library for transforming query parameters into SQLAlchemy filters in a structured and organised manner

Installation

Use the package manager pip to install sqlaf.

pip install sqlaf

Usage

Creating a Filter

A filter can be created by inheriting from the sqlaf Filter class and building up the fields to filter the query with. The class variable name used will be the key that will be extracted from the query parameters for that given filter.

from sqlaf import filters, fields

class TeamFilter(filters.Filter):

    founded_date = fields.DateField(Team.founded_date, operator="gte")
    name = fields.CharField(Team.name, operator="icontains")
    size = fields.IntegerField(Team.size, operator="eq")

Using a Filter

A filter class can be used in the following ways:

Query parameter string with "?"

query = session.query(Team)
query = TeamFilter(query).filter("?name=A&size=2")

Query parameter string without "?"

query = session.query(Team)
query = TeamFilter(query).filter("name=A&size=2")

Query parameter string with dictionary

query = session.query(Team)
query = TeamFilter(query).filter({"name": "A", "size": 2})

Available Fields

The following fields are available out the box (in the usage below, the parameters are the defaults set for each field):

Field Usage Available Operators
CharField CharField(Model.field, operator="eq", default=None) "eq", "~eq", "ieq", "~ieq", "contains", "icontains"
IntegerField IntegerField(Model.field, operator="eq", default=None) "eq", "~eq", "gt", "gte", "lt", "lte"
EnumField EnumField(Model.field, enum_class=Enum operator="eq", default=None) "eq", "~eq"
BooleanField BooleanField(Model.field, operator="eq", truthy=[True, 1], falsy=[False, 0], default=None) "eq"
ArrayField ArrayField(Model.field, operator="eq", default=None) "contains", "~contains"
DateField DateField(Model.field, format=""%Y-%m-%d", operator="eq", default=None) "eq", "~eq", "gt", "gte", "lt", "lte"
DateTimeField DateTimeField(Model.field, format="%Y-%m-%dT%H:%M:%S%z", operator="eq", default=None) "eq", "~eq", "gt", "gte", "lt", "lte"
TimeField TimeField(Model.field, format="%H:%M:%S%z", operator="eq", default=None) "eq", "~eq", "gt", "gte", "lt", "lte"

Available Operators

The following operators are available out of the box.

Key Name Description
eq Case-sensitive equals The value is equal to the column value.
ieq Case-insensitive equals The value is equal to the column value regardless of case.
~eq Case-sensitive not equal The value is not equal to the column value.
~ieq Case-sensitive not equal The value is not equal to the column value regardless of case.
gt Greater than The value is greater than the column value.
gte Greater than or equal The value is greater than or equal to the column value.
lt Less than The value is less than the column value.
lte Less than or equal The value is less than or equal to the column value.
contains Contains The value is contained within the column value.
~contains Does not contain The value is not contained within the column value.
icontains Case-insensitive contains The value is contained within the column value regardless of case.

Custom Operators

To extend the above operators, you can create custom operators:

from sqlaf import filters, fields


def custom_operator(source, value):
    return column == value


class TeamFilter(filters.Filter):
    
    team_size = fields.IntegerField(Team.team_size, operator=custom_operator)

Custom Filtering

If filtering is needed that is not covered by the sqlaf framework, add custom filtering by using the post_filter function:

from sqlaf import filters, fields

class TeamFilter(filters.Filter):
    
    size = fields.IntegerField(Team.size, operator="eq")
    
    def post_filter(self, data, filters):
        if name := data.get("name"):
            filters.append(Team.name == name)

        return filters

Todo

  • Prepare roadmap.
  • Write better documentation.
  • Establish contribution and release processes.

Contributing

For the meantime, I will be maintaining the project myself while getting v1.0 prepared. After v1.0 is released, the guidelines and processes for contribution will be documented here :)

License

Apache 2.0

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

sqlaf-1.0.0.tar.gz (37.2 kB view details)

Uploaded Source

Built Distribution

sqlaf-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sqlaf-1.0.0.tar.gz.

File metadata

  • Download URL: sqlaf-1.0.0.tar.gz
  • Upload date:
  • Size: 37.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for sqlaf-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bc81cad1607c5684bcc8a51351f740e9c77519abf2316473adc785be44b665e8
MD5 96a4802b974d5eb205daebffbd1bc988
BLAKE2b-256 1a47264633801da1e32febcbebe9505181412c1ffc00c89dd88dcccf59120185

See more details on using hashes here.

File details

Details for the file sqlaf-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sqlaf-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for sqlaf-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bc2f3779f66ca88e71d7bc37270b2b22b61948f64d95accf20c5e4e8ffeb225a
MD5 fd6fd1370e20099207e85a75b0118fe2
BLAKE2b-256 ef3688393779500868f4e353e94f2fbc3a2e5e0b8791d99dbf21b98fc3ab34ff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page