Skip to content

Instantly share code, notes, and snippets.

@Przemocny
Created December 3, 2022 14:29
Show Gist options
  • Save Przemocny/bc76d96395845763ae20403524751dc0 to your computer and use it in GitHub Desktop.
Save Przemocny/bc76d96395845763ae20403524751dc0 to your computer and use it in GitHub Desktop.
airtable.updateMultipleRows.js
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