Function Endpoint: How to Use datasourceToBePopulated

Hi all,
I am trying to write a FaaS function that saves it output in a dynamic Apps table using the datasourceToBePopulated attribute. Unfortunatelly, the official documentation is very minimalistic and I am struggeling to do so. I’ve already managed to create a dynamic table and feed it with some initial values. I wrote a FaaS that would return some values that should be populated into this table and defined the Endpoint.
If I test the FaaS response in the Classic OD UI, it looks like

{
  "stdout": "",
  "stderr": "",
  "response": {
    "data": {
      "col1": "test"
    },
    "schema": {
      "name": "col1",
      "type": "STRING",
      "index": 0
    }
  },
  "status": "SUCCESS",
  "error": null
}

The function endpoint is has this entries:

"datasourceToBePopulated": {
      "datasourceId": "datasource1",
      "tableSchemaPath": "response.schema",
      "dataPath": "response.data"
    } 

(I also tried to dismiss the response in the Paths but it did not work).
But I always receive the error Error retrieving from datasource datasource1: Data could not be mapped to schema and the inital values disapper from the dynamic table.

Does anyone know, how to specifiy the tableSchemaPath and dataPath? In which format should the data JSON be build. If you check, for instance, the Pandas.DataFrame.to_dict documentation, you will see, that there are 7 JSON like ways to store a table.

Hi Kai,

We used this feature recently. Basically you need:

  1. Dynamic Datasource:
{
  "id": "dynamic_ds",
  "config": {
    "dataOptions": {
      "sql": "SELECT * AS empty_data AS  FROM inputTable i"
    },
    "initialData": [
      {
        "empty_data": "This is what populates the table before data is fetched from the function."
      }
    ],
    "schema": "table"
  },
  "origin": "dynamic"
}
  1. OD Function with response object in the following format:
{
            "message": "Success message goes here",
            "results": {
                "data": [
                    {
                        "column1": "Test value",
                        "column2": "Test value2"
                    },
                    {
                        "column1": "New value",
                        "column2": "New value2"
                    }
                ],
                "schema": [
                    {
                        "name": "column1",
                        "index": 0,
                        "type": "string"
                     },
                    {
                        "name": "column2",
                        "index": 1,
                        "type": "string"
                     }
                ],
            },
        }
  1. Endpoint to function (notice how the results.schema and results.data match the response object of the function above):
{
  "id": "dynamic_endpoint",
  "async": false,
  "config": {
    "functionId": "function uuid",
    "datasourceToBePopulated": {
      "dataPath": "results.data",
      "datasourceId": "dynamic_ds",
      "tableSchemaPath": "results.schema"
    },
    "payload": {
      "input": {
        <your input args as a dict>
      }
    }
  },
  "type": "function"
}
  1. A button or some element to trigger the endpoint)
          {
            "endpointId": "dynamic_endpoint",
            "type": "executeFunction"
          }
  1. A table to display the data (Note that the column array here needs to match the schema returned from the function. To handle this you could build a column array variable from another datasource, if the columns are different each time, or you can just not specify the column array but won’t have control over their formatting etc.):
{
  "id": "dynamic_table_ele",
  "config": {
    "$columns": {
      "type": "object",
      "value": "{{column_object_var}}"
    },
  },
  "source": "dynamic_ds",
  "type": "table"
}

I hope that helps :slight_smile: If not, you are welcome to give me call.

2 Likes

Thank you for your help, that was exactly the information I needed!