-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_helper.js
52 lines (48 loc) · 1.51 KB
/
node_helper.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
const NodeHelper = require('node_helper');
const Airtable = require('airtable');
module.exports = NodeHelper.create({
start: function() {
this.airtableBase = null;
console.log(this.name + ' helper started ...');
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'MMM_AIRTABLE_DATA_FETCH') {
const { tableConfig } = payload;
const that = this;
this.initializeAirtableOnce(tableConfig);
this.airtableBase(tableConfig.workspaceName)
.select({
view: 'Grid view',
})
.firstPage((err, records) => {
if (err) {
console.error(err);
return;
}
const allRecords = records.map(record => record.fields);
const tableData = {
columns: Object.keys(allRecords[0]),
rows: allRecords
.map(record => Object.values(record))
.slice(0, tableConfig.maxRows),
};
//console.log({ allRecords });
//console.log({ tableData });
that.sendSocketNotification('MMM_AIRTABLE_DATA_RECEIVED', {
tableConfig,
tableData,
});
});
}
},
initializeAirtableOnce: function(config) {
if (this.airtableBase || !config.airtableAPIKey || !config.airtableBaseId) {
return;
}
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: config.airtableAPIKey,
});
this.airtableBase = Airtable.base(config.airtableBaseId);
},
});