-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (39 loc) · 1.57 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
// Use dotenv to read the .env file and load into process.env
require('dotenv').config()
// Load external dependencies
const Airtable = require('airtable')
// Load helper functions from helpers.js
const { upsertRecordsInChunks } = require('./helpers')
// Define variables and initialize Airtable client
const { AIRTABLE_API_KEY, AIRTABLE_BASE_ID, AIRTABLE_TABLE_ID, AIRTABLE_UNIQUE_FIELD_NAME_OR_ID } = process.env
Airtable.configure({ apiKey: AIRTABLE_API_KEY })
const base = Airtable.base(AIRTABLE_BASE_ID)
const table = base(AIRTABLE_TABLE_ID)
;
(async () => {
// Define input records (from the source system). This would usually be an API call or reading in a CSV or other format of data.
const inputRecords = [
// Existing person in the table, if using the sample data linked to in the README
{
'First Name': 'Juliette',
'Last Name': 'Schimmang',
'Unique ID': '16a05ea5-7bbd-4353-bc25-878a2245835e',
'Job Title': 'Account Executive II'
},
// New user to be added to the table
{
'First Name': 'Marsha',
'Last Name': 'Rickeard',
'Unique ID': 'bf68da9d-805b-4117-90dc-d54eb46db19f',
'Job Title': 'CTO',
'Hire Number': 201
}
]
// Format record objects to match Airtable API format
const recordsToUpsert = inputRecords.map(r => ({ fields: r }))
// Read out array size
console.log(`\nRecords to upsert: ${recordsToUpsert.length}`)
// Perform record upserts
await upsertRecordsInChunks(table, recordsToUpsert, [AIRTABLE_UNIQUE_FIELD_NAME_OR_ID])
console.log('\n\nScript execution complete!')
})()