Queries
Discret provides a query engine inspired by GraphQL
Most of the examples of this document will use the following data model:
with the following data:
mutate
Base Syntax
Let's start with a simple query that will retrieve the name and surname of the Person:
query
You will notice that:
- result is an alias, it can have a different name,
- the requested fields are not delimited by a comma.
This will return the following JSON object:
You will notice that:
- the query result is a JSON table, this is true even if there is only one result,
- If the database does not contains requested data, the query will return an empty table.
Multiple Queries
You can perform several queries at the same time. The following query will return the Pet list and the Person list.
query
persons et pets aliases must be unique withing the query because each one will generate a JSON object field.
It will return the following JSON object:
Filters
Previous examples recovers all the tuples of a specified entity, in real life most query will need to filter data.
Filters supports the following operators:
- = equals
- != not equals
- > greater than
- >= greater than or equals
- < lower than
- <= lower than or equals
The syntax is the following:
query
This Person query is filtered by name and indicates that we only want to retrieve tuples whose name equals to "Doe". It provides the following result:
You can define multiple filters by separating them with a comma.
query
You can also filter using the null value:
query
Full text search
By default, every text field is indexed to allow full text search using the search keyword. The search engine uses a trigram index, only string greater or equal than 3 characters can be searched.
The indexing can be disabled for an entity.
query
This will only returns the "Alice" tuple:
Nested queries
You can query an entity and its relations at the same time.
The following query get the Person and their Pets:
query
It produces the following result:
The query only provides Person that have at least a Pet. Person without pets are filtered. This makes the result very easy to use as it is guaranteed that you will never encounter a null value
Filters can be applied to sub-entities:
query
It produces:
"Nullables" Relations
For some query, you may wan't to recover entities with empty relations.It can be done by using the nullable keyword that defines the relation fields that can be null for the query. For example:
query
For this query, the pet field is allowed to be null and the query will return all the Person:
Once a field has been defined nullable, you can add a filter to only get tuples that have an empty relation:
query
Sorting result
When a query returns several tuples, the tuple order is not guaranteed. Sorting results can be done by using the order_by keyword.
You can sort on multiple fields. Each field must have a sort order:
- asc lower to greater (ascending)
- desc greater to lower (descending)
query
The query returns:
The order_by clause can be used in sub-entities.
Limiting the number of results
If a query is expected to return a large number of results, you can limit the number of field by using the first keyword.
The following query returns the first two tuples:
query
You can also skip a number of tuples by using the skip keyword.
The following query skip the first result and returns the next two tuples.
query
Pagination
It is tempting to use the skip and first keyword to implement pagination for large set of data, but this method can have performance issue because skipping tuple can be expensive for large dataset.
Discret provides an alternative with the before et after keywords. Those keyword work in association with the order_by an allow to filter the sorted fields.
The query syntax is the following:
query
The returned tuples will match those rules
- mdate system field greater than the $date parameter
- if the mdate is equal to $date, it will ensure that the id system field is greater than the $id parameter.
This method is much faster than the skip and first one.
Json Selector
JSON fields can be explored using the JSON selector ->$..
Let's consider the following data model:
with the following tuple
mutate
The following query will recover the title of the article
query sample
Aggregates
Discret provides the following aggregate function. (field) is the name of the field to aggregate.
- avg(field)
- count()
- max(field)
- min(field)
- sum(field)
An aggreation field has the form alias: function(). In a query, if an entity defines an aggregated field, all other fields must also be aggregated.
The following query counts the number of parents for a Person:
query
and provides the following result:
We can see that the parents field is used two times:
- once with the alias parent_count to count the number of parents
- once to recover the parents name
It is mandatory to use it two times because we cannot define aggregates and normal field in the same entity. The following query with name et count() would make no sense and will return an error:
query