Search code examples
google-cloud-sqlgoogle-app-maker

Query as Widget Datasource


I'm trying to define a widget's datasource as the result of a query, but I'm not sure if it's posible.

I'm working with SQL views and a Table, I'd like to show the values of the IDs that I have on the table that come from the views.

function queryValue(source, model, key){
  console.log("source " + source);
  app.datasources[model].query.filters.id._equals = source;
  app.datasources[model].load(function () {
    console.log(app.datasources.users.items[0][key]);
    return app.datasources.users.items[0][key];
  });
  app.datasources[model].query.clearFilters();
}

Calling it like:

queryValue(@datasource.item.[the_id], "[the_SQLView_Datasouce]", "[the_field_i_want]");

In this case the widget is a table, so the fucntion would repeat the amount of items in the talbe

The problem is that either I get the same result as meny times as the amount of items or the first one does not work!

And the second problem is that the result does not over write the widget text to show. enter image description here

It's a very simple function and I did found some workarounds but not with the datasource feature and they work too slowly, any sugestions? Is it possible to do such thing with the datasource?


Solution

  • If I understand the question correctly, you probably want to do the query on the server side. The issue with the sample code posted is it's triggering a load on a single data source multiple times before any of the loads can return. When this is done, the data source is only loaded with the results from one of the loads, I believe the last one. So you're probably seeing the results from the last query you did for all your callbacks.

    So instead your code should instead be a server side script, and should be something like:

    function queryValue(source, model, key){
      console.log("source " + source);
      var query = app.models.newQuery();
      query.filters.id._equals = source;
      var results = query.run;
      return results[0].key;
    }
    

    (Written from memory, so pardon any errors.)