@ruiapp/rapid-core 0.11.5 → 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 +118 -41
  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 -1986
  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;
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 {
@@ -4279,6 +4297,12 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4279
4297
  };
4280
4298
  }
4281
4299
  const model = dataAccessor.getModel();
4300
+ let baseModel;
4301
+ if (model.base) {
4302
+ baseModel = server.getModel({
4303
+ singularCode: model.base,
4304
+ });
4305
+ }
4282
4306
  if (model.derivedTypePropertyCode) {
4283
4307
  // TODO: should be allowed.
4284
4308
  throw newEntityOperationError("Delete base entity directly is not allowed.");
@@ -4292,7 +4316,8 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4292
4316
  if (!entity) {
4293
4317
  return;
4294
4318
  }
4295
- if (model.softDelete) {
4319
+ const isEntitySoftDeleteEnabled = model.base ? baseModel.softDelete || model.softDelete : model.softDelete;
4320
+ if (isEntitySoftDeleteEnabled) {
4296
4321
  if (entity.deletedAt) {
4297
4322
  return;
4298
4323
  }
@@ -4307,12 +4332,18 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4307
4332
  sender: plugin,
4308
4333
  routeContext,
4309
4334
  });
4310
- if (model.softDelete) {
4335
+ if (isEntitySoftDeleteEnabled) {
4311
4336
  const currentUserId = routeContext?.state?.userId;
4312
- await dataAccessor.updateById(id, {
4337
+ const updateFields = {
4313
4338
  deleted_at: getNowStringWithTimezone(),
4314
4339
  deleter_id: currentUserId,
4315
- }, routeContext?.getDbTransactionClient());
4340
+ };
4341
+ const softDeleteDataAccessor = model.base
4342
+ ? server.getDataAccessor({
4343
+ singularCode: model.base,
4344
+ })
4345
+ : dataAccessor;
4346
+ await softDeleteDataAccessor.updateById(id, updateFields, routeContext?.getDbTransactionClient());
4316
4347
  }
4317
4348
  else {
4318
4349
  const relationPropertiesWithDeletingReaction = getEntityPropertiesIncludingBase(server, model, (property) => {
@@ -4375,7 +4406,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4375
4406
  await server.queryDatabaseObject(`DELETE FROM ${server.queryBuilder.quoteTable({
4376
4407
  schema: relationProperty.linkSchema,
4377
4408
  tableName: relationProperty.linkTableName,
4378
- })}
4409
+ })}
4379
4410
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
4380
4411
  }
4381
4412
  else {
@@ -4385,8 +4416,8 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4385
4416
  await server.queryDatabaseObject(`UPDATE ${server.queryBuilder.quoteTable({
4386
4417
  schema: relationModel.schema,
4387
4418
  tableName: relationModel.tableName,
4388
- })}
4389
- SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
4419
+ })}
4420
+ SET ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = null
4390
4421
  WHERE ${server.queryBuilder.quoteObject(relationProperty.selfIdColumnName)} = $1`, [id], routeContext?.getDbTransactionClient());
4391
4422
  }
4392
4423
  }
@@ -4410,6 +4441,7 @@ async function deleteEntityById(server, dataAccessor, options, plugin) {
4410
4441
  sender: plugin,
4411
4442
  routeContext,
4412
4443
  });
4444
+ return entity;
4413
4445
  }
4414
4446
  class EntityManager {
4415
4447
  #server;
@@ -4478,30 +4510,39 @@ class EntityManager {
4478
4510
  throw new Error(`Operation 'addRelations' is only supported on property of 'many' relation`);
4479
4511
  }
4480
4512
  const { queryBuilder } = server;
4513
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4514
+ const relationTargetEntities = [];
4481
4515
  if (relationProperty.linkTableName) {
4482
4516
  for (const relation of relations) {
4483
4517
  const command = `INSERT INTO ${queryBuilder.quoteTable({
4484
4518
  schema: relationProperty.linkSchema,
4485
4519
  tableName: relationProperty.linkTableName,
4486
- })} (${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)})
4487
- SELECT $1, $2 WHERE NOT EXISTS (
4488
- SELECT ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}, ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}
4489
- FROM ${queryBuilder.quoteTable({ schema: relationProperty.linkSchema, tableName: relationProperty.linkTableName })}
4490
- 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
4491
4525
  )`;
4492
4526
  const targetId = relation[relationProperty.targetIdColumnName] || relation.id;
4493
4527
  const params = [selfId, targetId];
4494
- 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
+ }
4495
4536
  }
4496
4537
  }
4497
4538
  else {
4498
- const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4499
4539
  for (const relation of relations) {
4500
4540
  relation[relationProperty.selfIdColumnName] = selfId;
4501
- await targetEntityManager.createEntity({
4541
+ const targetEntity = await targetEntityManager.createEntity({
4502
4542
  routeContext,
4503
4543
  entity: relation,
4504
4544
  });
4545
+ relationTargetEntities.push(targetEntity);
4505
4546
  }
4506
4547
  }
4507
4548
  await server.emitEvent({
@@ -4512,10 +4553,17 @@ class EntityManager {
4512
4553
  entity,
4513
4554
  property,
4514
4555
  relations,
4556
+ relationTargetEntities,
4515
4557
  },
4516
4558
  sender: plugin,
4517
4559
  routeContext: options.routeContext,
4518
4560
  });
4561
+ return {
4562
+ entity,
4563
+ property,
4564
+ relations,
4565
+ relationTargetEntities,
4566
+ };
4519
4567
  }
4520
4568
  async removeRelations(options, plugin) {
4521
4569
  const server = this.#server;
@@ -4536,12 +4584,21 @@ class EntityManager {
4536
4584
  throw new Error(`Operation 'removeRelations' is only supported on property of 'many' relation`);
4537
4585
  }
4538
4586
  const { queryBuilder } = server;
4587
+ const targetEntityManager = server.getEntityManager(relationProperty.targetSingularCode);
4588
+ const relationTargetEntities = [];
4539
4589
  if (relationProperty.linkTableName) {
4540
4590
  for (const relation of relations) {
4541
- 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 })}
4542
4592
  WHERE ${queryBuilder.quoteObject(relationProperty.selfIdColumnName)}=$1 AND ${queryBuilder.quoteObject(relationProperty.targetIdColumnName)}=$2;`;
