-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_model.py
92 lines (55 loc) · 2.63 KB
/
generate_model.py
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
import os
def modifyValueByKeyAndType(key, value):
if(isinstance(value, bool)):
value= str(value).lower()
elif (value=='Date.now()' or key=='enum'):
value= value
elif(isinstance(value, int)): #bool checker
value= str(value)
else:
value= "'{}'".format(value)
return value
# def generateParams(field):
def generate_model(module_name, fields):
#creating model ouput file
model_output_file= './output/models/'+ module_name +'.js'
os.makedirs(os.path.dirname(model_output_file), exist_ok=True)
schema_first_line= "const {}Schema = new Schema({{\n".format(module_name)
schema_last_line= "})\n\n"
module_first_line= """const mongoose = require('mongoose')\nconst { Schema } = mongoose\n\n"""
module_last_line= "module.exports.{} = mongoose.model('{}', {}Schema)".format(module_name[0].upper()+module_name[1:], module_name[0].upper()+module_name[1:], module_name)
output= open(model_output_file, 'w')
output.write(module_first_line)
output.write(schema_first_line)
for field in fields:
# print('key: ', field['key'], ' type: ', type(field['type']))
field_last_line=""
if( isinstance(field['type'], list)):
field_last_line= '\t],\n'
output.write("\t{}: [\n".format(field['key']))
for field in field['type']:
output.write('\t\t{')
for key, value in field.items():
field_curr_line=""
if key=="type":
field_curr_line= "\n\t\t\t{}: {},".format(key, value)
else:
value= modifyValueByKeyAndType(key, value)
field_curr_line= "\n\t\t\t{}: {},".format(key, value)
output.write(field_curr_line)
output.write('\n\t\t},\n')
else:
field_curr_line= "\t{}: {{ \n\t\ttype: {}, \n".format(field['key'],field['type'])
output.write(field_curr_line)
field_last_line='\t},\n'
# output.write(field_curr_line)
if('params' in field):
for key, value in field['params'].items():
# print('key: ', key, ' value: ', value, ' type: ', type(value))
value= modifyValueByKeyAndType(key, value)
curr_line= "\t\t{}: {},\n".format(key, value)
output.write(curr_line)
output.write(field_last_line)
output.write(schema_last_line)
output.write(module_last_line)
# params - if bool, or number, else