Skip to main content

RQLAlchemy

Build Status

Resource Query Language extension for SQLAlchemy

Overview

Resource Query Language (RQL) is a query language designed for use in URIs, with object-style data structures.

rqlalchemy is an RQL extension for SQLAlchemy, making it easy to expose SQLAlchemy tables or models as an HTTP API endpoint and perform complex queries using only query string parameters.

Installing

pip install rqlalchemy

Usage

Support RQL queries in your application by using the select() construct provided by RQLAlchemy. After creating the selectable, use the rql() method to apply the RQL query string, and then use the execute() method with the session to retrieve the results.

For example, in a Flask HTTP API with a users collection endpoint querying the User model:

from urllib.parse import unquote
from flask import request

from rqlalchemy import select

@app.route('/users')
def get_users_collection():
    qs = unquote(request.query_string.decode(request.charset))
    users = select(User).rql(qs).execute(session)

    return render_response(users)

The .execute() method handles the session and adjusts the results accordingly, returning scalars, lists of dicts, or a single scalar result when appropriate. There's no need to use session.execute() or session.scalars() directly unless you want to handle the results yourself.

Pagination

RQLAlchemy offers limit/offset pagination with the rql_paginate() method, returning the requested page, RQL expressions for previous and next pages if available, and the total number of items.

from urllib.parse import unquote
from flask import request

from rqlalchemy import select

@app.route('/users')
def get_users_collection():
    qs = unquote(request.query_string.decode(request.charset))
    res = select(User).rql(qs).rql_paginate(session)

    response = {
        "data": res.page,
        "total": res.total,
    }

    if res.previous_page:
        response["previous"] = '/users?' + res.previous_page

    if res.next_page:
        response["next"] = '/users?' + res.next_page

    return render_response(response)

Pagination requires a limit, as a RQLSelect._rql_default_limit value, a query string limit(x), or the limit parameter to the rql() method. Calling rql_paginate() without a limit will raise RQLQueryError.

Reference Table

RQL SQLAlchemy equivalent Observation
QUERYING
select(a,b,c,...) select(Model.a, Model.b, Model.c,...)
values(a) [o.a for o in query.from_self(a)]
limit(count,start?) .limit(count).offset(start)
sort(attr1) .order_by(attr)
sort(-attr1) .order_by(attr.desc())
distinct() .distinct()
first() .limit(1)
one() [query.one()]
FILTERING
eq(attr,value) .where(Model.attr == value)
ne(attr,value) .where(Model.attr != value)
lt(attr,value) .where(Model.attr < value)
le(attr,value) .where(Model.attr <= value)
gt(attr,value) .where(Model.attr > value)
ge(attr,value) .where(Model.attr >= value)
in(attr,value) .where(Model.attr.in_(value)
out(attr,value) .where(not_(Model.attr.in_(value)))
contains(attr,value) .where(Model.contains(value)) Produces a LIKE expression when querying against a string, or an IN expression when querying against an iterable relationship
excludes(attr,value) .where(not_(Model.contains(value))) See above.
and(expr1,expr2,...) .where(and_(expr1, expr2, ...))
or(expr1,expr2,...) .where(or_(expr1, expr2, ...))
AGGREGATING All aggregation functions return scalar results.
aggregate(a,b(c),...) select(Model.a, func.b(Model.c)).group_by(Model.a)
sum(attr) select(func.sum(Model.attr))
mean(attr) select(func.avg(Model.attr))
max(attr) select(func.max(Model.attr))
min(attr) select(func.min(Model.attr))
count() select(func.count())

Metadata

Release files for rqlalchemy 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rqlalchemy 0.6.1
File Size Uploaded
rqlalchemy-0.6.1.tar.gz 367.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rqlalchemy 0.6.1
File Interpreter ABI Platform
rqlalchemy-0.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 375.1 kB

Release files / rqlalchemy-0.6.1.tar.gz

Download URL rqlalchemy-0.6.1.tar.gz
Size 367.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9113abd1401331728d98fbb3ae293130ddba1af04e0e83d30cdff9a1eb641c59
BLAKE2b-256 checksum
How to use checksums
1ee126916584e44355b52510d09cbbc6cd98ed695921138cadcbeed18f9f3eb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / rqlalchemy-0.6.1-py3-none-any.whl

Download URL rqlalchemy-0.6.1-py3-none-any.whl
Size 8.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
644375cbd4b5c5df232b17dd9b96622b01d9fd268eadd0d3b2d1470eac55a721
BLAKE2b-256 checksum
How to use checksums
52e959bba8c6a4c1dc8ffa552e483baa3dab480db4cb6c2f11b6f5ef565ecea7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.6.1 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page