Skip to main content

Resource Query Language to ORM

Project description

Quality Gate Status Coverage PyPI - Python Version

ReQueLa

ReQueLa, your data assistant!

What is ReQueLa?

ReQueLa is a Python library that allows you to build complex queries to filter and order data in your database. Query are written using RQL, Resource Query Language, a query language expecially designed for RESTful resources.

Installation

pip install requela

Supported RQL operators

Syntax Explanation
eq(field,value) Equal to
ne(field,value) Not Equal to
gt(field,value) Greater Than
gte(field,value) Greater Than or Equal
lt(field,value) Less Than
lte(field,value) Less Than or Equal
in(field,(value1,value2,...)) In list
out(field,(value1,value2,...)) Not In list
like(field,value) Like
ilike(field,value) Case-insensitive Like
and(expression1,expression2,...) Logical AND
or(expression1,expression2,...) Logical OR
not(expression) Logical NOT
any(relationship,expression) ANY operator for to-many relationships
order_by(field1,field2,...) Order By fields

Examples

  • Filtering: and(eq(name,John),gt(age,30))
  • Navigation through relationships using dot notation: eq(account.name,My Account) to filter on a related field.
  • Filtering with ANY operator: any(users,eq(users.name,John))
  • Ordering: order_by(name,age)
  • To order in descendant order, prefix the property name with a minus sign: order_by(-age)
  • Combining filtering and ordering: and(eq(name,John),gt(age,30))&order_by(name,age)
  • Using logical operators: or(eq(name,John),eq(name,Jane))&order_by(name)
  • Using NOT operator: not(eq(name,John))&order_by(name)
  • Using LIKE operator with wildcard symbol *: like(name,*John*)
  • Using ILIKE operator with wildcard symbol *: ilike(name,*John*)

Usage

Use QueryBuilder directly

SQLAlchemy

from requela.builders.sqlalchemy import SQLAlchemyQueryBuilder

from app.db import get_session
from app.models import MyModel

builder = SQLAlchemyQueryBuilder(MyModel)
statement = builder.build_query("and(eq(name,John),gt(age,30))&order_by(name,-age)")

with get_session() as session:
    result = session.execute(statement)
    for model in result:
        print(model)

Django

from requela.builders.django import DjangoQueryBuilder

from app.models import MyModel

builder = DjangoQueryBuilder(MyModel)
result = builder.build_query("and(eq(name,John),gt(age,30))&order_by(name,-age)")

for model in result:
    print(model)

Create a ModelFilter class

You can create a class to specify filtering and ordering rules for a given model like:

from requela.filters import FieldDefinition, ModelFilter, Operator
from tests.django.models import Account, User


class AccountFilter(ModelFilter):
    __model__ = Account

    name = FieldDefinition()
    description = FieldDefinition()
    status = FieldDefinition()
    balance = FieldDefinition()
    created_at = FieldDefinition(
        alias="events.created.at",
    )


class UserFilter(ModelFilter):
    __model__ = User

    name = FieldDefinition()
    age = FieldDefinition()
    role = FieldDefinition(
        allowed_operators=[Operator.IN, Operator.OUT],
    )
    is_active = FieldDefinition(allow_ordering=False)
    birth_date = FieldDefinition(
        alias="events.born.at",
    )
    account = AccountFilter()

    filters = UserFilter()
    query = filters.build_query("and(eq(name,John),gt(age,30),eq(events.born.at,2024-01-01))&order_by(name,-age)")

License

ReQueLa is licensed under the Apache 2.0 License and is copyright SoftwareOne AG.

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

requela-0.1.1.tar.gz (151.4 kB view details)

Uploaded Source

Built Distribution

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

requela-0.1.1-py3-none-any.whl (21.0 kB view details)

Uploaded Python 3

File details

Details for the file requela-0.1.1.tar.gz.

File metadata

  • Download URL: requela-0.1.1.tar.gz
  • Upload date:
  • Size: 151.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for requela-0.1.1.tar.gz
Algorithm Hash digest
SHA256 87258420377d84df7eb15b6c707459a7f2a80148a77a56e21e405c3511e36217
MD5 bc3387ec35ac15b1c39adb49c5e0293f
BLAKE2b-256 dc7965379df457b028ccfb52b9460d3a6b661ad941af97cce594e95932baf86c

See more details on using hashes here.

Provenance

The following attestation bundles were made for requela-0.1.1.tar.gz:

Publisher: on-release-main.yml on softwareone-platform/requela

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file requela-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: requela-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for requela-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 083eff29a7d16342422cbf780920463675b5526563b2a025c2858b7d6a07c5c8
MD5 19736c0bec11a0776ee01bbac62777bb
BLAKE2b-256 d83df3d433566c81accd9390a6b2b75b53b994eeaca014425320776df2fea379

See more details on using hashes here.

Provenance

The following attestation bundles were made for requela-0.1.1-py3-none-any.whl:

Publisher: on-release-main.yml on softwareone-platform/requela

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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