Skip to content

Commit

Permalink
fix(resources-generator): mongoose circular dependency warnings
Browse files Browse the repository at this point in the history
- `module.exporst.${name}` instead of `module.exports`
- add `allTypeSchemaTopDef` to export all FHIR types initial schema header
- add `FHIRDataTypesSchemaExport` to merge all circular FHIR types'
- get importLibs processing to function
- check and generate may remove?
  • Loading branch information
Chinlinlee committed Jan 26, 2022
1 parent 4b97705 commit d02a4ba
Show file tree
Hide file tree
Showing 516 changed files with 26,693 additions and 864 deletions.
108 changes: 48 additions & 60 deletions FHIR-mongoose-Models-Generator/resourceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ function fixChoiceTypeOfDate (fieldName, type) {
};
}

/**
* Parse fhir.schema (JSON Standard Schema) to Mongoose Schema
* @param {*} resource
* @param {*} name
* @returns
*/
function getSchema (resource , name) {
//let skipCol = ["resourceType" , "id" , "meta" ,"implicitRules" ,"language" , "text" ,"contained" , "extension" , "modifierExtension"];
let skipCol = ["id" , "resourceType" , "contained"];
Expand Down Expand Up @@ -126,7 +132,6 @@ function getSchema (resource , name) {
} else {
if (choiceTypeDate.yes) type = choiceTypeDate.type;
if (isPrimitiveType(type)) {
console.log(i, type);
result[i] = type;
} else {
result[i] = {
Expand All @@ -146,28 +151,47 @@ function getSchema (resource , name) {
return result;
}

async function generateSchema (type) {
let schema = getSchema(FHIRJson[type] , type);
cleanChildSchema(schema);
function getImportLibs(schema) {
let importLib = "const mongoose = require('mongoose');\r\n";
let importedTypeLib = [];
let cleanType = "";
for (let i in schema) {
let item = schema[i];
if (_.get(item , "type")) {
item.default = "void 0";
let cleanType = item.type.replace(/[\[\]]/gm , '');
if (skipFieldTypes.indexOf(cleanType) < 0 && !importedTypeLib.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('./${cleanType}');\r\n`;
importedTypeLib.push(cleanType);
}
cleanType = item.type.replace(/[\[\]]/gm , '');
} else {
let cleanType = item.replace(/[\[\]]/gm , '');
if (skipFieldTypes.indexOf(cleanType) < 0 && !importedTypeLib.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('./${cleanType}');\r\n`;
importedTypeLib.push(cleanType);
cleanType = item.replace(/[\[\]]/gm , '');
}
if (skipFieldTypes.indexOf(cleanType) < 0 && !importedTypeLib.includes(cleanType)) {
if (isPrimitiveType(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('../FHIRDataTypesSchema/${cleanType}');\r\n`;
} else {
importLib =`${importLib}const {${cleanType}} = require('../FHIRDataTypesSchemaExport/FHIRDataTypesSchemaExport');\r\n`;
}
importedTypeLib.push(cleanType);
}
}
return importLib;
}

function generateBackBoneElement(item) {
let isBackBone = true;
for (let key in DataTypesSummary) {
if (DataTypesSummary[key].includes(item)) {
isBackBone = false;
break;
}
}
if (isBackBone && item.includes("_")) {
console.log("back bone element type:" , item);
generateSchema(item);
}
}
async function generateSchema (type) {
let schema = getSchema(FHIRJson[type] , type);
cleanChildSchema(schema);
let importLibs = getImportLibs(schema);
let schemaStr = JSON.stringify(schema , null , 4).replace(/\"/gm , '')
let code = `module.exports = new mongoose.Schema (${schemaStr.replace(/\\/gm , '"')} , {
_id : false ,
Expand All @@ -176,7 +200,7 @@ async function generateSchema (type) {
getters: true
}
});`;
code = `${importLib}${code}`;
code = `${importLibs}${code}`;
fs.writeFileSync(`./models/mongodb/FHIRDataTypesSchema/${type}.js` , beautify(code , {indent_size : 4 ,pace_in_empty_paren: true }));
for (let i in schema) {
try {
Expand All @@ -187,17 +211,7 @@ async function generateSchema (type) {
deepItem = String(deepItem)
deepItem = deepItem.replace(/[\[\]]/gm , '');
if (!checkHaveSchema(deepItem) && isFHIRSchema(deepItem)) {
let isBackBone = true;
for (let key in DataTypesSummary) {
if (DataTypesSummary[key].includes(deepItem)) {
isBackBone = false;
break;
}
}
if (isBackBone && deepItem.includes("_")) {
console.log("type:" , deepItem);
generateSchema(deepItem);
}
generateBackBoneElement(deepItem);
}
}
}
Expand All @@ -216,17 +230,7 @@ function generateResourceSchema (type) {
for (let i in result) {
if (_.get(result[i] , "type")) {
let cleanType = result[i].type.replace(/[\[\]]/gm , '');
let isBackBone = true;
for (let key in DataTypesSummary) {
if (DataTypesSummary[key].includes(cleanType)) {
isBackBone = false;
break;
}
}
if (isBackBone && cleanType.includes("_")) {
console.log("type:" , cleanType);
generateSchema(cleanType);
}
generateBackBoneElement(cleanType);
result[i].default = "void 0";
}
}
Expand All @@ -248,7 +252,7 @@ function generateResourceSchema (type) {
_.set(result, "myCollection", tempCollectionField);
}

let importLib = "const mongoose = require('mongoose');\r\nconst moment = require('moment');\r\nconst _ = require('lodash');\r\n";
// let importLib = "const mongoose = require('mongoose');\r\nconst moment = require('moment');\r\nconst _ = require('lodash');\r\n";
let code = `module.exports = function () {
require('mongoose-schema-jsonschema')(mongoose);
const ${type} = ${JSON.stringify(result , null , 4).replace(/\"/gm , '').replace(/\\/gm , '"')};\r\n
Expand Down Expand Up @@ -409,31 +413,15 @@ function generateResourceSchema (type) {
const ${type}Model = mongoose.model("${type}" , ${type}Schema , "${type}");
return ${type}Model;\r\n}`;

let importedTypeLib = [];
for (let i in result) {
let item = result[i];
if (_.get(item , "type")) {
item.default = "void 0";
let cleanType = item.type.replace(/[\[\]]/gm , '');
if (!importedTypeLib.includes(cleanType) && !skipFieldTypes.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('${config.requirePath}/${cleanType}');\r\n`;
importedTypeLib.push(cleanType);
}
} else {
let cleanType = item.replace(/[\[\]]/gm , '');
if (!importedTypeLib.includes(cleanType) && !skipFieldTypes.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('${config.requirePath}/${cleanType}');\r\n`;
importedTypeLib.push(cleanType);
}
}
}
if (!importedTypeLib.includes("id")) {
importLib =`${importLib}const id = require('${config.requirePath}/id');\r\n`;
let importLibs = getImportLibs(result);
if (!importLibs.includes("const id = require")) {
importLibs =`const moment = require('moment');\r\nconst _ = require('lodash');\r\n${importLibs}const id = require('${config.requirePath}/id');\r\n`;
} else {
importLibs =`const moment = require('moment');\r\nconst _ = require('lodash');\r\n${importLibs}\r\n`;
}
code = `${importLib}${code}`;
code = `${importLibs}${code}`;
mkdirp.sync(config.resourcePath);
fs.writeFileSync(`${config.resourcePath}/${type}.js` , beautify(code , {indent_size : 4 ,pace_in_empty_paren: true }));
fs.writeFileSync(`${config.resourcePath}/${type}.js` , beautify(code , {indent_size : 4 ,pace_in_empty_paren: true }));
}


Expand Down
29 changes: 29 additions & 0 deletions models/mongodb/FHIRDataTypesSchema/Account_Coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require('mongoose');
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const {
Reference
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const positiveInt = require('../FHIRDataTypesSchema/positiveInt');

const {
Account_Coverage
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
Account_Coverage.add({
extension: {
type: [Extension],
default: void 0
},
modifierExtension: {
type: [Extension],
default: void 0
},
coverage: {
type: Reference,
required: true,
default: void 0
},
priority: positiveInt
});
module.exports.Account_Coverage = Account_Coverage;
36 changes: 36 additions & 0 deletions models/mongodb/FHIRDataTypesSchema/Account_Guarantor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const mongoose = require('mongoose');
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const {
Reference
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const boolean = require('../FHIRDataTypesSchema/boolean');
const {
Period
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');

const {
Account_Guarantor
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
Account_Guarantor.add({
extension: {
type: [Extension],
default: void 0
},
modifierExtension: {
type: [Extension],
default: void 0
},
party: {
type: Reference,
required: true,
default: void 0
},
onHold: boolean,
period: {
type: Period,
default: void 0
}
});
module.exports.Account_Guarantor = Account_Guarantor;
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require('mongoose');
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const string = require('../FHIRDataTypesSchema/string');
const {
Expression
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');

const {
ActivityDefinition_DynamicValue
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
ActivityDefinition_DynamicValue.add({
extension: {
type: [Extension],
default: void 0
},
modifierExtension: {
type: [Extension],
default: void 0
},
path: string,
expression: {
type: Expression,
required: true,
default: void 0
}
});
module.exports.ActivityDefinition_DynamicValue = ActivityDefinition_DynamicValue;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mongoose = require('mongoose');
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const code = require('../FHIRDataTypesSchema/code');
const {
CodeableConcept
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');

const {
ActivityDefinition_Participant
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
ActivityDefinition_Participant.add({
extension: {
type: [Extension],
default: void 0
},
modifierExtension: {
type: [Extension],
default: void 0
},
type: code,
role: {
type: CodeableConcept,
default: void 0
}
});
module.exports.ActivityDefinition_Participant = ActivityDefinition_Participant;
25 changes: 14 additions & 11 deletions models/mongodb/FHIRDataTypesSchema/Address.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const mongoose = require('mongoose');
const Extension = require('./Extension');
const string = require('./string');
const Period = require('./Period');
module.exports = new mongoose.Schema({
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const string = require('../FHIRDataTypesSchema/string');
const {
Period
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');

const {
Address
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
Address.add({
extension: {
type: [Extension],
default: void 0
Expand Down Expand Up @@ -31,10 +39,5 @@ module.exports = new mongoose.Schema({
type: Period,
default: void 0
}
}, {
_id: false,
id: false,
toObject: {
getters: true
}
});
});
module.exports.Address = Address;
39 changes: 39 additions & 0 deletions models/mongodb/FHIRDataTypesSchema/AdverseEvent_Causality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const mongoose = require('mongoose');
const {
Extension
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const {
CodeableConcept
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');
const string = require('../FHIRDataTypesSchema/string');
const {
Reference
} = require('../FHIRDataTypesSchemaExport/allTypeSchemaTopDef');

const {
AdverseEvent_Causality
} = require("../FHIRDataTypesSchemaExport/allTypeSchemaTopDef");
AdverseEvent_Causality.add({
extension: {
type: [Extension],
default: void 0
},
modifierExtension: {
type: [Extension],
default: void 0
},
assessment: {
type: CodeableConcept,
default: void 0
},
productRelatedness: string,
author: {
type: Reference,
default: void 0
},
method: {
type: CodeableConcept,
default: void 0
}
});
module.exports.AdverseEvent_Causality = AdverseEvent_Causality;
Loading

0 comments on commit d02a4ba

Please sign in to comment.