@izara_project/izara-core-generate-service-code 1.0.68 → 1.0.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/package.json +1 -1
  2. package/scripts/format-ejs.js +36 -32
  3. package/src/compile/compileProject.js +26 -10
  4. package/src/compile/loadSchemas.js +122 -59
  5. package/src/emit/coreFunction/emitCoreFunctionArtifacts.js +163 -0
  6. package/src/emit/coreFunction/templates/CoreFunction_Yaml.ejs +38 -0
  7. package/src/emit/coreFunction/templates/CoreResource_Yaml.ejs +235 -0
  8. package/src/emit/coreFunction/templates/GenerateSeedDataDynamo_HdrInv.ejs +16 -0
  9. package/src/emit/coreFunction/templates/GenerateSeedDataDynamo_Main.ejs +38 -0
  10. package/src/emit/coreFunction/templates/GenerateSeedDataGraphData_HdrInv.ejs +16 -0
  11. package/src/emit/coreFunction/templates/GenerateSeedDataGraphData_Main.ejs +32 -0
  12. package/src/emit/coreFunction/templates/GenerateSeedDataGraphSchema_HdrInv.ejs +16 -0
  13. package/src/emit/coreFunction/templates/GenerateSeedDataGraphSchema_Main.ejs +71 -0
  14. package/src/emit/coreFunction/templates/GeneratedGraphNodeSchemas.ejs +47 -0
  15. package/src/emit/coreFunction/templates/GeneratedGraphRelSchemas.ejs +27 -0
  16. package/src/emit/coreFunction/templates/GeneratedTableConfigs.ejs +34 -0
  17. package/src/emit/coreFunction/templates/InitialSetupConfig.ejs +108 -0
  18. package/src/emit/coreFunction/templates/InitialSetup_HdrInv.ejs +12 -0
  19. package/src/emit/coreFunction/templates/InitialSetup_Main.ejs +51 -0
  20. package/src/emit/coreFunction/templates/RemoveSeedDataDynamo_HdrInv.ejs +16 -0
  21. package/src/emit/coreFunction/templates/RemoveSeedDataDynamo_Main.ejs +31 -0
  22. package/src/emit/coreFunction/templates/UploadSchema_HdrInv.ejs +37 -0
  23. package/src/emit/coreFunction/templates/UploadSchema_Main.ejs +52 -0
  24. package/src/emit/crud/emitCrudApplicationArtifacts.js +194 -41
  25. package/src/emit/crud/emitDynamicCrudFlowSchemas.js +34 -17
  26. package/src/emit/crud/generateCrudApplicationArtifacts.js +14 -6
  27. package/src/emit/crud/generateDynamicCrudFlowSchemas.js +4 -1
  28. package/src/emit/crud/templates/CreateEndpoint_Main.ejs +1 -1
  29. package/src/emit/crud/templates/DeleteEndpoint_Main.ejs +1 -1
  30. package/src/emit/crud/templates/GetEndpoint_Main.ejs +2 -2
  31. package/src/emit/crud/templates/GetRangeEndpoint_Main.ejs +1 -1
  32. package/src/emit/crud/templates/UpdateEndpoint_Main.ejs +1 -1
  33. package/src/emit/custom/customFlows.js +23 -13
  34. package/src/emit/custom/emitCustomFlowApplicationArtifacts.js +535 -114
  35. package/src/emit/custom/generateCustomFlowApplicationArtifacts.js +28 -8
  36. package/src/emit/custom/templates/FlowEndpoint_Main.ejs +2 -2
  37. package/src/emit/custom/templates/FlowStep_Main.ejs +1 -1
  38. package/src/emit/custom/templates/s3/CreateObject_Main.ejs +1 -1
  39. package/src/emit/custom/templates/s3/FlowHandler_HdrS3.ejs +2 -2
  40. package/src/emit/custom/templates/s3/ReservedLimit_HdrSqs.ejs +1 -1
  41. package/src/emit/custom/templates/s3/ReservedLimit_Main.ejs +1 -1
  42. package/src/emit/custom/templates/triggerCache/CheckTriggerCache_HdrSqs.ejs +4 -4
  43. package/src/emit/function/functionYamlArtifact.js +273 -97
  44. package/src/emit/function/templates/ApiFunction_Yaml.ejs +7 -0
  45. package/src/emit/function/templates/EventBridgeFunction_Yaml.ejs +6 -0
  46. package/src/emit/function/templates/InvFunction_Yaml.ejs +5 -0
  47. package/src/emit/function/templates/S3Function_Yaml.ejs +16 -0
  48. package/src/emit/function/templates/SqsFunction_Yaml.ejs +10 -0
  49. package/src/emit/function/templates/WbsFunction_Yaml.ejs +10 -0
  50. package/src/emit/rbac/emitDynamicRbacFlowSchemas.js +28 -30
  51. package/src/emit/rbac/emitRbacApplicationArtifacts.js +6 -6
  52. package/src/emit/rbac/generateCreateTargetRoleHandler.js +6 -4
  53. package/src/emit/rbac/generateDynamicRbacFlowSchemas.js +8 -2
  54. package/src/emit/rbac/rbacFlowTag.js +3 -1
  55. package/src/emit/rbac/templates/UserRbacFlow_Main.ejs +1 -1
  56. package/src/emit/relationship/emitDynamicRelationshipFlowSchemas.js +26 -10
  57. package/src/emit/relationship/emitRelationshipApplicationArtifacts.js +189 -37
  58. package/src/emit/relationship/generateDynamicRelationshipFlowSchemas.js +8 -2
  59. package/src/emit/relationship/generateRelationshipApplicationArtifacts.js +15 -6
  60. package/src/emit/relationship/templates/ChangeRelationship_Main.ejs +1 -1
  61. package/src/emit/relationship/templates/CreateRelationship_Main.ejs +1 -1
  62. package/src/emit/relationship/templates/DeleteRelationship_Main.ejs +1 -1
  63. package/src/emit/relationship/templates/GetRelationship_Main.ejs +1 -1
  64. package/src/emit/relationship/templates/MoveRelationship_Main.ejs +1 -1
  65. package/src/emit/relationship/templates/UpdateRelationship_Main.ejs +1 -1
  66. package/src/emit/resource/emitResourceArtifacts.js +249 -123
  67. package/src/emit/resource/generateResourceArtifacts.js +15 -6
  68. package/src/emit/resource/templates/DynamoDBTable_Yaml.ejs +18 -18
  69. package/src/emit/resource/templates/FlowOut_Yaml.ejs +5 -5
  70. package/src/emit/resource/templates/GeneratedLambdaRoleServiceConfig.ejs +10 -9
  71. package/src/emit/resource/templates/ResourceBeforeLogicalYaml.ejs +44 -44
  72. package/src/emit/resource/templates/ResourceSqsYaml.ejs +36 -36
  73. package/src/emit/resource/templates/ResourceYaml.ejs +48 -48
  74. package/src/emit/resource/templates/S3Subscription_Yaml.ejs +7 -0
  75. package/src/emit/resource/templates/SharedResource_Yaml.ejs +11 -11
  76. package/src/emit/resource/templates/SystemDynamoDB_Yaml.ejs +122 -105
  77. package/src/emit/resource/templates/WebSocketSubscription_Yaml.ejs +7 -0
  78. package/src/emit/shared/templates/FindData_HdrSqs.ejs +120 -0
  79. package/src/emit/shared/templates/FindData_Main.ejs +345 -0
  80. package/src/emit/{crud → shared}/templates/FlowHandler_HdrApi.ejs +1 -1
  81. package/src/emit/{relationship → shared}/templates/FlowHandler_HdrDsq.ejs +4 -4
  82. package/src/emit/{crud → shared}/templates/FlowHandler_HdrInv.ejs +1 -1
  83. package/src/emit/{crud → shared}/templates/FlowHandler_HdrSqs.ejs +4 -4
  84. package/src/emit/shared/templates/PaginateProcessLogical_HdrDsq.ejs +162 -0
  85. package/src/emit/shared/templates/PaginateProcessLogical_HdrSqs.ejs +140 -0
  86. package/src/emit/shared/templates/PaginateProcessLogical_Main.ejs +268 -0
  87. package/src/emit/{relationship/templates/FlowHandler_HdrSqs.ejs → shared/templates/ProcessLogical_HdrSqs.ejs} +40 -55
  88. package/src/emit/shared/templates/ProcessLogical_Main.ejs +448 -0
  89. package/src/emit/system/emitSystemFindDataArtifacts.js +266 -0
  90. package/src/emit/system/emitSystemWebSocketArtifacts.js +61 -165
  91. package/src/emit/system/templates/WebSocketComplete_HdrSqs.ejs +48 -0
  92. package/src/emit/system/templates/WebSocketComplete_Main.ejs +91 -0
  93. package/src/emit/system/templates/WebSocketComplete_Yaml.ejs +19 -0
  94. package/src/emit/system/templates/WebSocketConnect.ejs +45 -0
  95. package/src/emit/system/templates/WebSocketConnect_Yaml.ejs +11 -0
  96. package/src/generate.js +166 -36
  97. package/src/generateIntTest.js +76 -28
  98. package/src/generateResourceIntTest.js +35 -12
  99. package/src/generateResources/IntTest/generateResources/generateResources.js +5 -2
  100. package/src/generateResources/IntTest/generateResources/templateResources.ejs +18 -30
  101. package/src/generateResources/IntTest/generateTests/events/events.js +1 -1
  102. package/src/generateResources/IntTest/generateTests/events/templateEvents.ejs +13 -10
  103. package/src/generateResources/IntTest/generateTests/generateTemplate.js +2 -2
  104. package/src/generateResources/IntTest/generateTests/pathIntTest/pathIntTest.js +2 -1
  105. package/src/generateResources/IntTest/generateTests/pathIntTest/template.ejs +1 -3
  106. package/src/generateResources/IntTest/generateTests/tests/templateTests.ejs +1 -1
  107. package/src/generateResources/IntTest/generateTests/tests/tests.js +7 -3
  108. package/src/generateResources/IntTest/libs/{libs.js → IntTestGenerateLib.js} +34 -23
  109. package/src/generateResources/IntTest/upload/uploadIntTest.js +33 -19
  110. package/src/intTestGeneratorShared.js +15 -10
  111. package/src/output/commitArtifacts.js +99 -39
  112. package/src/output/mergeHooks.js +34 -21
  113. package/src/output/validateArtifacts.js +37 -27
  114. package/src/plan/buildGenerationPlan.js +24 -13
  115. package/test/core.test.js +117 -58
  116. package/test/createTargetRoleHandler.test.js +1 -1
  117. package/test/crudApplicationArtifacts.test.js +132 -31
  118. package/test/customFlowApplicationArtifacts.test.js +353 -71
  119. package/test/dynamicCrudFlowSchemas.test.js +74 -26
  120. package/test/dynamicRbacFlowSchemas.test.js +61 -14
  121. package/test/dynamicRelationshipFlowSchemas.test.js +65 -18
  122. package/test/functionYamlArtifact.test.js +67 -10
  123. package/test/intTestApi.test.js +9 -10
  124. package/test/publicApi.test.js +178 -36
  125. package/test/relationshipApplicationArtifacts.test.js +90 -19
  126. package/test/resourceArtifacts.test.js +173 -33
  127. package/src/emit/relationship/dynamicRelationshipFlowSchemaTemplate.js +0 -11
  128. package/src/emit/relationship/templates/FlowHandler_HdrApi.ejs +0 -59
  129. package/src/emit/relationship/templates/FlowHandler_HdrInv.ejs +0 -47
