Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

rql

> 数据库
Open source

Resource Query Language for REST

366 stars0 likes2 views
WebsiteGitHub

About

Resource Query Language for REST

RQL is a resource query language for REST. It provides a simple and light-weight API for adding dynamic querying capabilities to web-applications that use SQL-based database. It functions as the connector between the HTTP handler and the DB engine, and manages all validations and translations for user inputs.

Motivation

In the last several years I have found myself working on different web applications in Go, some of them were small and some of them were big with a lot of entities and relations. In all cases I never found a simple and standard API to query my resources.

What do I mean by query? Let's say our application has a table of orders, and we want our users to be able to search and filter by dynamic parameters. For example: select all orders from today with price greater than 100.
In order to achieve that I used to pass these parameters in the query string like this: created_at_gt=X&price_gt=100.
But sometimes it became complicated when I needed to apply a disjunction between two conditions. For example, when I wanted to select all orders that canceled or created last week and still didn't ship. And in SQL syntax:

sql
SELECT * FROM ORDER WHERE canceled = 1 OR (created_at  Y AND shipped = 0)

I was familiar with the MongoDB syntax and I felt that it was simple and robust enough to achieve my goals, and decided to use it as the query language for this project. I wanted it to be project agnostic in the sense of not relying on anything that related to some specific application or resource. Therefore, in order to embed rql in a new project, a user just needs to import the package and add the desired tags to his struct definition. Follow the Getting Started section to learn more.

Getting Started

rql uses a subset of MongoDB query syntax. If you are familiar with the MongoDB syntax, it will be easy for you to start. Although, it's pretty simple and easy to learn.
In order to embed rql you simply need to add the tags you want (filter or sort) to your struct definition, and rql will manage all validations for you and return an informative error for the end user if the query doesn't follow the schema. Here's a short example of how to start using rql quickly, or you can go to API for more expanded documentation.

…

For input: { "admin": true }

Result is: admin = ?

You can see that RQL uses placeholders in the generated `WHERE` statement. Follow the [examples](#examples) section
to see how to use it properly. 
- If the field follows the format: `field: { : , ...}`, For example:

For input: { "age": { "$gt": 20, "$lt": 30 } }

Result is: age > ? AND age = ? AND zip ? AND created_at < ? AND (address = ? OR address = ?)" fmt.Println(params.FilterArgs) // [true, Time(2018-01-01T16:00:00.000Z), Time(2018-04-01T16:00:00.000Z), "TLV", "NYC"]

…

go type User struct { Address struct { Name string rql:"filter" } }

rql assumes that the `address_name` field exists in the table. Sometimes it's not the case and `address` exists in
a different table. Therefore, I want to add the `table=` option for fields, and support nested queries.
- [ ] Code generation version - low priority

## Performance and Reliability
The performance of RQL looks pretty good, but there is always a room for improvement. Here's the current bench result:

|      __Test__       | __Time/op__    | __B/op__   | __allocs/op__  |
|---------------------|----------------|------------|----------------|
| Small               |    1809        |   960      |   19           |
| Medium              |    6030        |   3100     |   64           |
| Large               |    14726       |   7625     |   148          |

I ran fuzzy testing using `go-fuzz` and I didn't see any crashes. You are welcome to run by yourself and find potential failures. 

## LICENSE
I am providing code in the repository to you under MIT license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook)

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gogoormrest-apirql

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库