Custom Queries

One of the tools that the AnythingDB module makes available to users is called Custom Queries. T​his function offers the ability to perform queries directly on the AnythingDB database where the information of the different data models (Things and Categories) defined by the users is stored.

In this way, you will be able to perform complex queries on the data stored in the platform and similarly develop and test complex queries that can later be incorporated into the business logic implemented in the Workers of the Function module.

To access this section, in the AnythingDB section, click Custom Queries.


Figure 1.

Thing Diagram

To perform queries correctly, you need to know how the data is stored within the AnythingDB database in order to exploit its different possibilities. To help you understand this structure, a diagram is shown below, corresponding to the right half of the screen, where the data structure followed by the different elements stored on the platform is defined.


Figure 2.

Running AnythingDB Queries

To run queries, enter the code with the appropriate format in the text box located below the Run button.


Figure 3.
By default, the code corresponding to one of the examples offered by the platform is included. You can see the different fields that can be included when making queries. Following is an explanation for each of them.
query
The Anything DB query itself with the data to be collected from the database.
count
True/False, define a count of items matching the query that should be returned in the result or not.
batchSize
Defines the number of elements to display in the response
bindVars
Variables referenced in the query
The response returned by the platform will content the following parameters:
code
Response status code
count
Number of elements that matched the query
error
True / false, depending on existing errors
extra
(stats): Stats about the execution of the query
id
See the pagination section below for more information
hasMore
True / false depending on if all elements were returned in result
result
Elements returned by the query.


Figure 4.

Example Queries

To help you understand this tool, three query examples are included that can be chosen from the drop down menu located at the top of the page.


Figure 5.

To use these examples, replace the values in the bindVars parameter of the body.

Using AnythingDB Queries from a Worker

A powerful way to explore the use of AnythingDB from within the SmartWorks platform is by automating these queries through the use of functions. To be able to incorporate calls in the functions, use the https protocol using a POST call.

You must have the necessary credentials to make the call following the basic operation of our API.
https://api.swx.altairone.com/beta/spaces/develop/query/cursor/

The body of the POST call must include in JSON format the fields defined above, where the query to be made will be included.

Pagination

Occasionally, the result returned after a query will not show all the results. This will be indicated in the response field with the title hasMore. To access the rest of the results, use the field "id" that is returned in the query result.

Make a call with the URL indicated in the previous section, but incorporate the ID that was returned in the response.
https://api.swx.altairone.com/beta/spaces/develop/query/cursor/id

You can make calls to this URL sequentially until you get the rest of the values. The hasMore field will return false when the last data has been displayed.

Documentation

To learn more about AnythingDB Beta, you can access its documentation by clicking .