Skip to main content

Create a table & index

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

Abstract

Tutorial to 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.

    6. Click Done on the inject node.

  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 in a JSON node and connect the output from the request node to the input of the JSON node.

  9. Drag a debug node into the flow.

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

  11. Click Deploy.

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

  13. 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 for more information about an error.

Create index

  1. Add an Inject node and connect the output of the inject node to the input of the Add auth token node from above.

  2. Enter createIndex in the Name textbox.

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

  4. Click ... to open the JSON editor.

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

  7. Click Done on the inject node.

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

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

  10. 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 for more information about an error.