How to make queries on Entities with the FlowEngine? (Query Static and Query Dynamic nodes)
Introduction
This guide describes the Query Static and Query Dynamic nodes, showing examples for each. The methodology that the guide follows is the creation of an ontology, definitions of the queries and creation of the flow in FlowEngine.
Creating the Ontology
Start by creating an ontology called queryNoderedTest. Using the Creation Step by Step option, add the fields "field" of type string and "value" of type number, update the schema and generate the instance.
JSON of the queryNoderedTest ontology
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "queryNoderedTest",
"type": "object",
"required": [
"queryNoderedTest"
],
"properties": {
"queryNoderedTest": {
"type": "string",
"$ref": "#/datos"
}
},
"datos": {
"description": "Info queryNoderedTest",
"type": "object",
"required": [
"field",
"value"
],
"properties": {
"field": {
"type": "string"
},
"value": {
"type": "number"
}
}
},
"description": "Creation of an ontology for the use of query nodes in the Flow Engine.",
"additionalProperties": true
}You can insert data through the CRUD of the ontology itself or through the FlowEngine and its Insert node.
Defining the Query Static node
A static query is based on the information added at the time of creation. Once the initial data is established, the fields cannot be modified.
You can make several types of requests through this node, both data and information, including: select, update y delete.
Node properties
Name: Optional field used to identify the node with a specific name.
Ontology: Name of the ontology on which you want to query, update or delete data.
Query type: There are two options depending on the type of database you are working with.
SQL
Native. Used if you have MongoDB as database.
Query: Data query, update or deletion sentence to be launched.
The output data will depend on the type of query that is registered in the node.
Defining the Query Dynamic node
In a dynamic query, the information can be updated at any time. As was the case with the static query, the requests that can be made are query, update, and deletion.
Node properties
Name: Optional field used to identify the node with a specific name.
This node expects to receive information from a function. This code will be passed through the function node. The parameters to be entered are:
Ontology: Name of the ontology on which you want to query, update or delete data.
Query type: There are two options depending on the type of database you are working with.
SQL
Native. Used if you have MongoDB as database.
Query: Data query, update or deletion sentence to be launched.
The output data will depend on the type of query that is registered in the node.
Creating the flow in FlowEngine
Flow with Query Static
Start with the creation of a static query flow. The nodes to be used are:
Inject: Activates the flow manually or at intervals.
Query Static: Used to query against static databases.
Debug: Monitors the messages.
Query flow, select
Double click on the query static node to open the window corresponding to the right image. Fill in the fields:
Name: select query
Ontologies available: queryNoderedTest
Ontology: queryNoderedTest
Query type: SQL
Query: select * from queryNoderedTest
In the Debug node, change the msg. option is changed to complete msg object. The result obtained in the payload is a text-type message with the ontology information. If you want to convert it to an object, you will have to make a parse object.
Update flow, update
Double click on the query static node to open the window corresponding to the right image. Fill in the fields:
Name: update query
Ontologies available: queryNoderedTest
Ontology: queryNoderedTest
Query type: SQL
Query: update queryNoderedTest set queryNoderedTest.value=24;
In the Debug node, change the msg. option is changed to complete msg object. The result obtained in the payload is a counter with the number of objects that have been modified. In this example, only one value is modified.
Flujo de borrado, delete
Double click on the query static node to open the window corresponding to the right image. Fill in the fields:
Name: delete query
Ontologies available: queryNoderedTest
Ontology: queryNoderedTest
Query type: SQL
Query: delete from queryNoderedTest where queryNoderedTest.value=24;
In the Debug node, change the msg. option is changed to complete msg object. The result obtained in the payload is a counter with the number of objects that have been deleted. In this example, only one value is deleted.
Flow with Query Dynamic
Creating a flow with query dynamic. The nodes to be used are:
Inject: Activates the flow manually or at intervals.
Function: JavaScript function against the messages received by the node.
Query Static: Used to query against static databases.
Debug: Monitors the messages.
Enter the name that the node will carry in the window of the query dynamic node, in this case, Dynamic query. Also, change the Debug option from msg to complete msg object.
In the Function node, write the fields requested by the dynamic query, that is: ontology, type of query and query. You can give this node a name, nodo consultas (“query node”)
msg.ontology = 'queryNoderedTest';
msg.queryType = 'SQL';
msg.query = 'Select * from queryNoderedTest where queryNoderedTest.value='+msg.payload.value;
return msg;
In this case the query has been made with select but, as in the static case, you can write a query with update or delete.
You still have to configure the Inject node. In payload, choose the JSON option and enter the data you want to search for. In the Function node, msg.payload.filtro has been written, so when writing the field in Inject it will be done with filtro (Spanish for “filter”). The result would be: Payload: {"filtro":28.6}.
Nodo Inject
Nodo Function
Click on Deploy and execute the flow. The result you get as a payload is an empty array since the lookup value, 28.6, is not included in the data.