EN | ES
Sometimes, to create a gadget template that meets our needs, we need to be able to access more than one datasource. For this purpose, some methods have been implemented:
vm.getDataFromDataSource(datasource,callbackFunction,filters,group,project,sort,limit,offset,param,debug)
First three params are available in versions older than empire but the others are new in this version. Every operation is applied in server pushing down the logic to the database.
datasource: identifier of the datasource that we want to access. Bear in mind that they will only return data to which the user has access. If we want to show a public dashboard, then the ontologies from which the datasources are formed must also be marked as public
callbackFunction: function that will return the data after the query. When defining it, you only need a parameter that will be the one that returns the data to us.
filters: we can pass an array of filters for the data. An example of filters would be this one, where we pass two: [{field: f.field1,op: "=",exp: f.value1},{field: f.field2,op: "=",exp: f.value2}], where field is the path of the datasource parameter, for example 'Helsinki.year', op is the operator and can be =,>, <, ... and exp is the value.
IN filter example:
If we have an array of strings with the elements by which we want to filter, we can create an array that contains all the filters in case we use more than one and then pass this variable to the query, for example:
var SelectedItems = ['item1', item2 ',' item4 ']; var filters = []; filters.push ({field: 'fieldOfTheEntityByFilter', op: "IN", exp: "('" + selectedelements.join ("', '") + "')"}); In the case that they were numerical values, we would not put the single quotes. vm.from (datasource) .filters (filters) .exec ...
group: array of strings for the grouped fields in the datasource, this fields must be in project also. When some of the next params are required can be set with the default value []
project: is the project applied to the datasource. The structure is an array of json with two fields {"field":"field/projectop","alias":"alias"} defining each projection with it alias (not required) for example [{"field":"Restaurant.cuisine"}] ó [{"field":"Restaurant.cuisine","alias":"cui"}]. When there are also group fields, they must be here. When some of the next params are required can be set with the default value []
sort: is the sort operation applied to the datasource compose by an array of json with two fields {"field":"fieldtosort","asc":true/false} for example, some valid descending sort is [{"field":"Restaurant.cuisine","asc":false}]. When there are also group fields, they must be here. When some of the next params are required can be set with the default value []
offset: means the offset applied to the datasource, is an integer value. When some of the next params are required can be set with the default value -1
limit: means the limit applied to the datasource, is an integer value. When some of the next params are required can be set with the default value -1
param: it can be posible to use some params in the datasource, for example, overwrite inside some subquery some value for optimization. This param enable sql-inyection so you need to use it in controlled way. Param in the datasource is defined as {$param} for example:
It's compose with an array of json with two fields {"field":"paramtoverwrite","value":"value"} for example: [{"field":"cdest","value":"United States"}]
debug: only enabled in development enviroment (for visualizing must be disable), default value is false. When you enable it, dashboardengine return the generated query instead of the data returned of it execution
We will show an example of use:
We initially create two datasources, one with the HelsinkiPopulation ontology and another one with the Restaurants ontology
In a dashboard, we will create a gadget template.
We access the gadget edition
and we write in the HTML text box:
<!-- Write your HTML <div></div> and CSS <style></style> here --> <!--Focus here and F11 to full screen editor--> <h2>HELSINKIDATA </h2> {{vm.helsinkiData}} <br><h2>RESTAURANTSDATA </h2> {{vm.restaurantsData}}
in the JAVASCRIPT text box:
//Write your controller (JS code) code here //Focus here and F11 to full screen editor //This function will be call once to init components vm.initLiveComponent = function(){ vm.getDataFromDataSource('helsinki',vm.callbackFunctionHelsinki,[{field:'Helsinki.year',op:'=',exp:'2000'}]); vm.getDataFromDataSource('restaurants',vm.callbackFunctionRestaurants); }; vm.callbackFunctionHelsinki = function (data){ vm.helsinkiData = data; console.log(vm.helsinkiData); } vm.callbackFunctionRestaurants = function (data){ vm.restaurantsData = data; console.log(vm.restaurantsData); } //This function will be call when data change. On first execution oldData will be null vm.drawLiveComponent = function(newData, oldData){ }; //This function will be call on element resize vm.resizeEvent = function(){ } //This function will be call when element is destroyed vm.destroyLiveComponent = function(){ };
If you want to pass in the filter a string, then you can pass it by escaping the quotes this way exp: '\' TEXT \ ''
To make a filter on dates we can use this format: [{field: 'DATE', op: '<=' , exp: "TIMESTAMP('2011-01-02T00:00:00.000Z')"}]
Datasource Api Chaining and promise operation (>= Empire version):
Since this version, there is a new API for calling datasources, base on promises. From any template the following operations are availables:
vm.get('datasource',params): id of datasource is the only required. Params is a structure embebbing al operations for example:
{"limit":3,"offset":4}
A promise is returned by this metod:
vm.get( "DsRawRestaurants" ).then( data => console.log(data) ) |
or (for backward compatibility)
vm.get( "DsRawRestaurants" ).then( function(data){ console.log(data) } ) |
vm.getOne('datasource'): syntactic sugar for "vm.get(datasourcename,{"limit":1}) for only one value
vm.from('datasource')...: with this function we use the Api Chaining of the datasources returning also a promise, structure is the following:
vm.from(datasource).(chaining operations).exec();
The "chaining operations" are all the operations of the datasources and they are used for composing the params structure is a easy way. Operations needing some array params can be compose with the params array and also adding many steps of the same type. The operation order doesn't matter, only required order is for the origin from and the end step "exec/execute". For example:
vm.from(vm.datasource.identification).filter(getDataStatusFilters()).group(vm.getGroupFields()).select(vm.getSelectFields()).sort(vm.getSortFields()).exec();
Allowed "chaining operations" are the following : from, where (filter), offset (skip), limit(max), group, project(select), sort, param, debug, exec (execute)
vm.from("restaurants") .project([{"field":"Restaurant.cuisine","alias":"cui"},{"field":"Restaurant.cuisine","alias":"coun","op":"count"}]) .group("Restaurant.cuisine") //or you can put several like ["Restaurant.cuisine","Restaurant.borough"] .exec() .then( function(data){ vm.data = data; });