/
genExpressApi.py
291 lines (233 loc) · 12.6 KB
/
genExpressApi.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import json
TYPE_MAP = {
'string': 'string',
'boolean': 'boolean',
'number': 'number',
'integer': 'number',
}
with open('templates/expressInterface.ts', 'r', encoding='utf-8') as f:
INTERFACES_TEMPLATE = f.read()
with open('templates/expressClass.ts', 'r', encoding='utf-8') as f:
CLASSES_TEMPLATE = f.read()
with open('templates/expressRouter.ts', 'r', encoding='utf-8') as f:
ROUTER_TEMPLATE = f.read()
with open('templates/expressBusiness.ts', 'r', encoding='utf-8') as f:
BUSINESS_TEMPLATE = f.read()
with open('templates/expressBase.ts', 'r', encoding='utf-8') as f:
BASE_TEMPLATE = f.read()
with open('templates/expressIndex.ts', 'r', encoding='utf-8') as f:
INDEX_TEMPLATE = f.read()
def pluralize(s:str) -> str:
if s.endswith('y'):
s = f'{s[:-1]}ies'
else:
s += 's'
return s
def genArtifacts(entityName:str, entity:dict, schema:dict) -> tuple[str,str, str, str]:
TYPE_OPERATORS = { #str uuid bool int float date datetime time
'str': 'like',
'uuid': 'in',
'bool': 'in',
'int': 'bt',
'float': 'bt',
'date': 'bt',
'datetime': 'bt',
'time': 'bt',
}
def getTypeProps(schemaName:str) -> str:
typeProps = {}
for prop, attrs in schema[schemaName]['properties'].items():
isRequired = 'required' in schema[schemaName] and prop in schema[schemaName]['required']
isAutoKey = 'autokey' in entity and entity['autokey'] and 'key' in entity and entity['key'] == prop
typeProp = {
'required': isRequired and not isAutoKey,
'type': TYPE_MAP[attrs["type"]] if 'type' in attrs else attrs["$ref"].split('/')[-1].replace('View', '')
}
if 'filterOperator' in entity['properties'][prop]:
typeProp["operator"] = entity['properties'][prop]['filterOperator']
typeProps[prop] = typeProp
return json.dumps(typeProps, ensure_ascii=False, indent=2)
#for validating entities received from client
createTypeProps = getTypeProps('create')
updateTypeProps = getTypeProps('update')
partialTypeProps = getTypeProps('partial')
queryTypeProps = getTypeProps('query')
sortableProps = ", ".join([f'"{s}"' for s in entity["sortable"]] if 'sortable' in entity else [])
# viewTypeProps = getTypeProps('view')
def getInterface(schemaName:str, asClass:bool = False):
prefix = "I" if not asClass else ""
interface = []
for prop, attrs in schema[schemaName]['properties'].items():
if prop == "id":
continue
optional = '?' if 'required' not in schema[schemaName] or prop not in schema[schemaName]['required'] else ''
# if schemaName == 'view':
# print(asClass, schemaName, prop, attrs, prefix)
root:str
if '$ref' in attrs:
root = attrs["$ref"].split('/')[-1].replace('View', '')
type = f"{prefix}{root.capitalize()}View"
elif attrs['type'] == 'array':
root = attrs['items']["$ref"].split('/')[-1].replace('View', '')
# type = f"I{root.capitalize()}View[]" if not asClass else f"{root.capitalize()}View[]"
type = f"IQueryResult<IQuery, {prefix}{root.capitalize()}View>"
else:
type = attrs["type"]
tsType = TYPE_MAP[type] if type in TYPE_MAP else type
interface.append(f'{prop}{optional}:{tsType};')
return '\n '.join(interface)
storeInterface = getInterface('store')
queryInterface = getInterface('query')
createInterface = getInterface('create')
updateInterface = getInterface('update')
partialInterface = getInterface('partial')
viewInterface = getInterface('view')
def getClass(schemaName:str):
return getInterface(schemaName, True)
storeClass = getClass('store')
createClass = getClass('create')
updateClass = getClass('update')
partialClass = getClass('partial')
viewClass = getClass('view')
def getRefEntities(schemaName:str):
refEntities = {}
refListEntities = {}
for propertyName, attrs in schema[schemaName]['properties'].items():
if '$ref' in attrs or 'items' in attrs:
if '$ref' in attrs:
root = attrs["$ref"].split('/')[-1].replace('View', '')
refEntities[propertyName] = root
elif 'items' in attrs:
root = attrs['items']["$ref"].split('/')[-1].replace('View', '')
refListEntities[propertyName] = root
return refEntities, refListEntities
def getImports(schemaName:str, namePrefix:str = "I", nameSuffix:str = "", sourceSuffix:str = "Interfaces"):
refEntities, refListEntities = getRefEntities(schemaName)
refs = {**refEntities, **refListEntities}
imports = []
refFiles = {}
for prop, root in refs.items():
if root == entityName:
continue
ref = root.capitalize()
refEntity = f'{namePrefix}{ref}{nameSuffix}'
refEntityFile = f"./{root}{sourceSuffix}"
if refEntityFile not in refFiles:
refFiles[refEntityFile] = []
refFiles[refEntityFile].append(refEntity)
for refEntityFile, interfaceImports in refFiles.items():
interfaceImportsStr = ', '.join(list(set(interfaceImports)))
importStmt = f'import {{ {interfaceImportsStr} }} from "{refEntityFile}";'
imports.append(importStmt)
return '\n'.join(set(imports))
entityInterfaceImports = getImports('view', "I", "View", "Interfaces")
entityClassImports = getImports('view', "", "View", "Classes")
entityBusinessImports = getImports('view', "", "Business", "Business")
def getEntityReferencedEntitiesGets(schemaName:str):
refProps = {pn: attrs for pn, attrs in entity['properties'].items() if 'typeReferenceViaProperty' in attrs}
entityReferencedEntitiesGets = []
newLineIndent = "\n "
refEntityName:str
for propertyName, property in refProps.items():
refEntityNameCapitalized = property['type'].capitalize()
typeReferenceViaProperty = property['typeReferenceViaProperty']
getter = f"if ({entityName}.{typeReferenceViaProperty}) {{ {entityName}.{propertyName} = await new {refEntityNameCapitalized}Business(this.context).getById({entityName}.{typeReferenceViaProperty}); }}"
entityReferencedEntitiesGets.append(getter)
refCollectionProps = {pn: attrs for pn, attrs in entity['properties'].items() if 'subTypeReferenceViaProperty' in attrs}
entityReferencedEntitiesCollectionGets = []
for propertyName, property in refCollectionProps.items():
refEntityNameCapitalized = property['subtype'].capitalize()
subTypeReferenceViaProperty = property['subTypeReferenceViaProperty']
query = f"var query{refEntityNameCapitalized} = {{ where: [ {{ column: '{subTypeReferenceViaProperty}', operator: Operator.Equals, value: {entityName}.id }} as ICondition] }} as IDataQuery;"
getter = f"{entityName}.{propertyName} = (await new {refEntityNameCapitalized}Business(this.context).getAll(query{refEntityNameCapitalized}, maxDepth));"
entityReferencedEntitiesCollectionGets.append(f"{query}{newLineIndent} {getter}")
refEntitiesGetsStr = newLineIndent.join(entityReferencedEntitiesGets)
refEntitiesCollectionGetsStr = f"{newLineIndent} ".join(entityReferencedEntitiesCollectionGets)
return f"{refEntitiesGetsStr}{newLineIndent*2}if (maxDepth) {{{newLineIndent} {refEntitiesCollectionGetsStr}{newLineIndent*2} maxDepth--;{newLineIndent}}}"
entityReferencedEntitiesGets = getEntityReferencedEntitiesGets('view')
autoCreateId = ""
if 'autokey' in entity and entity['autokey']:
if not 'key' in entity:
raise Exception("missing entity key property for " + entityName)
key = entity["key"]
if 'autonumber' in entity and entity['autonumber'] == key:
autoCreateId = ""
else:
autoCreateId = f"{entityName}.{key} = randomUUID();"
replacements = {
"__EntityInterfaceImports__": entityInterfaceImports,
"__EntityClassImports__": entityClassImports,
"__EntityBusinessImports__": entityBusinessImports,
"__EntityName__": entityName,
"__EntityNameCapitalized__": entityName.capitalize(),
"__EntityNameCapitalizedPlural__": pluralize(entityName.capitalize()),
"__EntityCreateTypeProperties__": createTypeProps,
"__EntityUpdateTypeProperties__": updateTypeProps,
"__EntityPartialTypeProperties__": partialTypeProps,
"__EntityQueryTypeProperties__": queryTypeProps,
"__EntitySortableProperties__": sortableProps,
"__EntityAutoCreateId__": autoCreateId,
"__EntityClientQueryInterface__": queryInterface,
"__EntityInterface__": storeInterface,
"__EntityCreateInterface__": createInterface,
"__EntityUpdateInterface__": updateInterface,
"__EntityPartialInterface__": partialInterface,
"__EntityViewInterface__": viewInterface,
"__EntityClass__": storeClass,
"__EntityCreateClass__": createClass,
"__EntityUpdateClass__": updateClass,
"__EntityPartialClass__": partialClass,
"__EntityViewClass__": viewClass,
"__GetReferencedEntitiesById__": entityReferencedEntitiesGets
}
interfacesScript = INTERFACES_TEMPLATE
classesScript = CLASSES_TEMPLATE
routerScript = ROUTER_TEMPLATE
businessScript = BUSINESS_TEMPLATE
for find, repl in replacements.items():
interfacesScript = interfacesScript.replace(find, repl)
classesScript = classesScript.replace(find, repl)
routerScript = routerScript.replace(find, repl)
businessScript = businessScript.replace(find, repl)
return routerScript, interfacesScript, businessScript, classesScript
def _genCode(entities:dict, openApiSchemas:dict):
routers = {}
interfaces = {}
businesses = {}
classes = {}
routeImports = []
routeUses = []
for entityName, entity in entities.items():
schema = {
'store': openApiSchemas[entityName],
'query': openApiSchemas[f'{entityName}Query'],
'create': openApiSchemas[f'{entityName}Create'],
'update': openApiSchemas[f'{entityName}Update'],
'partial': openApiSchemas[f'{entityName}Partial'],
'view': openApiSchemas[f'{entityName}View'],
}
routers[entityName], interfaces[entityName], businesses[entityName], classes[entityName] = genArtifacts(entityName, entity, schema)
routeImports.append(f"import {{ {entityName}Router }} from './{entityName}Route';")
routeUses.append(f"indexRouter.use('/{pluralize(entityName)}', {entityName}Router);")
index = INDEX_TEMPLATE
index = index.replace("__EntitiesRoutesImports__", "\n".join(routeImports))
index = index.replace("__EntitiesRoutesUses__", "\n".join(routeUses))
return routers, interfaces, businesses, classes, BASE_TEMPLATE, index
def createFiles(scriptsOutputDir:str, entities:dict, openApiSchemas:dict):
routers, interfaces, businesses, classes, base, index = _genCode(entities=entities, openApiSchemas=openApiSchemas)
for entity, script in interfaces.items():
with open(f'{scriptsOutputDir}/{entity}Interfaces.ts', 'w', encoding='utf-8') as f:
f.write(script)
for entity, script in classes.items():
with open(f'{scriptsOutputDir}/{entity}Classes.ts', 'w', encoding='utf-8') as f:
f.write(script)
for entity, script in routers.items():
with open(f'{scriptsOutputDir}/{entity}Route.ts', 'w', encoding='utf-8') as f:
f.write(script)
for entity, script in businesses.items():
with open(f'{scriptsOutputDir}/{entity}Business.ts', 'w', encoding='utf-8') as f:
f.write(script)
with open(f'{scriptsOutputDir}/base.ts', 'w', encoding='utf-8') as f:
f.write(base)
with open(f'{scriptsOutputDir}/index.ts', 'w', encoding='utf-8') as f:
f.write(index)