-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
186 lines (184 loc) · 7.17 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const fs =require('fs');
const _ = require('lodash');
const beautify = require('js-beautify').js;
const primitiveType = ["Boolean" , "String" , "Date" , "Number" , "Buffer"];
const path = require('path');
let schemaJson = JSON.parse(fs.readFileSync(path.join(__dirname ,'./fhir.schema.json') , {encoding: 'utf-8'}));
let FHIRJson = schemaJson.definitions;
let config = {};
let genedType = [];
function generateResourceSchema (type) {
if (!FHIRJson[type]) {
console.error('Unknown resource type ' + type);
process.exit(1);
}
let result = getSchema(FHIRJson[type]);
cleanChildSchema(result);
for (let i in result) {
let cleanType = result[i].type.replace(/[\[\]]/gm , '');
if (primitiveType.indexOf(cleanType) < 0 ) {
if (!fs.existsSync(`${config.typePath}/${cleanType}.js`)) {
generateSchema(cleanType);
}
}
result[i].default = "void 0"
}
let topLevelObj = {
id : {
type : "String" ,
unique : "true" ,
index : "true"
} ,
resourceType : {
type : "String" ,
required : "true"
}
};
result = Object.assign({} , topLevelObj , result);
let importLib = "const mongoose = require('mongoose');\r\n";
let code = `module.exports = function () {
const ${type} = ${JSON.stringify(result , null , 4).replace(/\"/gm , '')};\r\n const ${type}Schema = new mongoose.Schema(${type} , { _id : false });\r\n
return ${type}Schema;\r\n}`;
for (let i in result) {
let item = result[i];
item.default = "void 0";
let cleanType = item.type.replace(/[\[\]]/gm , '');
if (checkIsFHIRSchema(cleanType) && !importLib.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('${config.requirePath}/${cleanType}');\r\n`;
}
}
code = `${importLib}${code}`;
let uniqGenedType = _.uniq(genedType);
for (let gentype of uniqGenedType) {
generateSchema(gentype);
}
fs.writeFileSync(`${config.resourcePath}/${type}.js` , beautify(code , {indent_size : 4 ,pace_in_empty_paren: true }));
}
function getSchema (resource) {
let skipCol = ["resourceType" , "id" , "meta" ,"implicitRules" ,"language" , "text" ,"contained" , "extension" , "modifierExtension"];
let result = {};
for (let i in resource.properties) {
//skip the unusual type
if (skipCol.indexOf(i) >= 0 ) continue;
else if (i.includes("_")) continue;
let type = _.get(resource.properties[i] , "type")
let refSchema = _.get(resource.properties[i] , "$ref");
let isCode = _.get(resource.properties[i] , "enum");
if (type == 'array') {
//typeStatus = "array";
let arrayRef = resource.properties[i].items.$ref;
let arrayRefClean = arrayRef.split('/');
type = arrayRefClean[arrayRefClean.length-1];
if (/^#/.test(arrayRef)) {
result[i] = {
type : `[${type}]`
}
//console.log('custom array:' + type);
} else {
result[i] = {
type : `[${type}]`
}
// console.log('array:' + type);
}
}
else if (refSchema) {
if (/^#/.test(refSchema)) {
//typeStatus = "primitive";
let refClean = refSchema.split('/');
type = refClean[refClean.length-1];
result[i] = {
type : type
}
//console.log(type);
} else if (!/^#/.test(refSchema)) {
//typeStatus = "nested";
let refClean = refSchema.split('/');
type = refClean[refClean.length-1];
result[i] = {
type : type ,
}
//console.log(type);
}
} else if (isCode) {
type = "String";
//console.log(type);
result[i] = {
type : type ,
}
} else {
result[i] = {
type : type ,
}
}
}
return result;
}
let stringPrimitiveType = ['uri' , 'url' , 'code' , 'markdown' , 'string']
let datePrimitiveType = ['instant' ,'time' , 'dateTime' , 'date'];
let numberPrimitiveType = ['unsignedInt' , 'positiveInt' , 'number' , 'decimal'];
function cleanChildSchema (item) {
for (let i in item) {
let isArray = /[\[\]]/gm.test(item[i].type);
let type = item[i].type.replace(/[\[\]]/gm,'');
if (primitiveType.indexOf(type) < 0) {
if (stringPrimitiveType.indexOf(type) >= 0 ) {
item[i].type = isArray ? "[String]" : "String";
} else if (numberPrimitiveType.indexOf(type) >= 0) {
item[i].type = isArray ? "[Number]" : "Number";
} else if (datePrimitiveType.indexOf(type) >= 0) {
item[i].type = isArray ? "[Date]" : "Date";
} else if (type == "boolean") {
item[i].type = isArray ? "[Boolean]" : "Boolean";
} else if (type == "base64Binary") {
item[i].type = isArray ? "[Buffer]" : "Buffer";
}
}
}
}
function checkIsFHIRSchema (type) {
return fs.existsSync(`${config.typePath}/${type}.js`);
}
function generateSchema (type) {
let schema = getSchema(FHIRJson[type]);
if (!checkIsFHIRSchema (type)) {
cleanChildSchema(schema);
let importLib = "const mongoose = require('mongoose');\r\n";
for (let i in schema) {
let item = schema[i];
item.default = "void 0";
let cleanType = item.type.replace(/[\[\]]/gm , '');
if (primitiveType.indexOf(cleanType) < 0 && !importLib.includes(cleanType)) {
importLib =`${importLib}const ${cleanType} = require('./${cleanType}');\r\n`;
genedType.push(cleanType);
}
}
let code = `module.exports = new mongoose.Schema (${JSON.stringify(schema , null , 4).replace(/\"/gm , '')} , { _id : false });`;
code = `${importLib}${code}`;
fs.writeFileSync(`${config.typePath}/${type}.js` , beautify(code , {indent_size : 4 ,pace_in_empty_paren: true }));
}
}
/**
* @param {String} inputResourceType The FHIR resource type that you want to generate
* @param {Object} option
* @param {String} option.typePath The path to save FHIR data type
* @param {String} option.resourcePath The path to save FHIR resource type
* @param {String} option.cwd Current work directory
*/
module.exports = function (inputResourceType , option) {
if (option.cwd) {
process.chdir(option.cwd);
}
let typePath = option.typePath;
let resourcePath = option.resourcePath;
if (!typePath) {
console.error('missing typePath option');
process.exit(1);
} else if (!resourcePath) {
console.error('missing resourcePath option');
process.exit(1);
}
config.typePath = typePath;
config.resourcePath = resourcePath;
config.requirePath = path.relative(resourcePath ,typePath).replace(/\\/gm ,"/");
generateResourceSchema(inputResourceType);
}