-
Notifications
You must be signed in to change notification settings - Fork 0
/
etl.js
executable file
·59 lines (47 loc) · 1.62 KB
/
etl.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const core = require('@actions/core');
const AIRTABLE = {
domain: 'https://api.airtable.com',
path: '/v0/appQeNGOVlznG2nqu/',
view: 'Grid view',
tables: ['Questions', 'Concepts', 'Lectures', 'Tools', 'Sources'],
key: process.env.AIRTABLE_API_KEY,
}
const dataFolder = '/public/data';
async function getDataRecursive(endpoint, offsetId) {
let originalEndpoint = endpoint;
let offsetableEndpoint = endpoint;
if (offsetId) {
offsetableEndpoint = `${originalEndpoint}&offset=${offsetId}`;
}
console.log(`Pulling from ${endpoint}`);
try {
const { data: { records, offset } } = await axios(offsetableEndpoint, {
headers: {
'Authorization': `Bearer ${AIRTABLE.key}`,
},
});
const normalized = records.map(r => {
return { id: r.id, ...r.fields };
});
if (offset) {
return [...normalized, ...(await getDataRecursive(originalEndpoint, offset))];
};
return normalized;
} catch (e) {
core.setFailed(e);
}
}
// execute and persist data
Promise.all(AIRTABLE.tables.map(table => {
return getDataRecursive(`${AIRTABLE.domain}${AIRTABLE.path}${table}?view=${AIRTABLE.view}`)
.then((data) => {
const pathToData = (ext = '.json') => path.join(__dirname, dataFolder, `${table}`.toLowerCase()) + ext;
// persist data
fs.writeFileSync(path.resolve(pathToData('.json')), JSON.stringify(data, null, 2));
fs.writeFileSync(path.resolve(pathToData('.min.json')), JSON.stringify(data));
})
})).catch(e =>{console.log(e); core.setFailed(e)});