4543
4593
  const params = [id, relation.id];
4544
- 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
+ }
4545
4602
  }
4546
4603
  }
4547
4604
  await server.emitEvent({
@@ -4552,10 +4609,17 @@ class EntityManager {
4552
4609
  entity,
4553
4610
  property,
4554
4611
  relations,
4612
+ relationTargetEntities,
4555
4613
  },
4556
4614
  sender: plugin,
4557
4615
  routeContext: options.routeContext,
4558
4616
  });
4617
+ return {
4618
+ entity,
4619
+ property,
4620
+ relations,
4621
+ relationTargetEntities,
4622
+ };
4559
4623
  }
4560
4624
  }
4561
4625
 
@@ -5004,7 +5068,15 @@ class RapidServer {
5004
5068
  throw new Error("Unknown handler: " + actionCode);
5005
5069
  }
5006
5070
  await this.beforeRunActionHandler(handlerContext, action);
5007
- 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
+ }
5008
5080
  }
5009
5081
  }
5010
5082
  async beforeRunRouteActions(handlerContext) {
@@ -5013,6 +5085,9 @@ class RapidServer {
5013
5085
  async beforeRunActionHandler(handlerContext, actionConfig) {
5014
5086
  await this.#pluginManager.beforeRunActionHandler(handlerContext, actionConfig);
5015
5087
  }
5088
+ async afterRunActionHandler(handlerContext, actionConfig, error) {
5089
+ await this.#pluginManager.afterRunActionHandler(handlerContext, actionConfig, error);
5090
+ }
5016
5091
  async beforeCreateEntity(model, options) {
5017
5092
  await this.#pluginManager.beforeCreateEntity(model, options);
5018
5093
  }
@@ -5890,9 +5965,9 @@ class MetaService {
5890
5965
  await server.tryQueryDatabaseObject(`COMMENT ON TABLE ${queryBuilder.quoteTable(model)} IS ${queryBuilder.formatValueToSqlLiteral(model.name)};`, []);
5891
5966
  }
5892
5967
  }
5893
- 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
5894
- FROM information_schema.columns c
5895
- 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)
5896
5971
  LEFT JOIN pg_catalog.pg_description d ON (d.objoid = st.relid and d.objsubid = c.ordinal_position);`;
5897
5972
  const columnsInDb = await server.queryDatabaseObject(sqlQueryColumnInformations, []);
5898
5973
  for (const model of applicationConfig.models) {
@@ -6436,13 +6511,15 @@ async function handler$o(plugin, ctx, options) {
6436
6511
  routeContext: routeContext,
6437
6512
  filters,
6438
6513
  });
6514
+ const outputs = [];
6439
6515
  for (const entity of entities) {
6440
- await entityManager.deleteById({
6516
+ const output = await entityManager.deleteById({
6441
6517
  routeContext,
6442
6518
  id: entity.id,
6443
6519
  }, plugin);
6520
+ outputs.push(output);
6444
6521
  }
6445
- return {};
6522
+ return outputs;
6446
6523
  });
6447
6524
  }
6448
6525
 
@@ -6456,11 +6533,11 @@ const code$n = "deleteCollectionEntityById";
6456
6533
  async function handler$n(plugin, ctx, options) {
6457
6534
  await runCollectionEntityActionHandler(ctx, options, code$n, true, true, async (entityManager, input) => {
6458
6535
  const { routerContext: routeContext } = ctx;
6459
- await entityManager.deleteById({
6536
+ const output = await entityManager.deleteById({
6460
6537
  id: input.id,
6461
6538
  routeContext,
6462
6539
  }, plugin);
6463
- return {};
6540
+ return output;
6464
6541
  });
6465
6542
  }
6466
6543
 
@@ -6475,8 +6552,8 @@ async function handler$m(plugin, ctx, options) {
6475
6552
  await runCollectionEntityActionHandler(ctx, options, code$m, true, true, async (entityManager, input) => {
6476
6553
  const { routerContext: routeContext } = ctx;
6477
6554
  input.routeContext = routeContext;
6478
- await entityManager.addRelations(input, plugin);
6479
- return {};
6555
+ const result = await entityManager.addRelations(input, plugin);
6556
+ return result;
6480
6557
  });
6481
6558
  }
6482
6559
 
@@ -6491,8 +6568,8 @@ async function handler$l(plugin, ctx, options) {
6491
6568
  await runCollectionEntityActionHandler(ctx, options, code$l, true, true, async (entityManager, input) => {
6492
6569
  const { routerContext: routeContext } = ctx;
6493
6570
  input.routeContext = routeContext;
6494
- await entityManager.removeRelations(input, plugin);
6495
- return {};
6571
+ const result = await entityManager.removeRelations(input, plugin);
6572
+ return result;
6496
6573
  });
6497
6574
  }
6498
6575
 
@@ -10119,9 +10196,9 @@ class EntityAccessControlPlugin {
10119
10196
  if (!userId) {
10120
10197
  return;
10121
10198
  }
10122
- const actions = await server.queryDatabaseObject(`select distinct a.* from sys_actions a
10123
- inner join oc_role_sys_action_links ra on a.id = ra.action_id
10124
- 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
10125
10202
  where ru.user_id = $1;`, [userId]);
10126
10203
  routeContext.state.allowedActions = actions.map((item) => item.code);
10127
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.5",
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
+ };