Skip to content

Query APIs

Query APIs are a more free-form and type-unsafe way of exposing data using virtual tables based on user inputs and stored procedures. Please make sure to take a look at record APIs first. Views and generated columns may be a better fit for transforming data if no explicit user input is required.

Example

Using migrations and sqlean’s define we can define a table query with unbound inputs (see placeholder $1):

CREATE VIRTUAL TABLE
_is_editor
USING
define((SELECT EXISTS (SELECT * FROM editors WHERE user = $1) AS is_editor));

Subsequently, an API can be configured to query the newly created VIRTUAL TABLE, also binding URL query parameters as inputs to above placeholders.

query_apis: [
{
name: "is_editor"
virtual_table_name: "_is_editor"
params: [
{
name: "user"
type: BLOB
}
]
acl: WORLD
}
]

Finally, we can query the API, e.g. using curl:

Terminal window
curl -g 'localhost:4000/api/query/v1/is_editor?user=<b64_user_id>'