Node-RED tutorials
Node-RED tutorials for FairCom Edge
Node-RED works natively with the FairCom JSON DB API. It does not require a driver or custom-built nodes. Database capabilities to Node-RED are enabled by sending simple JSON commands to FairCom Edge.
Ensure the FairCom server is installed and running.
Ensure Node-RED is installed.
Section | Description |
---|---|
Create a session with any of the FairCom JSON APIs. An authenticated session must be created before commands can be sent to a FairCom server. | |
Create a table and an index in a FairCom Edge database. | |
Insert and query records in a FairCom Edge database. |
Node-RED works natively with the FairCom JSON DB API. It does not require a driver or custom-built nodes. Database capabilities to Node-RED are enabled by sending simple JSON commands to FairCom Edge
Drag an
https
request node into a flow.Double-click the request node to edit it.
Select POST from the Method dropdown menu.
Enter
https://localhost:8443/api
in the URL textbox.Check the Enable secure (SSL/TLS) connection checkbox.
Create a new TLS Configuration.
Uncheck the Verify server certificate.
Create certificates for your FairCom Edge server and configure them.
Click
to finish configuring the node and to close the node editor.
Drag a json node into the flow.
Connect the output of the
https
request node to the input of the json node.Note
The FairCom Edge server returns info in JSON format. The json node will translate the response into a native JSON object.
Drag an inject node into the flow.
Connect the output of the inject flow to the input of the
https
request node.Double-click the inject node to edit it.
Enter
createSession
in the Name textbox.Enter
{} JSON
in the msg.payload textbox.Click
to open the JSON editor.Paste the following JSON in the JSON editor textbox:
{ "api": "admin", "action": "createSession", "params": { "username": "ADMIN", "password": "ADMIN" } }
Click
to close the JSON editor.Click FairCom Edge server.
so that now the inject node will send a JSON object that requests a new session from theNote
The response will include an authtoken.
Drag a function node into the flow.
Note
We will configure this node to save the authtoken for use in other nodes.
Connect the output of the json node to the input of the function node.
Double-click the function node to edit it.
Paste the following code in the On Message code editor textbox:
var authToken = msg.payload.result.authToken; node.status("Got token:"+authToken); flow.set("tutorial_authToken", authToken); return msg;
Enter
Store Auth Token
in the Name textbox.Click
to finish configuring the node and to close the node editor.Note
This node will parse the JSON response object and grab the returned authtoken then store it in a flow variable called
"tutorial_authToken"
.
Click
to save the flow and make it ready for use.Click the corresponding createSession node button to send the request for a new session to the FairCom Edge server.
Note
The authtoken for this session will be stored in a flow variable. You can inspect the flow variable via the Context Data tab or add a debug node connected to the output of the json node to see it.
Tutorials to use Node-RED to create a session with FairCom Edge
Create table
Drag an inject node into the flow.
Double-click the inject node to edit it.
Enter
createTable
in the Name textbox.Enter
{} JSON
in the msg.payload textbox.Click
to open the JSON editor.Paste the following JSON in the JSON editor textbox:
{ "api": "db", "authToken": "replaceWithRealToken", "action": "createTable", "params": { "databaseName": "ctreeSQL", "tableName": "athlete", "fields": [ { "name": "name", "type": "varchar", "length": 30 }, { "name": "ranking", "type": "smallint", "nullable": false }, { "name": "birthDate", "type": "date" }, { "name": "playerNumber", "type": "number", "length": 32, "scale": 6 }, { "name": "livedPast2000", "type": "bit" }, { "name": "earnings", "type": "money", "length": 32, "scale": 4 }, { "name": "favoriteSaying", "type": "varchar", "length": 500 } ] } }
Click
to close the JSON editor.Click
on the inject node.
Drag a function node into the flow.
Connect the output of the createTable inject node to the input of the function mode.
Double-click the function node to edit it.
Enter
Add auth token
in the Name textbox.Paste the following code in the On Message code editor textbox:
var authToken = flow.get("tutorial_authToken")||"BadToken"; msg.payload.authToken = authToken; return msg;
Click
to finish configuring the node and to close the node editor.Note
This node will now replace the
"authToken"
property of the JSON object it receives with the real auth token stored in a flow variable.
Copy the http request node and paste a duplicate of it into the flow.
Connect the output of the Add auth token node to the input of the request node.
Drag in a JSON node and connect the output from the request node to the input of the JSON node.
Drag a debug node into the flow.
Connect the output of the JSON node to the input of the debug node.
Note
This will send the results of the request to the debug tab where it is easily inspected.
Click
.Click the corresponding createTable node button to send the request.
Observe the results in the debug tab in a JSON payload object and an empty table in the FairCom Edge server.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Create index
Add an Inject node and connect the output of the inject node to the input of the Add auth token node from above.
Enter
createIndex
in the Name textbox.Enter
{} JSON
in the msg.payload textbox.Click
to open the JSON editor.Paste the following JSON in the JSON editor textbox:
{ "api": "db", "authToken": "replaceWithRealToken", "action": "createIndex", "params": { "databaseName": "ctreeSQL", "tableName": "athlete", "indexName": "ranking", "fields": [ { "name": "ranking" } ], "waitToBeLoaded": true } }
Click
to close the JSON editor.Click
on the inject node.Click FairCom Edge server instructing it to add an index to the created table.
to configure the inject node to send a JSON object to theClick the corresponding createIndex node button to send the request.
Observe the results in the debug tab in a response object and a new index on ranking in the created table.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Tutorial to use Node-RED to create a table & index in FairCom Edge
Add Records

