-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairtableClient.js
59 lines (48 loc) · 1.46 KB
/
airtableClient.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
const Airtable = require('airtable');
require('dotenv').config();
const apiKey = process.env.AIRTABLE_API_KEY
const baseName = process.env.AIRTABLE_BASE_NAME
const base = new Airtable({apiKey: apiKey}).base(baseName);
/* Bottleneck, instanced as rateLimiter, allows us to conform to rate limits specified by Airtable's API
Failure to comply with the Airtable rate limit locks down its API for 30 seconds
*/
const Bottleneck = require('bottleneck');
const rateLimiter = new Bottleneck({
minTime: 1000,
maxConcurrent: 1
}) // ~1 requests per second since each of our Airtable calls actually call the api 4 times
async function fetchRecords(query) {
return base('mutual_aid_locations')
.select(query)
.all()
}
async function fetchHours() {
return base('hours_periods_test')
.select()
.all()
}
async function getHours() {
const wrappedAirtableCall = rateLimiter.wrap(fetchHours)
const result = await wrappedAirtableCall()
.catch((error) => {
throw new Error("Error fetching Airtable hours records" + error)
})
return result
}
module.exports = {
getMutualAidSites: async function() {
const field = 'org_name'
const direction = 'asc'
const query = {
sort: [{field, direction}],
filterByFormula: 'inactive = 0'
}
const wrappedAirtableCall = rateLimiter.wrap(fetchRecords);
const result = await wrappedAirtableCall(query)
.catch((error) => {
throw new Error("Error fetching Airtable records" + error)
});
return result
},
getHours: getHours
}