Is it possible to query the data using custom JS?

Hello folks,

I’m wondering if there’s a way to use custom JS to query a dataset in a connection?
This would be independent of the query for the widget itself. It would be a separate query.

For example, I’d like to write a query to get the distinct values of a field and display it as a popup in the filter section. So if a user mouses over “Active”, they will see a list of all distinct values that we are labeling as “Active” by running this query on the back and returning this list of values.

Is this possible?

@fernie You can call the Data API end point using JS.

Below is sample custom style that calls the API using API key.

    var f = function() {};
    f.version = "1";
    f.afterDraw = function() {
        var url = "http://10.0.0.5:38888/arc/api/data"
        var apikey = "Kasdf132qL5iYh0Sgep7mv591eIwbd0KN26SB5Sc9VRRuh"

        var dsreq = {
          "version":1,"type":"SQL","limit":50,
          "dimensions":[
            {"type":"SIMPLE","expr":"[manufacturer] as 'manufacturer'"}],
          "aggregates":[
            {"expr":"sum([sodium_mg]) as 'sum(sodium_mg)'"},
            {"expr":"avg([fat_grams]) as 'avg(fat_grams)'"},
            {"expr":"sum(1) as 'Record Count'"}],
          "filters":["[cold_or_hot] in ('C')"],
          "dataset_id":10
        };
        
        var payload = {
          'version': 1,
          'dsreq': JSON.stringify(dsreq),
        };
        
       // Call the API
       
        $.ajax({
            url: url,
            data: payload,
            type: 'POST',
            beforeSend: function (xhr) {
                xhr.setRequestHeader("X-CSRFToken", arc.globals.csrf_token);
                xhr.setRequestHeader("Authorization", 'apikey ' + apikey);
            }
        }).done(function(response) {
            var colNames = response["colnames"]
            var columns = []
            colNames.forEach(function(column) {
              columns.push({"title": column})
            })
            
           // "response" contains the results of the query.
 
            $('#example').DataTable({
              data: response["rows"],
              columns: columns
            });
            
            var params = {
              "request_url": url,
              "request_data": JSON.stringify(payload),
            };
            
            arcapi.sendParameters(params)
        });
        
        arcapi.addStyles([
          "https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css"
        ]);
    };

    return f;
}();