[]
        
(Showing Draft Content)

Connect to a Javascript Data Provider

Connect Using a Javascript Custom Connector

If you have a Javascript custom data source, click the icon to create a Wyn data source.


JSTypicodeDataSource

Connection String

Give your data source a suitable name from the API you are using as a data source.


The connection string for the data source is a list of properties for the Javascript engine that will be used to interpret your Javascript queries later. It consists of four properties but the engine property has to be Jint as it is the only supported Javascript engine for now.

  • Engine (Required): Currently, the Javascript custom provider only supports the Jint script engine.

  • LimitMemory (Optional): Limits the memory allocation to a given amount in MB. The default is 100MB.

  • TimeoutInterval (Optional): The timeout interval in seconds. The default is 30 seconds.

  • MaxStatements (Optional): The maximum number of script statements that can be executed. The default is 2000.

Here's an example connection string and a screenshot:

    Engine = Jint;
    LimitMemory = 100;
    TimeoutInterval = 30;
    MaxStatements = 2000;

In the example in the screenshot below, we use Typicode to fetch random blog post data. So we called the data source "Typicode Posts Data Source"


JSTypicodeDataSource

Test the connection.

Create a Native Query Dataset

The next step after creating a data source is to create a dataset. In the Resource Portal, navigate to Create (+) > Create Data Source and choose Prepare Data under DASHBOARD or REPORT and choose Native Query Dataset

Choose the Data Source you created earlier from the pulldown and write the text of a Javascript snippet in the Query textbox.

Here we use a snippet that fetches the blog posts from the API and adds them to a predefined variable called resultset. The variables and functions that could be used in the query text are explained below.

    resultset.schema({
        userId: 'integer',
        id: 'integer',
        title: 'string',
        body: 'string'
    });

    var response = helper.fetch(
        'https://jsonplaceholder.typicode.com/posts');
    JSON.parse(response.body).forEach(
        r => resultset.row(r));

Press Validate to make sure your query does not have syntax errors. If it works, preview the results by pressing the Preview button and then Save the query and give it a name.

Command text pattern

The command text is a Javascript code snippet that should be written in pure Javascript as browser APIs (Functions like fetch()) are not available. The Wyn custom Javascript provider, however, has predefined objects to help you construct the result set:

  • resultset: where you define the schema object property.

  • helper object that has a function (fetch)

variable: resultset

Sample code setting resultset schema:

    resultset.schema({
        column1: 'string',
        column2: 'integer'
    });

The available data types are string, integer, double, decimal, boolean, and datetime. Invalid data types will be treated asstring.


Sample code adding a single row to resultset:

    resultset.row(['value1', 123]);

or

    resultset.row({column1:'value1', column2:123});

If the parameter is an array, the order of the values should match the order of the columns in the schema.


The array length can be less or greater than the column count, in which case the mismatched columns will be filled with null values.


If the parameter is an object, the keys should match the column names in the schema. The object can contain more or fewer keys than the column count, in which case the mismatched columns will be filled with null values.

variable: helper

As the most common use case is to fetch data via RESTful API, this provider injects a helper object that has a fetch function.


Sample code fetching data from RESTful API:

var response =
helper.fetch('https://jsonplaceholder.typicode.com/posts');

or

var response =
    helper.fetch(
        'https://jsonplaceholder.typicode.com/posts', {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
        }
    });

This fetch() function is different from the popular browser fetch() function. It does not return a promise. Instead, it returns a response object.


You can access the following properties of the response object:

  • status: Integer. The HTTP status code.

  • statusText: String. The HTTP status text.

  • headers: Object. The response headers.

  • body: String. The literal raw response body.

Data type conversion

The types defined in the schema are used to convert the values in the row.


If the value cannot be converted to the specified type, an exception will be thrown.