0

I have an object that looks like this:

let queries = [
  {
    name: "checkUsers",
    query: 'select * from users where inactive = 1'
  },
  {
    name: "checkSubscriptions",
    query: 'select * from subscriptions where invalid = 1'
  }
]

I am making an AWS Lambda function that will iterate these queries, and if any of them returns a value, I will send an email.

I have come up with this pseudo code:

for (let prop in queries) {  

  const result = await mysqlConnector.runQuery(prop.query).catch(async error => {
    // handle error in query    
  });

  if (result.length < 0){
    // send email
  }
}

return;

I am wondering is this ideal approach? I need to iterate all the object queries.

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • Just for clarification, do you want to send mail every time if a response returns? or just when one of the response return first? – Sándor Bakos Feb 17 '20 at 08:58
  • For all queries that return something, I will send the email. Others will be ignored. – Amiga500 Feb 17 '20 at 09:01
  • I would map the "queries" array's object into request promises, and would use the promise.all with a little workaround, or if the lambda node runtime is 12.9 or higher I would use promise.allSettled. – Sándor Bakos Feb 17 '20 at 09:08

1 Answers1

1

I don't see anything wrong with what you are trying to achieve but there are few changes you could do

  • Try to use Promise.all if you can. This will speed up the overall process as things will execute in parallel. It will depend on number of queries as well.

  • Try leverage executing multiple statements in one query. This way you will make one call and then you can add the logic to identify. Check here

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35