-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.ts
81 lines (69 loc) · 2.78 KB
/
migration.ts
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
import axios from "axios";
import { SymptomModel } from "./schema/models/symptoms";
import convert from "xml-js";
import logger from "./config/loggerconfig";
import { DiseaseModel } from "./schema/models/disease";
export const migration = async () => {
const sourceUrl = "http://www.orphadata.org/data/xml/en_product4.xml"
logger.info("running Startup Script");
const res = await axios.get(sourceUrl);
const convertedData: any = convert.xml2js(res.data, {
compact: true,
});
logger.info(`Res length ${JSON.stringify(convertedData).length}`);
const symptomsList: any = [];
const existingSymptoms = await SymptomModel.find({});
const insertDataInDb =
convertedData.JDBOR.HPODisorderSetStatusList.HPODisorderSetStatus.map(
async (disease: any, id: number) => {
logger.info(
`Disease Name:-${JSON.stringify(disease.Disorder.Name._text)}`
);
// Insert Symptoms in master table
const processHPOs = await Promise.all(
disease.Disorder.HPODisorderAssociationList.HPODisorderAssociation.map(
async (HPOData: any) => {
logger.info(`HPO Data:--${HPOData.HPO.HPOId._text}`);
const findExistingSymptom = symptomsList.filter(
(symptom: any) => symptom.hpoId === HPOData.HPO.HPOId._text
);
if (!findExistingSymptom.length) {
const filterExistingSymptoms =
existingSymptoms &&
existingSymptoms.filter(
(symptom: any) => symptom.hpoId === HPOData.HPO.HPOId._text
);
if (!filterExistingSymptoms?.length) {
symptomsList.push({
hpoId: HPOData.HPO.HPOId._text,
hpoTerm: HPOData.HPO.HPOTerm._text,
});
}
}
return {
hpoId: HPOData.HPO.HPOId._text,
hpoTerm: HPOData.HPO.HPOTerm._text,
hpoFrequency: HPOData.HPOFrequency.Name._text,
hpoFrequencyTerm: HPOData.HPOFrequency.Name._text
.split("(")[0]
.trim(),
};
}
)
);
const diseaseData = {
orphaCode: disease.Disorder.OrphaCode._text,
diseaseName: disease.Disorder.Name._text,
disorderType: disease.Disorder.DisorderType.Name._text,
disorderGroup: disease.Disorder.DisorderGroup.Name._text,
HPODisorderAssociation: processHPOs,
};
console.log("ProcessHPOs", processHPOs.length);
await DiseaseModel.create(diseaseData);
logger.info("New Disease is created");
return null;
}
);
await SymptomModel.insertMany(symptomsList);
await Promise.all(insertDataInDb);
};