-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathairtable_helpers.js
36 lines (33 loc) · 1.51 KB
/
airtable_helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Helper function from https://stackoverflow.com/questions/8495687/split-array-into-chunks
const chunkArray = function (arrayToChunk, chunkSize = 10) {
const arraysOfChunks = []
for (let i = 0; i < arrayToChunk.length; i += chunkSize) { arraysOfChunks.push(arrayToChunk.slice(i, i + chunkSize)) }
return arraysOfChunks
}
// Helper function to upsert a chunk of records
// method="PUT" -> upsert (insert-or-update) records, will clear all unspecified cell values
// method="PATCH" -> upsert (insert-or-update) records, will only update the fields you specify, leaving the rest as they were
const upsertRecordsInChunks = async function (table, records, fieldsToMergeOn) {
console.log(`\nupsert'ing ${records.length} records`)
const arrayOfChunks = chunkArray(records)
for (const chunkOfRecords of arrayOfChunks) {
console.log(`\tProcessing batch of ${chunkOfRecords.length} records`)
try {
await table.update(chunkOfRecords, { performUpsert: { fieldsToMergeOn }, typecast: true })
} catch (error) {
console.error(`Error occurred while upserting records: ${error.message}`)
}
}
}
// Helper function that takes an array of records and returns a mapping of primary field to record ID
const createMappingOfUniqueFieldToRecordId = function (records, fieldName) {
const mapping = {}
for (const existingRecord of records) {
mapping[existingRecord.fields[fieldName]] = existingRecord.id
}
return mapping
}
module.exports = {
upsertRecordsInChunks,
createMappingOfUniqueFieldToRecordId
}