@ruiapp/rapid-core 0.11.6 → 0.11.7

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 (204) hide show
  1. package/CHANGELOG.md +11 -11
  2. package/dist/core/pluginManager.d.ts +2 -0
  3. package/dist/core/server.d.ts +3 -0
  4. package/dist/dataAccess/entityManager.d.ts +3 -3
  5. package/dist/index.js +102 -38
  6. package/dist/server.d.ts +1 -0
  7. package/dist/types.d.ts +2 -0
  8. package/package.json +1 -1
  9. package/rollup.config.js +16 -16
  10. package/src/bootstrapApplicationConfig.ts +904 -904
  11. package/src/core/ExpressionInterpreter.ts +45 -45
  12. package/src/core/actionHandler.ts +24 -24
  13. package/src/core/actionHandlers/ifActionHandler.ts +27 -27
  14. package/src/core/eventManager.ts +20 -20
  15. package/src/core/facility.ts +7 -7
  16. package/src/core/http/formDataParser.ts +87 -87
  17. package/src/core/http-types.ts +4 -4
  18. package/src/core/pluginManager.ts +202 -193
  19. package/src/core/providers/runtimeProvider.ts +5 -5
  20. package/src/core/request.ts +96 -96
  21. package/src/core/response.ts +84 -84
  22. package/src/core/routeContext.ts +127 -127
  23. package/src/core/routes/healthz.ts +20 -20
  24. package/src/core/routes/index.ts +3 -3
  25. package/src/core/routesBuilder.ts +129 -122
  26. package/src/core/server.ts +161 -158
  27. package/src/dataAccess/columnTypeMapper.ts +22 -22
  28. package/src/dataAccess/dataAccessTypes.ts +165 -165
  29. package/src/dataAccess/dataAccessor.ts +135 -135
  30. package/src/dataAccess/entityManager.ts +2031 -1996
  31. package/src/dataAccess/entityMapper.ts +111 -111
  32. package/src/dataAccess/entityValidator.ts +33 -33
  33. package/src/dataAccess/propertyMapper.ts +28 -28
  34. package/src/deno-std/assert/assert.ts +9 -9
  35. package/src/deno-std/assert/assertion_error.ts +7 -7
  36. package/src/deno-std/datetime/to_imf.ts +32 -32
  37. package/src/deno-std/encoding/base64.ts +141 -141
  38. package/src/deno-std/http/cookie.ts +372 -372
  39. package/src/facilities/cache/CacheFacilityTypes.ts +29 -29
  40. package/src/facilities/cache/CacheFactory.ts +31 -31
  41. package/src/facilities/cache/MemoryCache.ts +58 -58
  42. package/src/facilities/cache/MemoryCacheProvider.ts +15 -15
  43. package/src/facilities/log/LogFacility.ts +35 -35
  44. package/src/helpers/dbTransactionHelper.ts +42 -42
  45. package/src/helpers/entityHelper.ts +89 -89
  46. package/src/helpers/filterHelper.ts +148 -148
  47. package/src/helpers/inputHelper.ts +11 -11
  48. package/src/helpers/licenseHelper.ts +29 -29
  49. package/src/helpers/metaHelper.ts +111 -111
  50. package/src/helpers/runCollectionEntityActionHandler.ts +58 -58
  51. package/src/index.ts +85 -85
  52. package/src/plugins/auth/AuthPlugin.ts +107 -107
  53. package/src/plugins/auth/AuthPluginTypes.ts +11 -11
  54. package/src/plugins/auth/actionHandlers/changePassword.ts +61 -61
  55. package/src/plugins/auth/actionHandlers/createSession.ts +68 -68
  56. package/src/plugins/auth/actionHandlers/deleteSession.ts +18 -18
  57. package/src/plugins/auth/actionHandlers/getMyProfile.ts +28 -28
  58. package/src/plugins/auth/actionHandlers/index.ts +8 -8
  59. package/src/plugins/auth/actionHandlers/resetPassword.ts +45 -45
  60. package/src/plugins/auth/models/AccessToken.ts +56 -56
  61. package/src/plugins/auth/models/index.ts +3 -3
  62. package/src/plugins/auth/routes/changePassword.ts +15 -15
  63. package/src/plugins/auth/routes/getMyProfile.ts +15 -15
  64. package/src/plugins/auth/routes/index.ts +7 -7
  65. package/src/plugins/auth/routes/resetPassword.ts +15 -15
  66. package/src/plugins/auth/routes/signin.ts +15 -15
  67. package/src/plugins/auth/routes/signout.ts +15 -15
  68. package/src/plugins/auth/services/AuthService.ts +66 -66
  69. package/src/plugins/cronJob/CronJobPlugin.ts +104 -104
  70. package/src/plugins/cronJob/CronJobPluginTypes.ts +44 -44
  71. package/src/plugins/cronJob/actionHandlers/index.ts +4 -4
  72. package/src/plugins/cronJob/actionHandlers/runCronJob.ts +32 -32
  73. package/src/plugins/cronJob/entityWatchers/cronJobEntityWatchers.ts +24 -24
  74. package/src/plugins/cronJob/entityWatchers/index.ts +4 -4
  75. package/src/plugins/cronJob/models/CronJob.ts +129 -129
  76. package/src/plugins/cronJob/models/index.ts +3 -3
  77. package/src/plugins/cronJob/routes/index.ts +3 -3
  78. package/src/plugins/cronJob/routes/runCronJob.ts +15 -15
  79. package/src/plugins/cronJob/services/CronJobService.ts +255 -255
  80. package/src/plugins/dataManage/DataManagePlugin.ts +165 -165
  81. package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +15 -15
  82. package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +17 -17
  83. package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +81 -81
  84. package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +20 -20
  85. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +47 -45
  86. package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +20 -20
  87. package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +27 -27
  88. package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +30 -30
  89. package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +22 -22
  90. package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +15 -15
  91. package/src/plugins/dataManage/actionHandlers/saveEntity.ts +46 -46
  92. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +38 -38
  93. package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
  94. package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
  95. package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
  96. package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
  97. package/src/plugins/fileManage/actionHandlers/uploadFile.ts +33 -33
  98. package/src/plugins/fileManage/routes/downloadDocument.ts +15 -15
  99. package/src/plugins/fileManage/routes/downloadFile.ts +15 -15
  100. package/src/plugins/fileManage/routes/index.ts +5 -5
  101. package/src/plugins/fileManage/routes/uploadFile.ts +15 -15
  102. package/src/plugins/license/LicensePlugin.ts +79 -79
  103. package/src/plugins/license/LicensePluginTypes.ts +95 -95
  104. package/src/plugins/license/LicenseService.ts +141 -141
  105. package/src/plugins/license/actionHandlers/getLicense.ts +18 -18
  106. package/src/plugins/license/actionHandlers/index.ts +5 -5
  107. package/src/plugins/license/actionHandlers/updateLicense.ts +24 -24
  108. package/src/plugins/license/helpers/certHelper.ts +21 -21
  109. package/src/plugins/license/helpers/cryptoHelper.ts +47 -47
  110. package/src/plugins/license/models/index.ts +1 -1
  111. package/src/plugins/license/routes/getLicense.ts +15 -15
  112. package/src/plugins/license/routes/index.ts +4 -4
  113. package/src/plugins/license/routes/updateLicense.ts +15 -15
  114. package/src/plugins/mail/MailPlugin.ts +74 -74
  115. package/src/plugins/mail/MailPluginTypes.ts +27 -27
  116. package/src/plugins/mail/MailService.ts +38 -38
  117. package/src/plugins/mail/actionHandlers/index.ts +3 -3
  118. package/src/plugins/mail/models/index.ts +1 -1
  119. package/src/plugins/mail/routes/index.ts +1 -1
  120. package/src/plugins/metaManage/MetaManagePlugin.ts +198 -198
  121. package/src/plugins/metaManage/actionHandlers/getMetaModelDetail.ts +10 -10
  122. package/src/plugins/metaManage/actionHandlers/listMetaModels.ts +10 -10
  123. package/src/plugins/metaManage/actionHandlers/listMetaRoutes.ts +10 -10
  124. package/src/plugins/metaManage/services/MetaService.ts +376 -376
  125. package/src/plugins/notification/NotificationPlugin.ts +68 -68
  126. package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
  127. package/src/plugins/notification/NotificationService.ts +25 -25
  128. package/src/plugins/notification/actionHandlers/index.ts +3 -3
  129. package/src/plugins/notification/models/Notification.ts +60 -60
  130. package/src/plugins/notification/models/index.ts +3 -3
  131. package/src/plugins/notification/routes/index.ts +1 -1
  132. package/src/plugins/routeManage/RouteManagePlugin.ts +64 -64
  133. package/src/plugins/routeManage/actionHandlers/httpProxy.ts +13 -13
  134. package/src/plugins/routeManage/actionHandlers/mock.ts +28 -28
  135. package/src/plugins/sequence/SequencePlugin.ts +146 -146
  136. package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
  137. package/src/plugins/sequence/SequenceService.ts +92 -92
  138. package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
  139. package/src/plugins/sequence/actionHandlers/index.ts +4 -4
  140. package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -49
  141. package/src/plugins/sequence/models/SequenceRule.ts +42 -42
  142. package/src/plugins/sequence/models/index.ts +4 -4
  143. package/src/plugins/sequence/routes/generateSn.ts +15 -15
  144. package/src/plugins/sequence/routes/index.ts +3 -3
  145. package/src/plugins/sequence/segment-utility.ts +11 -11
  146. package/src/plugins/sequence/segments/autoIncrement.ts +90 -90
  147. package/src/plugins/sequence/segments/dayOfMonth.ts +19 -19
  148. package/src/plugins/sequence/segments/index.ts +9 -9
  149. package/src/plugins/sequence/segments/literal.ts +16 -16
  150. package/src/plugins/sequence/segments/month.ts +19 -19
  151. package/src/plugins/sequence/segments/parameter.ts +20 -20
  152. package/src/plugins/sequence/segments/year.ts +19 -19
  153. package/src/plugins/serverOperation/ServerOperationPlugin.ts +93 -93
  154. package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +25 -25
  155. package/src/plugins/serverOperation/actionHandlers/index.ts +4 -4
  156. package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +20 -20
  157. package/src/plugins/setting/SettingPlugin.ts +68 -68
  158. package/src/plugins/setting/SettingPluginTypes.ts +37 -37
  159. package/src/plugins/setting/SettingService.ts +222 -222
  160. package/src/plugins/setting/actionHandlers/getSystemSettingValues.ts +30 -30
  161. package/src/plugins/setting/actionHandlers/getUserSettingValues.ts +38 -38
  162. package/src/plugins/setting/actionHandlers/index.ts +6 -6
  163. package/src/plugins/setting/actionHandlers/setSystemSettingValues.ts +30 -30
  164. package/src/plugins/setting/models/SystemSettingGroupSetting.ts +57 -57
  165. package/src/plugins/setting/models/SystemSettingItem.ts +48 -48
  166. package/src/plugins/setting/models/SystemSettingItemSetting.ts +73 -73
  167. package/src/plugins/setting/models/UserSettingGroupSetting.ts +57 -57
  168. package/src/plugins/setting/models/UserSettingItem.ts +55 -55
  169. package/src/plugins/setting/models/UserSettingItemSetting.ts +73 -73
  170. package/src/plugins/setting/models/index.ts +8 -8
  171. package/src/plugins/setting/routes/getSystemSettingValues.ts +15 -15
  172. package/src/plugins/setting/routes/getUserSettingValues.ts +15 -15
  173. package/src/plugins/setting/routes/index.ts +5 -5
  174. package/src/plugins/setting/routes/setSystemSettingValues.ts +15 -15
  175. package/src/plugins/stateMachine/StateMachinePlugin.ts +196 -196
  176. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
  177. package/src/plugins/stateMachine/actionHandlers/index.ts +4 -4
  178. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +54 -54
  179. package/src/plugins/stateMachine/models/StateMachine.ts +42 -42
  180. package/src/plugins/stateMachine/models/index.ts +3 -3
  181. package/src/plugins/stateMachine/routes/index.ts +3 -3
  182. package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -15
  183. package/src/plugins/stateMachine/stateMachineHelper.ts +36 -36
  184. package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
  185. package/src/plugins/webhooks/pluginConfig.ts +75 -75
  186. package/src/polyfill.ts +5 -5
  187. package/src/proxy/mod.ts +38 -38
  188. package/src/proxy/types.ts +21 -21
  189. package/src/queryBuilder/index.ts +1 -1
  190. package/src/queryBuilder/queryBuilder.ts +755 -755
  191. package/src/server.ts +567 -556
  192. package/src/types/cron-job-types.ts +71 -71
  193. package/src/types.ts +884 -882
  194. package/src/utilities/accessControlUtility.ts +33 -33
  195. package/src/utilities/entityUtility.ts +18 -18
  196. package/src/utilities/errorUtility.ts +15 -15
  197. package/src/utilities/fsUtility.ts +137 -137
  198. package/src/utilities/httpUtility.ts +19 -19
  199. package/src/utilities/jwtUtility.ts +26 -26
  200. package/src/utilities/passwordUtility.ts +26 -26
  201. package/src/utilities/pathUtility.ts +14 -14
  202. package/src/utilities/timeUtility.ts +17 -17
  203. package/src/utilities/typeUtility.ts +15 -15
  204. package/tsconfig.json +19 -19