Drag an inject node into the flow.
Connect the output of the inject node to the input of the Add auth token node that the createTable node feeds into.
Double-click the inject node to edit it.
Enter
insertRocords
in the Name textbox.Enter
{} JSON
in the msg.payload textbox.Click
to open the JSON editor.Paste the following JSON into the JSON editor textbox:
"api": "db", "authToken": "<valid authToken>", "action": "insertRecords", "params": { "databaseName": "ctreeSQL", "tableName": "athlete", "dataFormat": "objects", "sourceData": [ { "name": "Michael Jordan", "ranking": 1, "birthDate": "19630217", "playerNumber": 23, "livedPast2000": true, "earnings": 1700000000, "favoriteSaying": "There is no 'i' in team but there is in win." }, { "name": "Babe Ruth", "ranking": 2, "birthDate": "18950206", "playerNumber": 3, "livedPast2000": false, "earnings": 800000, "favoriteSaying": "Every strike brings me closer to the next home run." }, { "name": "Muhammad Ali", "ranking": 3, "birthDate": "19420117", "playerNumber": 1, "livedPast2000": true, "earnings": 60000000, "favoriteSaying": "Float like a butterfly, sting like a bee." }, { "name": "Pele", "ranking": 4, "birthDate": "19401023", "playerNumber": 10, "livedPast2000": true, "earnings": 115000000, "favoriteSaying": "Everything is practice." }, { "name": "Wayne Gretzky", "ranking": 5, "birthDate": "19610126", "playerNumber": 99, "livedPast2000": true, "earnings": 1720000, "favoriteSaying": "You miss 100 percent of the shots you never take." }, { "name": "Michael Schumacher", "ranking": 6, "birthDate": "19690103", "playerNumber": 1, "livedPast2000": true, "earnings": 990000000, "favoriteSaying": "Once something is a passion, the motivation is there." } ] } }
Click
to close the JSON editor.Click
to save the inject node.
Click FairCom Edge server instructing it to insert 6 records into the table we created.
to configure the inject node to send a JSON object to theClick the corresponding createIndex node button to send the request.
Observe the results in the debug tab.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
Query data

Drag an inject node into the flow.
Connect the output of the inject node to the input of the Add auth token node that the createTable node feeds into.
Double-click the inject node to edit it.
Enter
getRecordsStartingAtKey
in the Name textbox.Enter
{} JSON
in the msg.payload textbox.Click
to open the JSON editor.Paste the following JSON in the JSON editor textbox:
{ "api": "db", "authToken": "replaceWithRealToken", "action": "getRecordsStartingAtKey", "params": { "databaseName": "ctreeSQL", "tableName": "athlete", "indexFilter": { "indexName": "ranking", "operator": ">=", "indexFields": [ { "fieldName": "ranking", "value": 3 } ] }, "reverseOrder": false } }
Click
to close the JSON editor.Click
to save the inject node.
Click FairCom Edge server instructing it to return records where
to configure the inject node to send a JSON object to the"ranking"
is equal to or greater than three.Click the corresponding createIndex node button to send the request.
Observe the results in the debug tab.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.Click the
"msg.payload"
object in the debug tab.Expand the
"result"
property.Expand the
"data"
property to view the list of four objects (numbered 0-3).Expand the
0
object to view the first row returned by the query.
Tutorial to use Node-RED to add & query records in FairCom Edge
This FairCom tutorial uses HTTP to communicate with the server on port 8080. Because HTTP is insecure, this port is typically disabled.
Do the following to enable the HTTP protocol on port 8080:
Shut down the FairCom server.
Edit the
services.json
file located in the<faircom>/config
folder.Find the listener service named
"http8080"
.Change
"enabled":false
to"enabled":true
.Restart the FairCom server.
The modified listener configuration object in services.json should look something like this:
{
"serviceName": "http8080",
"description": "Port 8080 using insecure HTTP protocol...",
"port": 8080,
"protocol": "http",
"enabled": true
}