-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdatabase-connection.js
99 lines (66 loc) · 2.6 KB
/
database-connection.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// database-connection.js
// Where we connect to Airtable and handle requests from server.js
const Airtable = require('airtable');
const base = new Airtable({
apiKey: process.env.AIRTABLE_API_KEY,
}).base(process.env.AIRTABLE_BASE_ID);
// ^ Configure Airtable using values in 🗝.env
const viewName = 'Grid view';
const tableAI = 'AI in Science Fiction';
const tableProperties = 'Science Fiction Properties';
// ^ These are the tables we'll be reading from Airtable
const Bottleneck = require('bottleneck');
const rateLimiter = new Bottleneck({
minTime: 1050 / 5
}) // ~5 requests per second
// ^ 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:
// https://airtable.com/api
const cache = require('./caching');
// ^ caching.js reads and writes local files
function sendResultWithResponse(result, response) {
response.status(200).end(JSON.stringify(result));
}
function cachePathForRequest(request) {
return '.newcache' + request.path + '.json';
}
module.exports = {
handleAIListRequest: function(request, response) {
var cachePath = cachePathForRequest(request);
var cachedResult = cache.readCacheWithPath(cachePath);
if (cachedResult != null) {
console.log("Cache hit. Returning cached result for " + request.path);
sendResultWithResponse(cachedResult, response);
}
else {
console.log("Cache miss. Loading from Airtable for " + request.path);
var pageNumber = 0;
rateLimiter.wrap(base(tableAI).select({
view: viewName,
pageSize: 3 //This page size is unnecessarily small, for demonstration purposes.
//You should probably use the default of 100 in your own code.
}).eachPage(function page(records, fetchNextPage) {
if (pageNumber == request.params.page) {
var results = [];
records.forEach(function(record) {
var result = {
name: record.get('Name'),
housing: record.get('Housing Type'),
notes: record.get('Notes'),
propertyID: record.get('Property')
}
results.push(result);
});
cache.writeCacheWithPath(cachePath, results);
console.log("Returning records");
sendResultWithResponse(results, response);
} else {
pageNumber++;
fetchNextPage();
}
}, function done(error) {
sendResultWithResponse([], response);
}));
}
}
}