package/CHANGELOG.md CHANGED
@@ -1,11 +1,11 @@
1
- # @ruiapp/rapid-core
2
-
3
- ## 0.7.0
4
-
5
- - feature: added server.registerCronJob to allow plugins register cron jobs.
6
-
7
- ## 0.2.4
8
-
9
- ### Patch Changes
10
-
11
- - 146a99d: add between filterMode, range is deprecated
1
+ # @ruiapp/rapid-core
2
+
3
+ ## 0.7.0
4
+
5
+ - feature: added server.registerCronJob to allow plugins register cron jobs.
6
+
7
+ ## 0.2.4
8
+
9
+ ### Patch Changes
10
+
11
+ - 146a99d: add between filterMode, range is deprecated
@@ -40,6 +40,8 @@ declare class PluginManager {
40
40
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
41
41
  /** 在执行 action hanlder 前调用。 */
42
42
  beforeRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig): Promise<void>;
43
+ /** 在执行 action handler 后调用。 */
44
+ afterRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig, error?: Error): Promise<void>;
43
45
  /** 在创建实体前调用。 */
44
46
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
45
47
  /** 在更新实体前调用。 */
@@ -34,6 +34,7 @@ export interface IRpdServer {
34
34
  runActionHandlers(handlerContext: ActionHandlerContext, actions: RpdRouteActionConfig[]): Promise<void>;
35
35
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
36
36
  beforeRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig): Promise<void>;
