-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (47 loc) · 1.31 KB
/
index.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
const fetch = require("node-fetch");
const mkdirp = require("mkdirp");
const fs = require("fs");
const resources = ["Patient", "Encounter", "Condition", "Procedure"];
const count = 1000;
const fhirBaseUrl =
"http://local.psbrandt.io:9876/omoponfhir-stu3-server/fhir/";
const dataDir = "bundles";
const findNext = links => {
const next = links.find(link => link.relation === "next");
return !next ? next : next.url;
};
const saveResource = resource => {
const type = resource.resourceType;
const id = resource.id;
const dirName = `${dataDir}/${type}`;
mkdirp(dirName, err => {
const filename = `${type}.${id}.json`;
fs.writeFile(
`${dirName}/${filename}`,
JSON.stringify(resource, null, 2),
err => {
if (err) throw err;
console.log(`Wrote: ${dirName}/${filename}`);
}
);
});
};
const extractBundle = bundle => {
saveResource(bundle);
return findNext(bundle.link);
};
const extractResource = async resource => {
let next = `${fhirBaseUrl}/${resource}?_count=${count}`;
do {
console.log(`Fetching ${next}`);
next = await fetch(next)
.then(res => res.json())
.then(bundle => extractBundle(bundle));
} while (next != null);
};
const extract = () => {
resources.forEach(resource => {
extractResource(resource);
});
};
extract();