1

Can I run something after every row has been processed by db.each?

var email = openEmailConnection();
db.each("SELECT * FROM mytable", async (err, row) => {
  var x = await makeNetworkApiCall();
  await email.sendMail({message: "your message: " + x});
  console.log("finished");
});
//somehow call closeEmailConnection() after all async callbacks have finished

I don't want to do db.all because that loads everything onto memory. Maybe I could do something with promises and Promise.all?

2 Answers 2

1

Maybe as an alternative, I suggest to use better-sqlite3 instead. Here, you have an iterate() function that allows you to process all entries in a for loop like this (from their docu, slightly adapted):

const email = openEmailConnection();

const stmt = db.prepare('SELECT * FROM mytable');
for (const entry of stmt.iterate()) {
  const x = await makeNetworkApiCall();
  await email.sendMail({message: "your message: " + x});
}

closeEmailConnection();
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you could use an anonymous callback function. I used a similar construction to add rows to an array and returning that array after all callbacks of the rows are completed:

select = (db, tableName) => {
  const rows = [];
  return new Promise((resolve, reject) => {
    const sql = `SELECT * FROM ${tableName}`
    db.each(sql, (err, row) => {
      if (err) {
        console.error(err.message);
      }
      rows.push(row);
    }, () => {
      resolve(rows);
    });
  })
}

For your code this would be something like this:

const email = openEmailConnection();
db.each("SELECT * FROM mytable", async (err, row) => {
  var x = await makeNetworkApiCall();
  await email.sendMail({message: "your message: " + x});
  console.log("finished");
}, () => { 
   closeEmailConnection();
});

1 Comment

This is the correct answer. For reference: github.com/TryGhost/node-sqlite3/wiki/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.