37
+ afterRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig, error?: Error): Promise<void>;
37
38
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
38
39
  beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any): Promise<void>;
39
40
  registerCronJob(job: CronJobConfiguration): void;
@@ -112,6 +113,8 @@ export interface RapidPlugin {
112
113
  beforeRunRouteActions?: (server: IRpdServer, handlerContext: ActionHandlerContext) => Promise<any>;
113
114
  /** 在执行 action hanlder 前调用。 */
114
115
  beforeRunActionHandler?: (server: IRpdServer, handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig) => Promise<any>;
116
+ /** 在执行 action handler 后调用。 */
117
+ afterRunActionHandler?: (server: IRpdServer, handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig, error?: Error) => Promise<any>;
115
118
  /** 在创建实体前调用。 */
116
119
  beforeCreateEntity?: (server: IRpdServer, model: RpdDataModel, options: CreateEntityOptions) => Promise<any>;
117
120
  /** 在更新实体前调用。 */
@@ -33,7 +33,7 @@ export default class EntityManager<TEntity = any> {
33
33
  createEntity(options: CreateEntityOptions, plugin?: RapidPlugin): Promise<TEntity>;
34
34
  updateEntityById(options: UpdateEntityByIdOptions, plugin?: RapidPlugin): Promise<TEntity>;
35
35
  count(options: CountEntityOptions): Promise<number>;
36
- deleteById(options: DeleteEntityByIdOptions | string | number, plugin?: RapidPlugin): Promise<void>;
37
- addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<void>;
38
- removeRelations(options: RemoveEntityRelationsOptions, plugin?: RapidPlugin): Promise<void>;
36
+ deleteById(options: DeleteEntityByIdOptions | string | number, plugin?: RapidPlugin): Promise<TEntity>;
37
+ addRelations(options: AddEntityRelationsOptions, plugin?: RapidPlugin): Promise<any>;
38
+ removeRelations(options: RemoveEntityRelationsOptions, plugin?: RapidPlugin): Promise<any>;
39
39
  }
package/dist/index.js CHANGED
@@ -936,6 +936,14 @@ class PluginManager {
936
936
  }
937
937
  }
