Skip to main content

Use Node-RED to create a table & index in FairCom Edge

Prerequisites:

Create table

  1. Drag an inject node into the flow.

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

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

  3. Drag a function node into the flow.

  4. Connect the output of the createTable inject node to the input of the function mode.

  5. Double-click the function node to edit it.

    1. Enter Add auth token in the Name textbox.

    2. Paste the following code in the On Message code editor textbox:

      var authToken = flow.get("tutorial_authToken")||"BadToken";
      msg.payload.authToken = authToken;
      return msg;
      
    3. Click Done 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.

  6. Copy the http request node and paste a duplicate of it into the flow.

  7. Connect the output of the Add auth token node to the input of the request node.

  8. Drag a debug node into the flow.

  9. 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.

  10. Click the corresponding createTable node button to send the request.

  11. 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 of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.

Create index

  1. Enter createIndex 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": "createIndex",
        "params": {
            "databaseName": "ctreeSQL",
            "tableName": "athlete",
            "indexName": "ranking",
            "fields": [
                {
                    "name": "ranking"
                }
            ],
            "waitToBeLoaded": true
        }
    }
    
  5. Click Done to close the JSON editor.

  6. Click Deploy to configure the inject node to send a JSON object to the FairCom Edge server instructing it to add an index to the created table.

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

  8. 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 of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.