-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathairtable.js
263 lines (231 loc) · 7.26 KB
/
airtable.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* eslint no-restricted-imports: 0 */
/*
THIS IS A GENERATED FILE
Changes might be overwritten in the future, edit with caution!
Helper functions that makes airtable API calls directly
Not meant to be called directly by functions outside of request.js
If you're adding a new function: make sure you add a corresponding test (at least 1) for it in airtable.spec.js
*/
import Airtable from 'REPLACE_IMPORT_LIB';
import { Columns } from './schema';
const BASE_ID = process.env.REACT_APP_AIRTABLE_BASE_ID;
const API_KEY = 'REPLACE_API_KEY';
const ENDPOINT_URL = 'REPLACE_ENDPOINT';
const VIEW = 'REPLACE_VIEW';
Airtable.configure({
endpointUrl: ENDPOINT_URL,
apiKey: API_KEY,
});
const base = Airtable.base(BASE_ID);
// Transformation Utilities
const fromAirtableFormat = (record, table) => {
const columns = Columns[table];
const invalidColumns = [];
if (!columns || columns == undefined) {
console.log(
`WARNING [fromAirtableFormat]: Failed to convert record and get data from Airtable. Could not find table: ${table}. \n\nIf the Airtable schema has recently changed, please run the schema generator and try again.`
);
return;
}
// Invert columns object
const invertedColumns = Object.keys(columns).reduce(
(obj, key) => ({
...obj,
[columns[key].name]: { name: key, type: columns[key].type },
}),
{}
);
// Create a new object mapping each record attribute
return Object.keys(record).reduce((obj, origColumn) => {
const jsFormattedName = invertedColumns[origColumn];
if (!jsFormattedName) {
console.log(
`WARNING [fromAirtableFormat]: Could not fully convert ${table} record from Airtable. Could not find column of name "${origColumn}" in local copy of schema.js. \n\nIf the Airtable schema has recently changed, please run the schema generator and try again.`
);
invalidColumns.push(jsFormattedName);
return obj;
}
let value = record[origColumn];
// Unwrap array if it's a single foreign key relationship
if (jsFormattedName.type === 'foreignKey-one') {
[value] = value; // Array Destructuring
}
return {
...obj,
[jsFormattedName.name]: value,
};
}, {});
};
const toAirtableFormat = (record, table) => {
const columns = Columns[table];
const invalidColumns = [];
if (!columns || columns == undefined) {
console.log(
`WARNING [toAirtableFormat]: Failed to convert record and send data to Airtable. Could not find table: ${table}. \n\nIf the Airtable schema has recently changed, please run the schema generator and try again.`
);
return;
}
return Object.keys(record).reduce((obj, jsFormattedColumnName) => {
const origColumn = columns[jsFormattedColumnName];
if (!origColumn) {
console.log(
`WARNING [toAirtableFormat]: Could not convert ${table} record from Airtable. Could not find column of name "${jsFormattedColumnName}" in local copy of schema.js. \n\nPlease check your "update" and "create" calls and ensure that your column names exist. If that doesn't work, run the schema generator again to update.`
);
invalidColumns.push(origColumn);
return obj;
}
let value = record[jsFormattedColumnName];
if (origColumn.type === 'foreignKey-one') {
value = [value]; // rewrap array if it's a single foreign key relationship
}
return { ...obj, [origColumn.name]: value };
}, {});
};
// ******** CRUD ******** //
// Given a table and a record object, create a record on Airtable.
function createRecord(table, record) {
const transformedRecord = toAirtableFormat(record, table);
return base(table)
.create([{ fields: transformedRecord }])
.then((records) => {
return records[0].getId();
})
.catch((err) => {
throw err;
});
}
function createRecords(table, records) {
const transformedRecords = records.map((record) => ({
fields: toAirtableFormat(record, table),
}));
return base(table)
.create(transformedRecords)
.then((newRecords) => {
return newRecords.map((newRecord) => newRecord.getId());
})
.catch((err) => {
throw err;
});
}
// Given a table, get all records from Airtable
function getAllRecords(table, filterByFormula = '', sort = []) {
return base(table)
.select({
view: VIEW,
filterByFormula,
sort,
})
.all()
.then((records) => {
if (records === null || records.length < 1) {
return [];
}
return records.map((record) => fromAirtableFormat(record.fields, table));
})
.catch((err) => {
throw err;
});
}
// Given a table and record ID, return the associated record object using a Promise.
function getRecordById(table, id) {
return base(table)
.find(id)
.then((record) => {
return fromAirtableFormat(record.fields, table);
})
.catch((err) => {
throw err;
});
}
/*
Given the desired table, field type (column), and field value ('nick wong' or '[email protected]'),
return the associated record object.
NOTE: `fieldValue` is a generic type - values can be a bit tricky. Notably, string type names must be further escaped.
Usage:
- getStoresByStoreName("'A & S Grocery'") --> `{Store Name} = 'A & S Grocery'`
- getProductsByPoints(325) --> `{Points} = 325`
- getStoresByOpen('TRUE()') --> `{Open} = TRUE()`
*/
function getRecordsByAttribute(
table,
fieldType,
fieldValue,
filterByFormula = '',
sort = []
) {
return base(table)
.select({
view: VIEW,
filterByFormula: filterByFormula
? `AND(${filterByFormula}, {${fieldType}}=${fieldValue})`
: `({${fieldType}}="${fieldValue}")`,
sort,
})
.all()
.then((records) => {
if (!records || records.length < 1) {
// No need for this to throw an error, sometimes there're just no values
return [];
}
return records.map((record) => fromAirtableFormat(record.fields, table));
})
.catch((err) => {
throw err;
});
}
// Given a table, a record ID, and an object of fields to update, update a record on Airtable.
function updateRecord(table, id, updatedRecord) {
const transformedRecord = toAirtableFormat(updatedRecord, table);
return base(table)
.update([
{
id,
fields: transformedRecord,
},
])
.then((records) => {
return records[0].id;
})
.catch((err) => {
throw err;
});
}
// Given a table, an array of record IDs, and an array of objects of fields to update, update records on Airtable.
function updateRecords(table, updatedRecords) {
const transformedRecords = updatedRecords.map((updatedRecord) => ({
id: updatedRecord.id,
fields: toAirtableFormat(updatedRecord.fields, table),
}));
return base(table)
.update(transformedRecords)
.then((records) => {
return records[0].id;
})
.catch((err) => {
throw err;
});
}
// Given a table and a record ID, delete a record on Airtable.
function deleteRecord(table, id) {
return base(table)
.destroy([id])
.then((records) => {
return records[0].fields;
})
.catch((err) => {
throw err;
});
}
export {
base,
fromAirtableFormat,
toAirtableFormat,
createRecord,
createRecords,
getAllRecords,
getRecordById,
getRecordsByAttribute,
updateRecord,
updateRecords,
deleteRecord,
};