@@ -0,0 +1,448 @@
1
+ /*
2
+ Copyright (C) 2020 Sven Mason <http://izara.io>
3
+
4
+ This program is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Affero General Public License as
6
+ published by the Free Software Foundation, either version 3 of the
7
+ License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Affero General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Affero General Public License
15
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import dynamodbSharedLib from '@izara_project/izara-core-library-dynamodb';
19
+ import snsSharedLib from '@izara_project/izara-core-library-sns';
20
+ import logicalResultsSharedLib from '@izara_project/izara-core-library-logical-result';
21
+ import searchResultSharedLib from '@izara_project/izara-core-library-search-result';
22
+ import { sns } from '@izara_project/izara-core-library-external-request';
23
+
24
+ import {
25
+ getObjectSchema,
26
+ serviceConfig,
27
+ consts,
28
+ utils
29
+ } from '@izara_project/izara-core-library-service-schemas';
30
+ import { NoRetryError } from '@izara_project/izara-core-library-core';
31
+ const { STORAGE_TYPES } = consts;
32
+ const { getUsedFieldNamesOfIdentifiers } = utils;
33
+
34
+ //(<additionalModule>)
35
+ import { identifiersObject as identifierObj } from '@izara_project/izara-core-library-service-schemas';
36
+ //(</additionalModule>)
37
+
38
+ /**
39
+ * performs the logical operation/filter/search in the element, saves into LogicalResultsData table
40
+ * @param {Object} _izContext
41
+ * @param {Object} objType - contain objectType and serviceTag
42
+ * @param {Object} element - logical element
43
+ * @param {Object} element.filterType - 'logical'
44
+ * @param {string} element.logicalType - contain 'identifier' | 'partitionKey'
45
+ * @param {Object} [element.partitionKeyValues] - value of fieldNames that is partitionKey.
46
+ * @param {Object} [element.sortKeyValues] - value of fieldNames that is sortKey. (optional)
47
+ * @param {string} [element.comparison] - use for query in queryElements.sortKeyCondition. (optional)
48
+ * @param {string} [element.conditionalFieldValues] - user after query for filter data before save to logicalData table. (optional)
49
+ * @param {boolean} overWriteCache
50
+ *
51
+ * @returns {string} description of return value
52
+ */
53
+
54
+ export default async function processLogical(
55
+ _izContext,
56
+ objType,
57
+ element,
58
+ overWriteCache,
59
+ callingFlow
60
+ ) {
61
+ try {
62
+ _izContext.logger.debug('----- ProcessLogical event params -----', {
63
+ objType: objType,
64
+ element: element,
65
+ overWriteCache: overWriteCache,
66
+ callingFlow: callingFlow
67
+ });
68
+
69
+ let removeAttributes = ['paginateProcessLogicalUniqueRequestId'];
70
+
71
+ //(<removeAttributes>)
72
+ //(</removeAttributes>)
73
+
74
+ // Check/Create logicalResultsMain
75
+ let [logicalResultsMainStatus, logicalResultId, logicalResultMain] =
76
+ await logicalResultsSharedLib.checkLogicalMainStatus(
77
+ _izContext,
78
+ element, // for create logicalResultId
79
+ objType,
80
+ callingFlow,
81
+ removeAttributes,
82
+ overWriteCache
83
+ );
84
+ _izContext.logger.debug('After check logicalMainStatus: ', {
85
+ logicalResultsMainStatus: logicalResultsMainStatus,
86
+ logicalResultId: logicalResultId,
87
+ logicalResultMain: logicalResultMain
88
+ });
89
+
90
+ if (logicalResultsMainStatus !== 'process') {
91
+ _izContext.logger.debug(
92
+ `logicalResultsMainStatus status isn't process :${logicalResultsMainStatus}`
93
+ );
94
+ return {
95
+ returnResponse: 'logicalResultsMainStatus status is not process'
96
+ };
97
+ }
98
+
99
+ let errorsFound = [];
100
+
101
+ let standardErrorParams = [
102
+ _izContext,
103
+ logicalResultId,
104
+ logicalResultMain.logicalElement,
105
+ logicalResultMain.objType,
106
+ overWriteCache,
107
+ callingFlow,
108
+ 'error'
109
+ ];
110
+
111
+ if (
112
+ element.comparison &&
113
+ !searchResultSharedLib.consts.POSSIBLE_COMPARISON_SORTID.includes(
114
+ element.comparison
115
+ )
116
+ ) {
117
+ errorsFound.push('comparison is not possible comparison');
118
+ }
119
+
120
+ // validate partitionKeyValues
121
+ if (!element.hasOwnProperty('partitionKeyValues')) {
122
+ _izContext.logger.debug('element not have partitionKeyValues');
123
+ errorsFound.push('element not have partitionKeyValues');
124
+ } else if (Object.keys(element.partitionKeyValues).length === 0) {
125
+ _izContext.logger.debug('partitionKeyValues is empty');
126
+ errorsFound.push('partitionKeyValues is empty');
127
+ }
128
+
129
+ const objectSchemas = await getObjectSchema.getObjSchemaS3WithHierarchy(
130
+ _izContext,
131
+ objType
132
+ );
133
+ _izContext.logger.debug('objectSchemas: ', objectSchemas);
134
+
135
+ let partitionKeyFieldName = null;
136
+ let tableName = null;
137
+ let sortKeyFieldName = null;
138
+
139
+ for (const schemasIdentifier of objectSchemas.identifiers) {
140
+ if (schemasIdentifier.type === 'partitionKey') {
141
+ if (partitionKeyFieldName === null) {
142
+ if (schemasIdentifier.hasOwnProperty('fieldName')) {
143
+ partitionKeyFieldName = schemasIdentifier.fieldName;
144
+ } else {
145
+ partitionKeyFieldName = schemasIdentifier.name;
146
+ }
147
+ } else {
148
+ throw new NoRetryError(
149
+ 'Invalid object schemas have more than one partitionKey'
150
+ );
151
+ }
152
+ }
153
+
154
+ if (schemasIdentifier.type === 'sortKey') {
155
+ if (sortKeyFieldName === null) {
156
+ if (schemasIdentifier.hasOwnProperty('fieldName')) {
157
+ sortKeyFieldName = schemasIdentifier.fieldName;
158
+ } else {
159
+ sortKeyFieldName = schemasIdentifier.name;
160
+ }
161
+ } else {
162
+ throw new NoRetryError(
163
+ 'Invalid object schemas have more than one sortKey'
164
+ );
165
+ }
166
+ _izContext.logger.debug('have sortKeyFieldName: ', sortKeyFieldName);
167
+ }
168
+ }
169
+
170
+ const storageResources = objectSchemas.storageResources;
171
+
172
+ storageResourcesLoop: for (const storageResourceTag of Object.keys(
173
+ storageResources
174
+ )) {
175
+ _izContext.logger.debug('check storageResourceTag: ', storageResourceTag);
176
+
177
+ if (
178
+ storageResources[storageResourceTag].storageType ===
179
+ STORAGE_TYPES.dynamoDB
180
+ ) {
181
+ if (
182
+ storageResources[storageResourceTag].hasOwnProperty(
183
+ 'groupByPartitionKeyField'
184
+ )
185
+ ) {
186
+ if (element.hasOwnProperty('sortKeyValues')) {
187
+ continue;
188
+ } else {
189
+ errorsFound.push(
190
+ 'schema error: dynamo storageResource set to groupByPartitionKeyField but no sortKey'
191
+ );
192
+ break;
193
+ }
194
+ }
195
+
196
+ // validate conditionalFieldValues
197
+ if (
198
+ element.hasOwnProperty('conditionalFieldValues') &&
199
+ element.conditionalFieldValues.length > 0
200
+ ) {
201
+ conditionalFieldValuesLoop: for (const conditionalFieldValue of element.conditionalFieldValues) {
202
+ let conditionalFieldName = conditionalFieldValue.fieldName;
203
+ let storageResourceTagsFieldName =
204
+ objectSchemas.fieldNames[conditionalFieldName]
205
+ .storageResourceTags;
206
+
207
+ if (storageResourceTagsFieldName.includes(storageResourceTag)) {
208
+ continue conditionalFieldValuesLoop;
209
+ } else {
210
+ continue storageResourcesLoop;
211
+ }
212
+ }
213
+ }
214
+
215
+ tableName = storageResources[storageResourceTag].tableName;
216
+ _izContext.logger.debug('found tableName: ', tableName);
217
+ break;
218
+ }
219
+ }
220
+
221
+ if (!tableName) {
222
+ errorsFound.push('no valid dynamo storageResource found');
223
+ }
224
+
225
+ if (
226
+ element.hasOwnProperty('sortKeyValues') &&
227
+ Object.keys(element.sortKeyValues).length > 0 &&
228
+ !sortKeyFieldName
229
+ ) {
230
+ _izContext.logger.debug(
231
+ 'element have sortKeyValues but cannot find sortKey in object schemas'
232
+ );
233
+ errorsFound.push(
234
+ 'element have sortKeyValues but cannot find sortKey in object schemas'
235
+ );
236
+ }
237
+
238
+ if (errorsFound.length > 0) {
239
+ _izContext.logger.debug('Have errorsFound: ', errorsFound);
240
+ standardErrorParams.push(errorsFound);
241
+ await logicalResultsSharedLib.completeLogicalResultsMain(
242
+ ...standardErrorParams
243
+ );
244
+ return {
245
+ returnResponse: 'Have errorsFound'
246
+ };
247
+ }
248
+
249
+ // concatenate partitionKey
250
+ let partitionKeyReqIdentifiers = {};
251
+ for (const [fieldName, fieldNameValue] of Object.entries(
252
+ element.partitionKeyValues
253
+ )) {
254
+ partitionKeyReqIdentifiers[fieldName] = fieldNameValue.value;
255
+ }
256
+
257
+ // concatenate sortKey
258
+ if (element.hasOwnProperty('sortKeyValues')) {
259
+ for (const [fieldName, fieldNameValue] of Object.entries(
260
+ element.sortKeyValues
261
+ )) {
262
+ partitionKeyReqIdentifiers[fieldName] = fieldNameValue.value;
263
+ }
264
+ }
265
+ _izContext.logger.debug(
266
+ 'partitionKeyReqIdentifiers: ',
267
+ partitionKeyReqIdentifiers
268
+ );
269
+
270
+ let dynamoDbIdentifiers =
271
+ await identifierObj.dynamoDbIdentifiersByStorageResource(
272
+ _izContext,
273
+ objectSchemas,
274
+ storageResources,
275
+ partitionKeyReqIdentifiers
276
+ );
277
+ _izContext.logger.debug('dynamoDbIdentifiers: ', dynamoDbIdentifiers);
278
+
279
+ if (element.hasOwnProperty('sortKeyValues')) {
280
+ let comparison = 'equals';
281
+ if (element.comparison && element.comparison !== 'equals') {
282
+ comparison = element.comparison;
283
+ let sentMessageToPaginateProcessLogical = {
284
+ Message: JSON.stringify({
285
+ logicalResultId: logicalResultId,
286
+ tableName: tableName,
287
+ partitionKeyFieldName: partitionKeyFieldName,
288
+ sortKeyFieldName: sortKeyFieldName,
289
+ dynamoDbIdentifiers: dynamoDbIdentifiers,
290
+ comparison: comparison,
291
+ overWriteCache: overWriteCache,
292
+ callingFlow: callingFlow
293
+ }),
294
+ TopicArn: await snsSharedLib.snsTopicArnByFlowSchema(
295
+ _izContext,
296
+ 'PaginateProcessLogical_In'
297
+ )
298
+ };
299
+ _izContext.logger.debug(
300
+ 'Request params before send to topic PaginateProcessLogical_In: ',
301
+ sentMessageToPaginateProcessLogical
302
+ );
303
+
304
+ let resSNS = await sns.publishAsync(
305
+ _izContext,
306
+ sentMessageToPaginateProcessLogical
307
+ );
308
+ _izContext.logger.debug(
309
+ 'resSNS send to topic ProcessSortResult finish!',
310
+ resSNS
311
+ );
312
+
313
+ return {
314
+ returnResponse: 'Send message to PaginateProcessLogical_In'
315
+ };
316
+ } else {
317
+ let queryDatas = await dynamodbSharedLib.query(
318
+ _izContext,
319
+ dynamodbSharedLib.tableName(_izContext, tableName),
320
+ {
321
+ [partitionKeyFieldName]: dynamoDbIdentifiers[partitionKeyFieldName]
322
+ },
323
+ {
324
+ sortKeyCondition: {
325
+ comparison: comparison,
326
+ name: sortKeyFieldName,
327
+ value: dynamoDbIdentifiers[sortKeyFieldName]
328
+ }
329
+ }
330
+ );
331
+ _izContext.logger.debug('queryDatas result: ', queryDatas);
332
+
333
+ if (queryDatas.Items.length == 0) {
334
+ _izContext.logger.debug(`can't find data in ${tableName}`);
335
+ errorsFound.push(`can't find data in ${tableName}`);
336
+ standardErrorParams.push(errorsFound);
337
+ await logicalResultsSharedLib.completeLogicalResultsMain(
338
+ ...standardErrorParams
339
+ );
340
+ return {
341
+ returnResponse: 'Cannot find data in table'
342
+ };
343
+ }
344
+
345
+ for (const objInstanceBase of queryDatas.Items) {
346
+ await logicalResultsSharedLib.saveLogicalData(
347
+ _izContext,
348
+ logicalResultId,
349
+ logicalResultMain.objType,
350
+ objInstanceBase
351
+ );
352
+ }
353
+ }
354
+ } else {
355
+ // element don't have sortKey
356
+
357
+ if (sortKeyFieldName) {
358
+ _izContext.logger.debug(
359
+ 'identifiers have sortKey: query dynamo in PaginateProcessLogical'
360
+ );
361
+
362
+ let sentMessageToPaginateProcessLogical = {
363
+ Message: JSON.stringify({
364
+ logicalResultId: logicalResultId,
365
+ tableName: tableName,
366
+ partitionKeyFieldName: partitionKeyFieldName,
367
+ sortKeyFieldName: sortKeyFieldName,
368
+ dynamoDbIdentifiers: dynamoDbIdentifiers,
369
+ overWriteCache: overWriteCache,
370
+ callingFlow: callingFlow
371
+ }),
372
+ TopicArn: await snsSharedLib.snsTopicArnByFlowSchema(
373
+ _izContext,
374
+ 'PaginateProcessLogical_In'
375
+ )
376
+ };
377
+ _izContext.logger.debug(
378
+ 'Request params before send to topic PaginateProcessLogical_In: ',
379
+ sentMessageToPaginateProcessLogical
380
+ );
381
+
382
+ let resSNS = await sns.publishAsync(
383
+ _izContext,
384
+ sentMessageToPaginateProcessLogical
385
+ );
386
+ _izContext.logger.debug(
387
+ 'resSNS send to topic ProcessSortResult finish!',
388
+ resSNS
389
+ );
390
+
391
+ return {
392
+ returnResponse: 'Send message to PaginateProcessLogical_In'
393
+ };
394
+ } else {
395
+ _izContext.logger.debug('identifiers not have sortKey: save dataId');
396
+
397
+ let getDataByPartitionKey = await dynamodbSharedLib.getItem(
398
+ _izContext,
399
+ dynamodbSharedLib.tableName(_izContext, tableName),
400
+ {
401
+ [partitionKeyFieldName]: dynamoDbIdentifiers[partitionKeyFieldName]
402
+ }
403
+ );
404
+
405
+ if (getDataByPartitionKey) {
406
+ await logicalResultsSharedLib.saveLogicalData(
407
+ _izContext,
408
+ logicalResultId,
409
+ objType,
410
+ getDataByPartitionKey
411
+ );
412
+ } else {
413
+ _izContext.logger.debug(`can't find data in ${tableName}`);
414
+ errorsFound.push(`can't find data in ${tableName}`);
415
+ standardErrorParams.push(errorsFound);
416
+ await logicalResultsSharedLib.completeLogicalResultsMain(
417
+ ...standardErrorParams
418
+ );
419
+ return {
420
+ returnResponse: 'Cannot find data in table'
421
+ };
422
+ }
423
+ }
424
+ }
425
+
426
+ // complete processLogical and send message Out
427
+ await logicalResultsSharedLib.completeLogicalResultsMain(
428
+ _izContext,
429
+ logicalResultId,
430
+ element,
431
+ objType,
432
+ overWriteCache,
433
+ callingFlow,
434
+ 'complete',
435
+ []
436
+ );
437
+
438
+ //(<additionalProcessLogical>)
439
+ //(</additionalProcessLogical>)
440
+
441
+ return {
442
+ returnResponse: 'Function ProcessLogical is Finished'
443
+ };
444
+ } catch (err) {
445
+ _izContext.logger.error('error ProcessLogicalMain: ', err);
446
+ throw err;
447
+ }
448
+ }
@@ -0,0 +1,266 @@
1
+ import ejsLib from 'ejs';
2
+ import { readFile } from 'node:fs/promises';
3
+
4
+ const SHARED_TEMPLATE_URL = new URL('../shared/templates/', import.meta.url);
5
+ const FIND_DATA_SOURCE = 'app/src/generatedCode/FindData/source';
6
+ const FIND_DATA_HANDLER_ROOT = 'src/generatedCode/FindData/source';
7
+ const PROCESS_LOGICAL_SOURCE = 'app/src/generatedCode/ProcessLogical/source';
8
+ const PROCESS_LOGICAL_HANDLER_ROOT = 'src/generatedCode/ProcessLogical/source';
9
+ const YAML_PATH = 'app/sls_yaml/generatedCode/source/flow-system.yml';
10
+
11
+ const STORAGE_TYPES = { dynamoDB: 'dynamoDB', graph: 'graph' };
12
+
13
+ export async function emitSystemFindDataArtifacts(objects) {
14
+ return _emitSystemArtifacts(objects);
15
+ }
16
+
17
+ export function findDataManagedPaths() {
18
+ return [
19
+ `${FIND_DATA_SOURCE}/FindData_Main.js`,
20
+ `${FIND_DATA_SOURCE}/FindData_HdrSqs.js`,
21
+ `${PROCESS_LOGICAL_SOURCE}/ProcessLogical_Main.js`,
22
+ `${PROCESS_LOGICAL_SOURCE}/ProcessLogical_HdrSqs.js`,
23
+ `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_Main.js`,
24
+ `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_HdrDsq.js`,
25
+ `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_HdrSqs.js`,
26
+ YAML_PATH
27
+ ];
28
+ }
29
+
30
+ function _hasStorageObjects(objects) {
31
+ if (!Array.isArray(objects) || objects.length === 0) {
32
+ return false;
33
+ }
34
+ return objects.some(
35
+ (object) =>
36
+ object.storageResources &&
37
+ Object.keys(object.storageResources).length > 0
38
+ );
39
+ }
40
+
41
+ async function _emitSystemArtifacts(objects) {
42
+ if (!_hasStorageObjects(objects)) {
43
+ return [];
44
+ }
45
+
46
+ const hasFindDataEntries = buildFindDataEntries(objects).length > 0;
47
+
48
+ const artifacts = [];
49
+
50
+ if (hasFindDataEntries) {
51
+ const findDataMain = await render('FindData_Main.ejs', {
52
+ datas: buildFindDataEntries(objects)
53
+ });
54
+ artifacts.push(
55
+ { path: `${FIND_DATA_SOURCE}/FindData_Main.js`, content: findDataMain },
56
+ {
57
+ path: `${FIND_DATA_SOURCE}/FindData_HdrSqs.js`,
58
+ content: await render('FindData_HdrSqs.ejs', {})
59
+ }
60
+ );
61
+ }
62
+
63
+ artifacts.push(
64
+ {
65
+ path: `${PROCESS_LOGICAL_SOURCE}/ProcessLogical_Main.js`,
66
+ content: await render('ProcessLogical_Main.ejs', {})
67
+ },
68
+ {
69
+ path: `${PROCESS_LOGICAL_SOURCE}/ProcessLogical_HdrSqs.js`,
70
+ content: await render('ProcessLogical_HdrSqs.ejs', {})
71
+ },
72
+ {
73
+ path: `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_Main.js`,
74
+ content: await render('PaginateProcessLogical_Main.ejs', {})
75
+ },
76
+ {
77
+ path: `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_HdrDsq.js`,
78
+ content: await render('PaginateProcessLogical_HdrDsq.ejs', {})
79
+ },
80
+ {
81
+ path: `${PROCESS_LOGICAL_SOURCE}/PaginateProcessLogical_HdrSqs.js`,
82
+ content: await render('PaginateProcessLogical_HdrSqs.ejs', {})
83
+ },
84
+ {
85
+ path: YAML_PATH,
86
+ content: renderSystemYaml()
87
+ }
88
+ );
89
+
90
+ return artifacts;
91
+ }
92
+
93
+ function renderSystemYaml() {
94
+ const findDataYaml = renderFindDataYaml();
95
+ const processLogicalYaml = renderProcessLogicalYaml();
96
+ const processLogicalPaginateDsqYaml = renderProcessLogicalPaginateDsqYaml();
97
+ const processLogicalPaginateSqsYaml = renderProcessLogicalPaginateSqsYaml();
98
+ return [
99
+ findDataYaml,
100
+ processLogicalYaml,
101
+ processLogicalPaginateDsqYaml,
102
+ processLogicalPaginateSqsYaml
103
+ ].join('\n\n');
104
+ }
105
+
106
+ function renderFindDataYaml() {
107
+ return _renderSqsYaml({
108
+ functionName: 'FindData',
109
+ handlerRoot: FIND_DATA_HANDLER_ROOT,
110
+ roleName: 'ProcFindDataRole'
111
+ });
112
+ }
113
+
114
+ function renderProcessLogicalYaml() {
115
+ return _renderSqsYaml({
116
+ functionName: 'ProcessLogical',
117
+ handlerRoot: PROCESS_LOGICAL_HANDLER_ROOT,
118
+ roleName: 'ProcFindDataRole'
119
+ });
120
+ }
121
+
122
+ function renderProcessLogicalPaginateDsqYaml() {
123
+ return _renderSqsYaml({
124
+ functionName: 'PaginateProcessLogical',
125
+ handlerType: 'HdrDsq',
126
+ handlerRoot: PROCESS_LOGICAL_HANDLER_ROOT,
127
+ roleName: 'ProcFindDataRole'
128
+ });
129
+ }
130
+
131
+ function renderProcessLogicalPaginateSqsYaml() {
132
+ return _renderSqsYaml({
133
+ functionName: 'PaginateProcessLogical',
134
+ handlerRoot: PROCESS_LOGICAL_HANDLER_ROOT,
135
+ roleName: 'ProcFindDataRole'
136
+ });
137
+ }
138
+
139
+ function _renderSqsYaml({
140
+ functionName,
141
+ handlerType = 'HdrSqs',
142
+ handlerRoot,
143
+ roleName
144
+ }) {
145
+ const upperHandlerType = handlerType;
146
+ const handlerPath = `${handlerRoot}/${functionName}_${upperHandlerType}.main`;
147
+ const name = `${functionName}${upperHandlerType}`;
148
+ return `${name}:
149
+ handler: ${handlerPath}
150
+ name: \${self:custom.iz_resourcePrefix}${name}
151
+ role: ${roleName}
152
+ #<#${name}FunctionSetting#>
153
+ #<#/${name}FunctionSetting#>
154
+ events:
155
+ - sqs:
156
+ arn: arn:aws:sqs:\${self:custom.iz_region}:\${self:custom.iz_accountId}:\${self:custom.iz_resourcePrefix}${name}
157
+ batchSize: 10`;
158
+ }
159
+
160
+ function buildFindDataEntries(objects) {
161
+ return objects
162
+ .filter(
163
+ (object) =>
164
+ object.storageResources &&
165
+ Object.keys(object.storageResources).length > 0
166
+ )
167
+ .map((object) => {
168
+ const objectType = object.objectType;
169
+ const storageResources = object.storageResources;
170
+ const fieldNames = object.fieldNames || {};
171
+ const identifiers = object.identifiers || [];
172
+ const identifierNames = new Set(
173
+ identifiers.map((id) => id.fieldName).filter(Boolean)
174
+ );
175
+
176
+ const fieldEntries = [];
177
+
178
+ for (const [field, schemas] of Object.entries(fieldNames)) {
179
+ if (schemas.type !== 'string' && schemas.type !== 'number') {
180
+ continue;
181
+ }
182
+
183
+ const storageResourceTags = schemas.storageResourceTags;
184
+ if (
185
+ !Array.isArray(storageResourceTags) ||
186
+ storageResourceTags.length === 0
187
+ ) {
188
+ continue;
189
+ }
190
+
191
+ const collectStorageDetail = {};
192
+ const getInIdentifiers = identifierNames.has(field);
193
+
194
+ for (const storageTag of storageResourceTags) {
195
+ const storageDetail = storageResources[storageTag];
196
+ if (!storageDetail) {
197
+ continue;
198
+ }
199
+
200
+ if (storageDetail.storageType === STORAGE_TYPES.graph) {
201
+ if (!collectStorageDetail[STORAGE_TYPES.graph]) {
202
+ collectStorageDetail[STORAGE_TYPES.graph] = {
203
+ graphServerTag: storageDetail.graphServerTag,
204
+ getInIdentifiers
205
+ };
206
+ }
207
+ } else if (storageDetail.storageType === STORAGE_TYPES.dynamoDB) {
208
+ if (!collectStorageDetail[STORAGE_TYPES.dynamoDB]) {
209
+ collectStorageDetail[STORAGE_TYPES.dynamoDB] = {
210
+ tableName: storageDetail.tableName
211
+ };
212
+ }
213
+ }
214
+ }
215
+
216
+ if (
217
+ collectStorageDetail[STORAGE_TYPES.graph] &&
218
+ collectStorageDetail[STORAGE_TYPES.dynamoDB]
219
+ ) {
220
+ delete collectStorageDetail[STORAGE_TYPES.graph];
221
+ }
222
+
223
+ if (Object.keys(collectStorageDetail).length > 0) {
224
+ fieldEntries.push({ [field]: collectStorageDetail });
225
+ }
226
+ }
227
+
228
+ if (Array.isArray(object.addOnDataStructure)) {
229
+ for (const versionedData of object.addOnDataStructure) {
230
+ if (versionedData.type === 'versionedData') {
231
+ const versionedStorageTag = versionedData.storageResourceTag;
232
+ const versionedFieldNames = versionedData.fieldNames || {};
233
+ const storageDetail = storageResources[versionedStorageTag];
234
+
235
+ if (
236
+ storageDetail &&
237
+ storageDetail.storageType === STORAGE_TYPES.graph
238
+ ) {
239
+ const versionedStorageDetail = {
240
+ [STORAGE_TYPES.graph]: {
241
+ graphServerTag: storageDetail.graphServerTag,
242
+ versionedDataLabel: versionedData.versionedDataLabel
243
+ }
244
+ };
245
+
246
+ for (const [key, value] of Object.entries(versionedFieldNames)) {
247
+ if (value.type === 'string' || value.type === 'number') {
248
+ fieldEntries.push({ [key]: versionedStorageDetail });
249
+ }
250
+ }
251
+ }
252
+ }
253
+ }
254
+ }
255
+
256
+ return { objectType, fieldNames: fieldEntries };
257
+ })
258
+ .filter((entry) => entry.fieldNames.length > 0);
259
+ }
260
+
261
+ async function render(templateName, data) {
262
+ return ejsLib.render(
263
+ await readFile(new URL(templateName, SHARED_TEMPLATE_URL), 'utf8'),
264
+ data
265
+ );
266
+ }