Query redshift cluster using NodeJs asynchronously

Viewed 786

My task is to copy few redshift tables from cluster one to a new cluster. For this I am writing a script in nodejs. I am using aws-sdk RedshiftData api to fetch the data. I have two separate queries which I want to run in parallel. Following is my code

class syncRedShiftNodes {
    
    constructor(){ ... }
    
    readDataOne(){
        let newSqlQuery = `select * from ${this.tableName} limit 10`;
        const params = {
          ClusterIdentifier: clusterIdentifier,
          Sql: newSqlQuery,
          Database: database,
          DbUser: dbUser
      };
      return new Promise((resolve, reject)=>{
        return awsRedshift.executeStatement(params, function(err, res){
                if (err) console.log(err, err.stack); // an error occurred
                else{
                    return awsRedshift.getStatementResult({Id:res.Id}, function(error, data){
                        if (error) console.log(error, error.stack); // an error occurred
                        else  return data;
                    });
                }
            });
      });
    }

   readDataTwo(){ ...//identical to above function except the query }

   main(){
       return Promise.all([this.readDataOne(), this.readDataTwo()])
         .spread((data1, data2)=>{
            console.log("promise resolved!!");
            return true;
   }
}

The problem is that my code is never reaching the "promise resolved" log. If I put a log in the callback of the redshift getStatementResult, that is being printed correctly but my handle is never reaching the promise.all().then statement which I am not able to understand why so.

Another question I had in mind was is it a good practice to use such a pattern inside a class?

1 Answers

You didn't resolve or reject your promise inside the class.

Example below

class syncRedShiftNodes {
  constructor() {}

  readDataOne() {
    let newSqlQuery = `select * from ${this.tableName} limit 10`;
    const params = {
      ClusterIdentifier: clusterIdentifier,
      Sql: newSqlQuery,
      Database: database,
      DbUser: dbUser,
    };
    return new Promise((resolve, reject) => {
      awsRedshift.executeStatement(params, function (err, res) {
        if (err) {
          console.log(err, err.stack);
          reject(err);
        } else {
          awsRedshift.getStatementResult(
            { Id: res.Id },
            function (error, data) {
              if (error) {
                console.log(error, error.stack);
                reject(error);
              } else {
                resolve(data);
              }
            }
          );
        }
      });
    });
  }

  readDataTwo() {}

  async main() {
    try {
      const result = await Promise.all([
        this.readDataOne(),
        this.readDataTwo(),
      ]);
      return result;
    } catch (err) {
      console.log(err);
    }
  }
}
Related