Created
December 3, 2022 14:29
-
-
Save Przemocny/bc76d96395845763ae20403524751dc0 to your computer and use it in GitHub Desktop.
airtable.updateMultipleRows.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function updateMultipleRows(table, toUpdate = []) { | |
let counter = 0 | |
const MAX_UPDATE_LEN = 50 | |
if (toUpdate.length == 0) { | |
console.log('done without any updates') | |
return; | |
} | |
console.log('rows to update: ', toUpdate.length) | |
const mapping = new Map() | |
toUpdate.forEach(({ id, fields }) => { | |
if (mapping.has(id)) { | |
const _fields = mapping.get(id) | |
mapping.set(id, { | |
..._fields, | |
...fields | |
}) | |
return | |
} | |
mapping.set(id, fields) | |
}) | |
const cleanedUpdatePayload = [...mapping.entries()].map(([id, fields]) => ({ id, fields })) | |
while (counter < cleanedUpdatePayload.length) { | |
const slice = cleanedUpdatePayload.slice(counter, counter + MAX_UPDATE_LEN) | |
await table.updateRecordsAsync(slice) | |
console.log(`rows ${counter} - ${counter + MAX_UPDATE_LEN}`) | |
counter += 50 | |
} | |
console.log('done') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment