1
\$\begingroup\$

Monitors DB2 for changes in table data. It doesn't take any actions with the data just yet, only checks to see if new data is available every 2 seconds.

const { dbconn, dbstmt } = require('idb-connector');// require idb-connector

// global for storing row data in 
var data = ''

async function queryDB() {
    const sSql = `SELECT * FROM LIBNAME.TABLE`;

    // create new promise
    let promise = new Promise ( function(resolve, reject) {
        // create new connection 
        const connection = new dbconn();
        connection.conn("*LOCAL");
        const statement = new dbstmt(connection);
        statement.exec(sSql, (rows, err) => {
          if (err) {
            throw err;
          }       

          // check if data is new
          if (data === rows) {
            resolve('no new data')
          } else {
            data = rows;
            resolve('new data arrived');
          };

          // close DB connections
          statement.close();
          connection.disconn();
          connection.close();
        })
    });

    let result = await promise;// await promise
    return result;
};

async function checkDB2() {
    const status = await queryDB();// check for new data

    if (status === 'new data arrived') {
        // new data action
    };

    setTimeout(checkDB2, 2000);// check again in 2 seconds
};

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.