dmpayton / reqon

Build simple, read-only RethinkDB queries from JSON

Home Page:https://reqon.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReQL Query Object Notation

travis-ci.org codeclimate.com codecov.io

ReQON ([ɹiːˈkʰɑn], /riːˈkɑn/, RE-kon) lets you build simple, read-only RethinkDB queries from JSON.

I love RethinkDB, and ReQL is awesome and powerful, but sometimes you need to expose RethinkDB's querying capabilities through an HTTP API endpoint. ReQON lets you do that.

ReQON transforms this:

{
    '$table': 'movies',
    '$query': [
        ['$filter', {'predicate': [
            ['rating', ['$gt', 8]],
            ['$or', [
                ['year', ['$lt', 1990]],
                ['year', ['$gt', 1999]]
            ]]
        ]}],
        ['$order_by', {'index': 'rank', 'ordering': 'asc'}],
        ['$sample', {'n': 5}]
    ]
}

into this:

r.table('movies').filter(
    r.row['rating'] > r.expr(8),
    r.or_(
        r.row['year'] < r.expr(1990),
        r.row['year'] > r.expr(1999),
    )
).order_by(r.asc('rank'))
).sample(5)

ReQON makes it easy to query RethinkDB through a web API:

import json
import reqon
import rethinkdb as r


class QueryEndpoint(View):
    def post(self, request):
        # Open a RethinkDB connection
        conn = r.connect()

        # Build the query from the POST body
        query = json.loads(request.body)
        reql = reqon.build_reql(query)

        # Get the results and close the connection
        results = reql.run(conn)
        if isinstance(results, r.Cursor):
            results = list(results)
        conn.close()
        return HttpResponse(json.dumps({'data': results}))

About

Build simple, read-only RethinkDB queries from JSON

https://reqon.readthedocs.org/

License:MIT License


Languages

Language:Python 100.0%