fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
45 lines • 2.21 kB
JavaScript
;
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSnapshot = void 0;
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const path_1 = tslib_1.__importDefault(require("path"));
const getStructureDefinition_1 = require("../jsonataFunctions/getStructureDefinition");
const snapshotBuilder_1 = require("../snapshotBuilder");
const stringFuncs = tslib_1.__importStar(require("../stringFunctions"));
const diskCachePath = path_1.default.join('.', 'snapshots');
fs_extra_1.default.ensureDirSync(diskCachePath);
const getSnapshot = async (rootType, fhirVersion, fhirPackageIndex, logger) => {
if (rootType.startsWith('#')) {
return (0, getStructureDefinition_1.getStructureDefinition)(rootType, fhirVersion, fhirPackageIndex, logger);
}
// fetch a snapshot from disk cache or build it and save to cache
const originalDefinitionPath = (0, getStructureDefinition_1.getStructureDefinitionPath)(rootType, fhirVersion, fhirPackageIndex, logger);
if (originalDefinitionPath) {
// found the definition path in index, turn it into a hashkey
const hashKey = stringFuncs.hashKey(originalDefinitionPath);
const snapshotPath = path_1.default.join(diskCachePath, hashKey + '.json');
// check if disk cache has a file with that key
if (fs_extra_1.default.existsSync(snapshotPath)) {
// snapshot exists in disk cache, return it
return JSON.parse(fs_extra_1.default.readFileSync(snapshotPath).toString());
}
else {
// snapshot does not exist in disk cache, generate it
const snapshot = await (0, snapshotBuilder_1.generateSnapshot)(rootType, fhirVersion, fhirPackageIndex, logger);
// then save it
fs_extra_1.default.writeFileSync(snapshotPath, JSON.stringify(snapshot, null, 2));
return snapshot;
}
}
else {
// did not find the definition in index
return undefined;
}
};
exports.getSnapshot = getSnapshot;
//# sourceMappingURL=getSnapshot.js.map