Running a Query

To run a query, you can call the runQuery method of a client instance with the queryAPI object you to run. In addition to the queryAPI object, you should provide callback function that receives the data and a callback function to handle errors in the query execution.

Example:

/**
 * Run a query.
 *
 * @param {QueryAPI} query The queryAPI object to be executed.
 * @param {function} onUpdate The function to handle data from a successful query execution. This function accepts a data object as its parameter.
 * @param {function} onError The function to handle data from a failed query execution. This function accepts a string containing an error message.
 * @example
 */
client.runQuery(
  query,
  data => {
    // do something with the data array
  },
  error => {
    // log the error to the console
    console.error(error);
  },
);

Try this example on CodeSandbox

It should output the data array to the console.

Accessing the Thread Object

In Zoomdata, a Thread object handles the communication between the backend data source and the front-end client via WebSockets. Sometimes, it is useful to listen to thread events to get more input into the stages of the query execution. To access the thread object, use the run method of the client instance.

Example:

/**
 * Run a query
 *
 * @param {QueryAPI} query The queryAPI object to be executed.
 * @returns {Promise} Promise with Thread object.
 * @example
 */
client.run(query).then(thread => {
  // thread:message event is triggered when new data arrives from the server
  thread.on('thread:message', data => console.log(data));

  // thread:noData event is triggered when the query returns not results
  thread.on('thread:noData', () => console.log('No data results'));

  // thread:notDirtyData event is triggered when all of the data has been received
  thread.on('thread:notDirtyData', () => console.log('Not dirty data'));

  // thread:error event is triggered when there is an error in the communication
  thread.on('thread:error', error =>
    console.error('Handle the error: ', error),
  );
});

Try this example on CodeSandbox

It should output the data array to the console.

Updated Jan 10, 2019