938
938
  }
939
+ /** 在执行 action handler 后调用。 */
940
+ async afterRunActionHandler(handlerContext, actionConfig, error) {
941
+ for (const plugin of this.#plugins) {
942
+ if (plugin.afterRunActionHandler) {
943
+ await plugin.afterRunActionHandler(this.#server, handlerContext, actionConfig, error);
944
+ }
945
+ }
946
+ }
939
947
  /** 在创建实体前调用。 */
940
948
  async beforeCreateEntity(model, options) {
941
949
  for (const plugin of this.#plugins) {
@@ -1044,9 +1052,19 @@ async function executeHandlerOfActions(server, routeConfig, handlerContext) {
1044
1052
  throw new Error("Unknown handler: " + actionCode);
1045
1053
  }
1046
1054
  await server.beforeRunActionHandler(handlerContext, actionConfig);
1047
- const result = handler(handlerContext, actionConfig.config);
1048
- if (result instanceof Promise) {
1049
- await result;
1055
+ let err;
1056
+ try {
1057
+ const result = handler(handlerContext, actionConfig.config);
1058
+ if (result instanceof Promise) {
1059
+ await result;
1060
+ }
1061
+ }
1062
+ catch (error) {
1063
+ err = error;
1064
+ throw error;
1065
+ }
1066
+ finally {
1067
+ await server.afterRunActionHandler(handlerContext, actionConfig, err);
1050
1068
  }
1051
1069
  }
1052
1070
  }
@@ -3174,7 +3192,7 @@ const UNDELETED_ENTITY_FILTER_OPTIONS = {
3174
3192
  field: "deletedAt",
3175
3193
  };
3176
3194
  function tryAddUndeletedEntityFilter(model, baseModel, filters) {
3177
- let isEntitySoftDeleteEnabled = baseModel ? (baseModel.softDelete || model.softDelete) : model.softDelete;
3195
+ let isEntitySoftDeleteEnabled = baseModel ? baseModel.softDelete || model.softDelete : model.softDelete;
3178
3196
  if (!isEntitySoftDeleteEnabled) {
3179
3197
  return filters;
3180
3198
  }
@@ -3406,8 +3424,8 @@ async function findManyRelationLinksViaLinkTable(options) {
3406
3424
  const command = `SELECT * FROM ${server.queryBuilder.quoteTable({
3407
3425
  schema: relationProperty.linkSchema,
3408
3426
  tableName: relationProperty.linkTableName,
3409
- })} WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = ANY($1::int[])
3410
- ORDER BY id
3427
+ })} WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = ANY($1::int[])
3428
+ ORDER BY id
3411
3429
  `;
3412
3430
  const params = [mainEntityIds];
3413
3431
  const relationLinks = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
@@ -4060,7 +4078,7 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
4060
4078
  await server.queryDatabaseObject(`DELETE FROM ${server.queryBuilder.quoteTable({
4061
4079
  schema: property.linkSchema,
4062
4080
  tableName: property.linkTableName,
4063
- })} WHERE ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = $1
4081
+ })} WHERE ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = $1
4064
4082
  AND ${server.queryBuilder.quoteObject(property.targetIdColumnName)} <> ALL($2::int[])`, [id, targetIdsToKeep], routeContext?.getDbTransactionClient());
4065
4083
  }
4066
4084
  else {
@@ -4089,8 +4107,8 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
4089
4107
  await server.queryDatabaseObject(`UPDATE ${server.queryBuilder.quoteTable({
4090
4108
  schema: relationModel.schema,
4091
4109
  tableName: relationModel.tableName,
4092
- })}
4093
- SET ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = null
4110
+ })}
4111
+ SET ${server.queryBuilder.quoteObject(property.selfIdColumnName)} = null
4094
4112
  WHERE id = ANY($1::int[])`, [targetIdsToRemove], routeContext?.getDbTransactionClient());
4095
4113
  }
4096
4114
  else {
@@ -4298,7 +4316,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4298
4316
  if (!entity) {
4299
4317
  return;
4300
4318
  }
4301
- const isEntitySoftDeleteEnabled = model.base ? (baseModel.softDelete || model.softDelete) : model.softDelete;
4319
+ const isEntitySoftDeleteEnabled = model.base ? baseModel.softDelete || model.softDelete : model.softDelete;
4302
4320
  if (isEntitySoftDeleteEnabled) {
4303
4321
  if (entity.deletedAt) {
4304
4322
  return;
@@ -4388,7 +4406,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4388
4406
  await server.queryDatabaseObject(`DELETE FROM ${server.queryBuilder.quoteTable({
4389
4407
  schema: relationProperty.linkSchema,
4390
4408
  tableName: relationProperty.linkTableName,
4391
- })}
4409
+ })}
4392
4410
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
4393
4411
  }
4394
4412
  else {
@@ -4398,8 +4416,8 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4398
4416
  await server.queryDatabaseObject(`UPDATE ${server.queryBuilder.quoteTable({
4399
4417
  schema: relationModel.schema,
4400
4418
  tableName: relationModel.tableName,
4401
- })}
4402
- SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
4419
+ })}
4420
+ SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
4403
4421
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
4404
4422
  }
4405
4423
  }
@@ -4423,6 +4441,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4423
4441
  sender: plugin,
4424
4442
  routeContext,
4425
4443
  });
