Skip to main content

Add & query records

Use Node-RED to add & query records in FairCom Edge

Abstract

Tutorial to use Node-RED to add & query records in FairCom Edge

Prerequisites:

Add Records

Figure 1. Flow of nodes
Flow of nodes


  1. Drag an inject node into the flow.

  2. Connect the output of the inject node to the input of the Add auth token node that the createTable node feeds into.

  3. Double-click the inject node to edit it.

    1. Enter insertRocords in the Name textbox.

    2. Enter {} JSON in the msg.payload textbox.

    3. Click ... to open the JSON editor.

    4. 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."
            }
          ]
        }
      }
    5. Click Done to close the JSON editor.

    6. Click Done to save the inject node.

  4. Click Deploy to configure the inject node to send a JSON object to the FairCom Edge server instructing it to insert 6 records into the table we created.

  5. Click the corresponding createIndex node button to send the request.

  6. Observe the results in the debug tab.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

Query data

Figure 2. Flow of nodes
Flow of nodes


  1. Drag an inject node into the flow.

  2. Connect the output of the inject node to the input of the Add auth token node that the createTable node feeds into.

  3. Double-click the inject node to edit it.

    1. Enter getRecordsStartingAtKey in the Name textbox.

    2. Enter {} JSON in the msg.payload textbox.

    3. Click ... to open the JSON editor.

    4. 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
          }
      }
      
    5. Click Done to close the JSON editor.

    6. Click Done to save the inject node.

  4. Click Deploy to configure the inject node to send a JSON object to the FairCom Edge server instructing it to return records where "ranking" is equal to or greater than three.

  5. Click the corresponding createIndex node button to send the request.

  6. Observe the results in the debug tab.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.

  7. Click the "msg.payload" object in the debug tab.

  8. Expand the "result" property.

  9. Expand the "data" property to view the list of four objects (numbered 0-3).

  10. Expand the 0 object to view the first row returned by the query.

    QueryData.png