-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
178 lines (132 loc) · 4.92 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
const { program } = require('commander');
const fs = require('fs');
// Material pallete 700 or 800 or 900 for white text
const colors = {
red: '#d32f2f',
pink: '#c2185b',
purple: '#7b1fa2',
deepPurple: '#512da8',
indigo: '#303f9f',
blue: '#1976d2',
lightBlue: '#0277bd',
cyan: '#00838f',
teal: '#00796b',
green: '#2e7d32',
lightGreen: '#387002',
lime: '#6c6f00',
yellow: '#bc5100',
amber: '#c43e00',
orange: '#bb4d00',
deepOrange: '#ac0800',
brown: '#5d4037',
grey: '#616161',
blueGrey: '#455a64'
}
const colorByIndex = (id) => {
const colorsArray = Object.values(colors);
const index = id % colorsArray.length;
return colorsArray[index];
}
const convert = (schemaJSON, schemaDBML) => {
console.log(`Converting: ${schemaJSON} to ${schemaDBML}`);
// read MongoDB _SCHEMA collection from _SCHEMA.json as JSON array of objects
const _SCHEMA = JSON.parse(fs.readFileSync(schemaJSON, 'utf-8'));
// console.log(_SCHEMA);
let classIndex = 0;
let DBML = [];
DBML.push(`// Generated by parseServerSchema2dbml`);
_SCHEMA.forEach(parseClass => {
const className = parseClass._id;
console.log(className);
const keys = Object.keys(parseClass);
const fields = keys.filter(key => {
// Remove specific Parse keys (keep only custom fields)
if (!['_id', 'objectId', 'updatedAt', 'createdAt', '_metadata'].includes(key)) {
return key;
}
});
const metadata = parseClass['_metadata'];
// console.log(metadata);
const fieldsOptions = metadata && metadata['fields_options'] || {};
const color = colorByIndex(classIndex);
let TABLE = `Table ${className} [headercolor: ${color}] {
objectId string
createdAt date [default: \`now()\`, note: "created time"]
updatedAt date [default: \`now()\`, note: "updated time"]
`;
const scalarFields = [];
const pointerFields = [];
const relationFields = [];
const indexes = [];
indexes.push(` objectId [pk]`);
const dbmlOptions = {};
fields.forEach(fieldName => {
const fieldType = parseClass[fieldName];
// console.log(fieldName, fieldType);
const fieldOptions = fieldsOptions[fieldName] || {};
// console.log(fieldName, fieldOptions);
dbmlOptions[fieldName] = [];
if (fieldOptions.defaultValue !== undefined) {
dbmlOptions[fieldName].push(`default: \`${JSON.stringify(fieldOptions.defaultValue)}\``)
}
const notes = [];
if (fieldOptions.required) {
notes.push('required');
}
if (fieldType.startsWith('*')) { // Pointer
pointerFields.push(fieldName);
const pointerClass = fieldType.substring(1); // Extract `*${pointerClass}`
dbmlOptions[fieldName].push(`ref: > ${pointerClass}.objectId`)
notes.push('MANY-to-ONE');
} else if (fieldType.startsWith('relation<')) { // Relation
relationFields.push(fieldName);
const relationClass = fieldType.substring(9, fieldType.length - 1); // Extract `relation<${relationClass}>`
dbmlOptions[fieldName].push(`ref: - ${relationClass}.objectId`)
notes.push('MANY-to-MANY');
} else { // others
scalarFields.push(fieldName);
}
dbmlOptions[fieldName].push(`note: '${notes.join(', ')}'`)
const dbmlOptionsAsString = dbmlOptions[fieldName].length ? `[${dbmlOptions[fieldName].join(', ')}]` : '';
dbmlOptions[fieldName] = dbmlOptionsAsString;
});
scalarFields.forEach(fieldName => {
const fieldType = parseClass[fieldName];
TABLE += ` ${fieldName} ${fieldType} ${dbmlOptions[fieldName]}
`;
});
pointerFields.forEach(fieldName => {
TABLE += ` ${fieldName} "Pointer" ${dbmlOptions[fieldName]}
`;
});
relationFields.forEach(fieldName => {
TABLE += ` ${fieldName} "Relation" ${dbmlOptions[fieldName]}
`;
});
TABLE += `
indexes {
${indexes.join('\n')}
}
`;
TABLE += `}`;
DBML.push(TABLE);
// console.log(fields);
classIndex += 1;
});
const dbml = DBML.join('\n\n')
// console.log(dbml);
fs.writeFileSync(schemaDBML, dbml);
console.log('DONE');
}
// CLI
program.version('0.2.0');
program
.option('-i --input <input>', 'path to the _SCHEMA.json')
.option('-o --output <output>', 'path to the _SCHEMA.dbml')
.action((env) => {
const schemaJSON = env.input || './_SCHEMA.json';
const schemaDBML = env.output || './_SCHEMA.dbml';
// console.log(input, output);
convert(schemaJSON, schemaDBML);
});
program.parse(process.argv);