4444
+ return entity;
4426
4445
  }
4427
4446
  class EntityManager {
4428
4447
  #server;
@@ -4491,30 +4510,39 @@ class EntityManager {
4491
4510
  throw new Error(`Operation 'addRelations' is only supported on property of 'many' relation`);
4492
4511
  }
4493
4512
  const { queryBuilder } = server;
4513
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4514
+ const relationTargetEntities = [];
4494
4515
  if (relationProperty.linkTableName) {
4495
4516
  for (const relation of relations) {
4496
4517
  const command = `INSERT INTO ${queryBuilder.quoteTable({
4497
4518
  schema: relationProperty.linkSchema,
4498
4519
  tableName: relationProperty.linkTableName,
4499
- })} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)})
4500
- SELECT $1, $2 WHERE NOT EXISTS (
4501
- SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}
4502
- FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4503
- WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
4520
+ })} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)})
4521
+ SELECT $1, $2 WHERE NOT EXISTS (
4522
+ SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}
4523
+ FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4524
+ WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2
4504
4525
  )`;
4505
4526
  const targetId = relation[relationProperty.targetIdColumnName] || relation.id;
4506
4527
  const params = [selfId, targetId];
4507
- await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
4528
+ const result = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
4529
+ if (result?.length > 0) {
4530
+ const targetEntity = await targetEntityManager.findById({
4531
+ routeContext,
4532
+ id: targetId,
4533
+ });
4534
+ relationTargetEntities.push(targetEntity);
4535
+ }
4508
4536
  }
4509
4537
  }
4510
4538
  else {
4511
- const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4512
4539
  for (const relation of relations) {
4513
4540
  relation[relationProperty.selfIdColumnName] = selfId;
4514
- await targetEntityManager.createEntity({
4541
+ const targetEntity = await targetEntityManager.createEntity({
4515
4542
  routeContext,
4516
4543
  entity: relation,
4517
4544
  });
4545
+ relationTargetEntities.push(targetEntity);
4518
4546
  }
4519
4547
  }
4520
4548
  await server.emitEvent({
@@ -4525,10 +4553,17 @@ class EntityManager {
4525
4553
  entity,
4526
4554
  property,
4527
4555
  relations,
4556
+ relationTargetEntities,
4528
4557
  },
4529
4558
  sender: plugin,
4530
4559
  routeContext: options.routeContext,
4531
4560
  });
4561
+ return {
4562
+ entity,
4563
+ property,
4564
+ relations,
4565
+ relationTargetEntities,
4566
+ };
4532
4567
  }
4533
4568
  async removeRelations(options, plugin) {
4534
4569
  const server = this.#server;
@@ -4549,12 +4584,21 @@ class EntityManager {
4549
4584
  throw new Error(`Operation 'removeRelations' is only supported on property of 'many' relation`);
4550
4585
  }
4551
4586
  const { queryBuilder } = server;
4587
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4588
+ const relationTargetEntities = [];
4552
4589
  if (relationProperty.linkTableName) {
4553
4590
  for (const relation of relations) {
4554
- const command = `DELETE FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4591
+ const command = `DELETE FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4555
4592
  WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2;`;
4556
4593
  const params = [id, relation.id];
4557
- await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
4594
+ const result = await server.queryDatabaseObject(command, params, routeContext?.getDbTransactionClient());
4595
+ if (result?.length > 0) {
4596
+ const targetEntity = await targetEntityManager.findById({
4597
+ routeContext,
4598
+ id: relation.id,
4599
+ });
4600
+ relationTargetEntities.push(targetEntity);
4601
+ }
4558
4602
  }
4559
4603
  }
4560
4604
  await server.emitEvent({
@@ -4565,10 +4609,17 @@ class EntityManager {
4565
4609
  entity,
4566
4610
  property,
4567
4611
  relations,
4612
+ relationTargetEntities,
4568
4613
  },
4569
4614
  sender: plugin,
4570
4615
  routeContext: options.routeContext,
4571
4616
  });
4617
+ return {
4618
+ entity,
4619
+ property,
4620
+ relations,
4621
+ relationTargetEntities,
4622
+ };
4572
4623
  }
4573
4624
  }
4574
4625
 
@@ -5017,7 +5068,15 @@ class RapidServer {
5017
5068
  throw new Error("Unknown handler: " + actionCode);
5018
5069
  }
5019
5070
  await this.beforeRunActionHandler(handlerContext, action);
5020
- await handler(handlerContext, action.config);
5071
+ try {
5072
+ await handler(handlerContext, action.config);
5073
+ }
5074
+ catch (error) {
5075
+ throw error;
5076
+ }
5077
+ finally {
5078
+ await this.afterRunActionHandler(handlerContext, action);
5079
+ }
5021
5080
  }
5022
5081
  }
5023
5082
  async beforeRunRouteActions(handlerContext) {
@@ -5026,6 +5085,9 @@ class RapidServer {
5026
5085
  async beforeRunActionHandler(handlerContext, actionConfig) {
5027
5086
  await this.#pluginManager.beforeRunActionHandler(handlerContext, actionConfig);
5028
5087
  }
5088
+ async afterRunActionHandler(handlerContext, actionConfig, error) {
5089
+ await this.#pluginManager.afterRunActionHandler(handlerContext, actionConfig, error);
5090
+ }
5029
5091
  async beforeCreateEntity(model, options) {
5030
5092
  await this.#pluginManager.beforeCreateEntity(model, options);
5031
5093
  }
@@ -5903,9 +5965,9 @@ class MetaService {
5903
5965
  await server.tryQueryDatabaseObject(`COMMENT ON TABLE ${queryBuilder.quoteTable(model)} IS ${queryBuilder.formatValueToSqlLiteral(model.name)};`, []);
5904
5966
  }
5905
5967
  }
5906
- const sqlQueryColumnInformations = `SELECT c.table_schema, c.table_name, c.column_name, c.ordinal_position, d.description, c.data_type, c.udt_name, c.is_nullable, c.column_default, c.character_maximum_length, c.numeric_precision, c.numeric_scale
5907
- FROM information_schema.columns c
5908
- INNER JOIN pg_catalog.pg_statio_all_tables st ON (st.schemaname = c.table_schema and st.relname = c.table_name)
5968
+ const sqlQueryColumnInformations = `SELECT c.table_schema, c.table_name, c.column_name, c.ordinal_position, d.description, c.data_type, c.udt_name, c.is_nullable, c.column_default, c.character_maximum_length, c.numeric_precision, c.numeric_scale
5969
+ FROM information_schema.columns c
5970
+ INNER JOIN pg_catalog.pg_statio_all_tables st ON (st.schemaname = c.table_schema and st.relname = c.table_name)
5909
5971
  LEFT JOIN pg_catalog.pg_description d ON (d.objoid = st.relid and d.objsubid = c.ordinal_position);`;
5910
5972
  const columnsInDb = await server.queryDatabaseObject(sqlQueryColumnInformations, []);
5911
5973
  for (const model of applicationConfig.models) {
@@ -6449,13 +6511,15 @@ async function handler$o(plugin, ctx, options) {
6449
6511
  routeContext: routeContext,
6450
6512
  filters,
6451
6513
  });
6514
+ const outputs = [];
6452
6515
  for (const entity of entities) {
6453
- await entityManager.deleteById({
6516
+ const output = await entityManager.deleteById({
6454
6517
  routeContext,
6455
6518
  id: entity.id,
6456
6519
  }, plugin);
6520
+ outputs.push(output);
6457
6521
  }
6458
- return {};
6522
+ return outputs;
6459
6523
  });
6460
6524
  }
6461
6525
 
@@ -6469,11 +6533,11 @@ const code$n = "deleteCollectionEntityById";
6469
6533
  async function handler$n(plugin, ctx, options) {
6470
6534
  await runCollectionEntityActionHandler(ctx, options, code$n, true, true, async (entityManager, input) => {
6471
6535
  const { routerContext: routeContext } = ctx;
6472
- await entityManager.deleteById({
6536
+ const output = await entityManager.deleteById({
6473
6537
  id: input.id,
6474
6538
  routeContext,
6475
6539
  }, plugin);
6476
- return {};
6540
+ return output;
6477
6541
  });
6478
6542
  }
6479
6543
 
@@ -6488,8 +6552,8 @@ async function handler$m(plugin, ctx, options) {
6488
6552
  await runCollectionEntityActionHandler(ctx, options, code$m, true, true, async (entityManager, input) => {
6489
6553
  const { routerContext: routeContext } = ctx;
6490
6554
  input.routeContext = routeContext;
6491
- await entityManager.addRelations(input, plugin);
6492
- return {};
6555
+ const result = await entityManager.addRelations(input, plugin);
6556
+ return result;
6493
6557
  });
6494
6558
  }
6495
6559
 
@@ -6504,8 +6568,8 @@ async function handler$l(plugin, ctx, options) {
6504
6568
  await runCollectionEntityActionHandler(ctx, options, code$l, true, true, async (entityManager, input) => {
6505
6569
  const { routerContext: routeContext } = ctx;
6506
6570
  input.routeContext = routeContext;
6507
- await entityManager.removeRelations(input, plugin);
6508
- return {};
6571
+ const result = await entityManager.removeRelations(input, plugin);
6572
+ return result;
6509
6573
  });
6510
6574
  }
6511
6575
 
@@ -10132,9 +10196,9 @@ class EntityAccessControlPlugin {
10132
10196
  if (!userId) {
10133
10197
  return;
10134
10198
  }
10135
- const actions = await server.queryDatabaseObject(`select distinct a.* from sys_actions a
10136
- inner join oc_role_sys_action_links ra on a.id = ra.action_id
10137
- inner join oc_role_user_links ru on ru.role_id = ra.role_id
10199
+ const actions = await server.queryDatabaseObject(`select distinct a.* from sys_actions a
10200
+ inner join oc_role_sys_action_links ra on a.id = ra.action_id
10201
+ inner join oc_role_user_links ru on ru.role_id = ra.role_id
10138
10202
  where ru.user_id = $1;`, [userId]);
10139
10203
  routeContext.state.allowedActions = actions.map((item) => item.code);
10140
10204
  }
package/dist/server.d.ts CHANGED
@@ -60,6 +60,7 @@ export declare class RapidServer implements IRpdServer {
60
60
  runActionHandlers(handlerContext: ActionHandlerContext, actions: RpdRouteActionConfig[]): Promise<void>;
61
61
  beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
62
62
  beforeRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig): Promise<void>;
63
+ afterRunActionHandler(handlerContext: ActionHandlerContext, actionConfig: RpdRouteActionConfig, error?: Error): Promise<void>;
63
64
  beforeCreateEntity(model: RpdDataModel, options: CreateEntityOptions): Promise<void>;
64
65
  beforeUpdateEntity(model: RpdDataModel, options: UpdateEntityByIdOptions, currentEntity: any): Promise<void>;
65
66
  }
package/dist/types.d.ts CHANGED
@@ -147,6 +147,7 @@ export interface RpdEntityAddRelationsEventPayload {
147
147
  entity: any;
148
148
  property: string;
149
149
  relations: any[];
150
+ relationTargetEntities: any[];
150
151
  }
151
152
  export interface RpdEntityRemoveRelationsEventPayload {
152
153
  namespace: string;
@@ -155,6 +156,7 @@ export interface RpdEntityRemoveRelationsEventPayload {
155
156
  entity: any;
156
157
  property: string;
157
158
  relations: any[];
159
+ relationTargetEntities: any[];
158
160
  }
159
161
  export interface RpdEntityBeforeResponseEventPayload {
160
162
  namespace: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.11.6",
3
+ "version": "0.11.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/rollup.config.js CHANGED
@@ -1,16 +1,16 @@
1
- import typescript from "rollup-plugin-typescript2";
2
- import tscAlias from "rollup-plugin-tsc-alias";
3
-
4
- export default {
5
- input: ["src/index.ts"],
6
- output: [
7
- {
8
- dir: "dist",
9
- entryFileNames: "[name].js",
10
- format: "cjs",
11
- exports: "named",
12
- },
13
- ],
14
- plugins: [typescript(), tscAlias()],
15
- external: [],
16
- };
1
+ import typescript from "rollup-plugin-typescript2";
2
+ import tscAlias from "rollup-plugin-tsc-alias";
3
+
4
+ export default {
5
+ input: ["src/index.ts"],
6
+ output: [
7
+ {
8
+ dir: "dist",
9
+ entryFileNames: "[name].js",
10
+ format: "cjs",
11
+ exports: "named",
12
+ },
13
+ ],
14
+ plugins: [typescript(), tscAlias()],
15
+ external: [],
16
+ };