-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
38 lines (36 loc) · 1.04 KB
/
index.ts
File metadata and controls
38 lines (36 loc) · 1.04 KB
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
import fetchPubMedJson from "./src/fetch-pub-med-json";
import batchLoadEs from "./src/batch-load-es";
interface S3ToDbRequest {
db?: string;
startId: number;
endId: number;
}
export const handler = (
event: any,
context: any,
callback: (err: Error, result?: any) => void,
) => {
if (event.Records.length !== 1) {
console.error("SNS event has more than one record!");
callback(new Error("Illegal number of records; must be exactly 1"));
return;
}
const payload: S3ToDbRequest = JSON.parse(event.Records[0].Sns.Message);
let done = Promise.resolve();
for (let currentId = payload.startId; currentId <= payload.endId; currentId++) {
done = done.then(() => {
return fetchPubMedJson(currentId);
}).then((articles: any[]) => {
console.log("Bulk uploading PubMed article set ID: " + currentId);
return batchLoadEs(articles);
});
}
done.then(values => {
console.log("All PubMed objects processed");
callback(null, {
isOk: true,
});
}).catch(error => {
callback(error);
});
};