-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.js
57 lines (49 loc) · 1.39 KB
/
writer.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
const Airtable = require('airtable');
const { AIRTABLE } = require('./config');
const coursesRaw = require('./courses.json');
Airtable.configure({
endpointUrl: 'https://api.airtable.com',
apiKey: AIRTABLE.apiKey,
});
const base = Airtable.base(AIRTABLE.tableId);
const ITEMS_PER_PAGE = 10;
const prepareCourses = (rawData = []) =>
rawData.map((dataRow) => ({
fields: {
Course: dataRow.title,
Link: dataRow.url,
Author: dataRow.author,
'Author From': dataRow.authorFrom,
Length: dataRow.length,
Status: 'Backlog'
}
}));
const createCourseRecords = (courses) => new Promise((resolve, reject) => {
base('List').create(courses, (err, records) => {
if (err) {
console.error(err);
reject(err);
}
console.log(`
Successfully written from: "${records[0].get('Course')}" to "${records[records.length - 1].get('Course')}"
`);
resolve();
});
})
const main = async () => {
const preparedCourses = prepareCourses(coursesRaw);
const coursesCount = preparedCourses.length;
const num = ITEMS_PER_PAGE;
for (let startIdx = 0; startIdx < coursesCount; startIdx += num) {
const endIdx = Math.min(startIdx + num, coursesCount);
const dataSlice = preparedCourses.slice(startIdx, endIdx);
await createCourseRecords(dataSlice);
}
};
(async () => {
try {
await main();
} catch (err) {
console.error(err)
}
})();