'MySQL - Upsert multiple rows at once (INSERT INTO ON DUPLICATE KEY UPDATE) with NodeJS

I'm trying to upsert (insert or update) about 500 rows in one query.
I started with a REPLACE INTO query which works fine:

function getSQLRows(withPrimaryKey) {
    return [rows.map(item => {
        return withPrimaryKey
           ? [id, item.timestamp, item.duration, item.sum]
           : [item.duration, item.sum];
   })]
}

await promisePool.query(
        'REPLACE INTO earnings (id, timestamp, duration, sum) VALUES ?', getSQLRows(true)
    );

However, I prefer an ON DUPLICATE KEY UPDATE to minimize DB writes I/O. I tried this but as far as I understand it's not possible to use the VALUES syntax in the second row:

await promisePool.query(
        'INSERT INTO earnings (id, timestamp, duration, sum) VALUES ? ' +
        'ON DUPLICATE KEY UPDATE (duration, sum) VALUES ?', getSQLRows(true), getSQLRows(false)
    );

I saw that I can use the ALIAS syntax starting with MySQL 8:

await promisePool.query(
        'INSERT INTO earnings (id, timestamp, duration, sum) VALUES ? AS row' +
        'ON DUPLICATE KEY UPDATE duration = ?, sum = ?', ...
    );

But then how can I use the multiple rows syntax (array of arrays) to insert multiple rows at once? Is that even possible?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source