forked from hackclub/airbender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
66 lines (59 loc) · 1.53 KB
/
util.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
const md5 = require('md5')
// to iterate through airtable tables
function forEachInTable(base, tableName, cb) {
return base(tableName)
.select()
.eachPage((records, fetchNextPage) => {
records.forEach(record => {
cb(record)
})
fetchNextPage()
})
}
// more performative forEachInTable
function forEachInFilter(base, tableName, formula, cb) {
const selectOptions = {
filterByFormula: formula
}
if (Array.isArray(formula)) {
selectOptions.filterByFormula = `AND(${formula.join(',')})`
}
return base(tableName)
.select(selectOptions)
.eachPage(async (records, fetchNextPage) => {
records.forEach(cb)
fetchNextPage()
})
}
// to iterate through airtable tables once per airbender cycle.
// for tables with race conditions or rate-limiting
// ex. the SDP airtable takes about 10 seconds to load all the records at once
function findInTable(base, tableName, formula, cb) {
return base(tableName)
.select({
maxRecords: 1,
filterByFormula: formula
})
.firstPage((err, records) => {
if (records[0]) {
cb(records[0])
}
})
}
async function stopwatch(name, func) {
const start = Date.now()
const result = await func()
const duration = Date.now() - start
console.log( '...', name, 'took', duration, 'ms')
return result
}
function hash(obj) {
return md5(JSON.stringify(obj))
}
module.exports = {
forEachInTable: forEachInTable,
forEachInFilter: forEachInFilter,
findInTable: findInTable,
stopwatch: stopwatch,
hash: hash
}