@ruiapp/rapid-core 0.1.29 → 0.1.31

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 (39) hide show
  1. package/dist/core/pluginManager.d.ts +3 -1
  2. package/dist/core/server.d.ts +4 -1
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.js +411 -135
  5. package/dist/plugins/sequence/SequencePluginTypes.d.ts +50 -0
  6. package/dist/plugins/stateMachine/StateMachinePlugin.d.ts +36 -0
  7. package/dist/plugins/stateMachine/StateMachinePluginTypes.d.ts +24 -0
  8. package/dist/plugins/stateMachine/StateMachineService.d.ts +3 -0
  9. package/dist/plugins/stateMachine/actionHandlers/index.d.ts +3 -0
  10. package/dist/plugins/stateMachine/actionHandlers/sendStateMachineEvent.d.ts +5 -0
  11. package/dist/plugins/stateMachine/models/StateMachine.d.ts +3 -0
  12. package/dist/plugins/stateMachine/models/index.d.ts +2 -0
  13. package/dist/plugins/stateMachine/routes/index.d.ts +12 -0
  14. package/dist/plugins/stateMachine/routes/sendStateMachineEvent.d.ts +12 -0
  15. package/dist/server.d.ts +2 -1
  16. package/dist/types.d.ts +2 -43
  17. package/package.json +3 -2
  18. package/src/core/pluginManager.ts +14 -1
  19. package/src/core/server.ts +4 -1
  20. package/src/dataAccess/entityManager.ts +6 -3
  21. package/src/index.ts +14 -1
  22. package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +7 -1
  23. package/src/plugins/sequence/SequencePlugin.ts +11 -12
  24. package/src/plugins/sequence/SequencePluginTypes.ts +70 -0
  25. package/src/plugins/sequence/SequenceService.ts +1 -1
  26. package/src/plugins/sequence/actionHandlers/generateSn.ts +0 -1
  27. package/src/plugins/stateMachine/StateMachinePlugin.ts +175 -0
  28. package/src/plugins/stateMachine/StateMachinePluginTypes.ts +30 -0
  29. package/src/plugins/stateMachine/StateMachineService.ts +19 -0
  30. package/src/plugins/stateMachine/actionHandlers/index.ts +6 -0
  31. package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +55 -0
  32. package/src/plugins/stateMachine/models/StateMachine.ts +42 -0
  33. package/src/plugins/stateMachine/models/index.ts +5 -0
  34. package/src/plugins/stateMachine/routes/index.ts +5 -0
  35. package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -0
  36. package/src/server.ts +5 -0
  37. package/src/types.ts +2 -62
  38. package/dist/plugins/sequence/sequence-types.d.ts +0 -8
  39. package/src/plugins/sequence/sequence-types.ts +0 -10
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ var bcrypt = require('bcrypt');
13
13
  var path = require('path');
14
14
  var fs = require('fs');
15
15
  var uuid = require('uuid');
16
+ var xstate = require('xstate');
16
17
 
17
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
19
 
@@ -557,6 +558,14 @@ class PluginManager {
557
558
  }
558
559
  }
559
560
  }
561
+ /** 在更新实体前调用。 */
562
+ async beforeUpdateEntity(model, options, currentEntity) {
563
+ for (const plugin of this.#plugins) {
564
+ if (plugin.beforeUpdateEntity) {
565
+ await plugin.beforeUpdateEntity(this.#server, model, options, currentEntity);
566
+ }
567
+ }
568
+ }
560
569
  }
561
570
 
562
571
  class EventManager {
@@ -2153,10 +2162,13 @@ async function updateEntityById(server, dataAccessor, options, plugin) {
2153
2162
  if (!entity) {
2154
2163
  throw new Error(`${model.namespace}.${model.singularCode} with id "${id}" was not found.`);
2155
2164
  }
2156
- const changes = getEntityPartChanges(entity, entityToSave);
2157
- if (!changes) {
2165
+ let changes = getEntityPartChanges(entity, entityToSave);
2166
+ if (!changes && !options.operation) {
2158
2167
  return entity;
2159
2168
  }
2169
+ options.entityToSave = changes || {};
2170
+ await server.beforeUpdateEntity(model, options, entity);
2171
+ changes = options.entityToSave;
2160
2172
  const oneRelationPropertiesToUpdate = [];
2161
2173
  const manyRelationPropertiesToUpdate = [];
2162
2174
  lodash.keys(changes).forEach((propertyCode) => {
@@ -2635,6 +2647,9 @@ class RapidServer {
2635
2647
  async beforeCreateEntity(model, options) {
2636
2648
  await this.#pluginManager.beforeCreateEntity(model, options);
2637
2649
  }
2650
+ async beforeUpdateEntity(model, options, currentEntity) {
2651
+ await this.#pluginManager.beforeUpdateEntity(model, options, currentEntity);
2652
+ }
2638
2653
  }
2639
2654
 
2640
2655
  // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
@@ -2784,32 +2799,32 @@ async function generateJwtSecretKey() {
2784
2799
  return encode(exportedKey);
2785
2800
  }
2786
2801
 
2787
- const code$n = "listMetaModels";
2788
- async function handler$n(plugin, ctx, options) {
2802
+ const code$o = "listMetaModels";
2803
+ async function handler$o(plugin, ctx, options) {
2789
2804
  const { applicationConfig } = ctx;
2790
2805
  ctx.output = { list: applicationConfig.models };
2791
2806
  }
2792
2807
 
2793
2808
  var listMetaModels = /*#__PURE__*/Object.freeze({
2794
2809
  __proto__: null,
2795
- code: code$n,
2796
- handler: handler$n
2810
+ code: code$o,
2811
+ handler: handler$o
2797
2812
  });
2798
2813
 
2799
- const code$m = "listMetaRoutes";
2800
- async function handler$m(plugin, ctx, options) {
2814
+ const code$n = "listMetaRoutes";
2815
+ async function handler$n(plugin, ctx, options) {
2801
2816
  const { applicationConfig } = ctx;
2802
2817
  ctx.output = { list: applicationConfig.routes };
2803
2818
  }
2804
2819
 
2805
2820
  var listMetaRoutes = /*#__PURE__*/Object.freeze({
2806
2821
  __proto__: null,
2807
- code: code$m,
2808
- handler: handler$m
2822
+ code: code$n,
2823
+ handler: handler$n
2809
2824
  });
2810
2825
 
2811
- const code$l = "getMetaModelDetail";
2812
- async function handler$l(plugin, ctx, options) {
2826
+ const code$m = "getMetaModelDetail";
2827
+ async function handler$m(plugin, ctx, options) {
2813
2828
  const { server, input } = ctx;
2814
2829
  const model = server.getModel(input);
2815
2830
  ctx.output = model;
@@ -2817,8 +2832,8 @@ async function handler$l(plugin, ctx, options) {
2817
2832
 
2818
2833
  var getMetaModelDetail = /*#__PURE__*/Object.freeze({
2819
2834
  __proto__: null,
2820
- code: code$l,
2821
- handler: handler$l
2835
+ code: code$m,
2836
+ handler: handler$m
2822
2837
  });
2823
2838
 
2824
2839
  /**
@@ -3170,9 +3185,9 @@ function transformFilterWithSubFilters(filter) {
3170
3185
  return filter;
3171
3186
  }
3172
3187
 
3173
- const code$k = "findCollectionEntities";
3174
- async function handler$k(plugin, ctx, options) {
3175
- await runCollectionEntityActionHandler(ctx, options, code$k, async (entityManager, input) => {
3188
+ const code$l = "findCollectionEntities";
3189
+ async function handler$l(plugin, ctx, options) {
3190
+ await runCollectionEntityActionHandler(ctx, options, code$l, async (entityManager, input) => {
3176
3191
  input.filters = removeFiltersWithNullValue(input.filters);
3177
3192
  const entities = await entityManager.findEntities(input);
3178
3193
  const result = { list: entities };
@@ -3187,14 +3202,14 @@ async function handler$k(plugin, ctx, options) {
3187
3202
 
3188
3203
  var findCollectionEntities = /*#__PURE__*/Object.freeze({
3189
3204
  __proto__: null,
3190
- code: code$k,
3191
- handler: handler$k
3205
+ code: code$l,
3206
+ handler: handler$l
3192
3207
  });
3193
3208
 
3194
- const code$j = "findCollectionEntityById";
3195
- async function handler$j(plugin, ctx, options) {
3209
+ const code$k = "findCollectionEntityById";
3210
+ async function handler$k(plugin, ctx, options) {
3196
3211
  const { logger, server, input } = ctx;
3197
- logger.debug(`Running ${code$j} handler...`, { input });
3212
+ logger.debug(`Running ${code$k} handler...`, { input });
3198
3213
  const { id } = input;
3199
3214
  const entityManager = server.getEntityManager(options.singularCode);
3200
3215
  const entity = await entityManager.findById(id);
@@ -3206,13 +3221,13 @@ async function handler$j(plugin, ctx, options) {
3206
3221
 
3207
3222
  var findCollectionEntityById = /*#__PURE__*/Object.freeze({
3208
3223
  __proto__: null,
3209
- code: code$j,
3210
- handler: handler$j
3224
+ code: code$k,
3225
+ handler: handler$k
3211
3226
  });
3212
3227
 
3213
- const code$i = "countCollectionEntities";
3214
- async function handler$i(plugin, ctx, options) {
3215
- await runCollectionEntityActionHandler(ctx, options, code$i, (entityManager, input) => {
3228
+ const code$j = "countCollectionEntities";
3229
+ async function handler$j(plugin, ctx, options) {
3230
+ await runCollectionEntityActionHandler(ctx, options, code$j, (entityManager, input) => {
3216
3231
  input.filters = removeFiltersWithNullValue(input.filters);
3217
3232
  return entityManager.count(input);
3218
3233
  });
@@ -3220,16 +3235,16 @@ async function handler$i(plugin, ctx, options) {
3220
3235
 
3221
3236
  var countCollectionEntities = /*#__PURE__*/Object.freeze({
3222
3237
  __proto__: null,
3223
- code: code$i,
3224
- handler: handler$i
3238
+ code: code$j,
3239
+ handler: handler$j
3225
3240
  });
3226
3241
 
3227
- const code$h = "createCollectionEntity";
3228
- async function handler$h(plugin, ctx, options) {
3242
+ const code$i = "createCollectionEntity";
3243
+ async function handler$i(plugin, ctx, options) {
3229
3244
  const { logger, server, input } = ctx;
3230
3245
  const { defaultInput, fixedInput } = options;
3231
3246
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3232
- logger.debug(`Running ${code$h} handler...`, { defaultInput, fixedInput, mergedInput });
3247
+ logger.debug(`Running ${code$i} handler...`, { defaultInput, fixedInput, mergedInput });
3233
3248
  const userId = ctx.routerContext.state?.userId;
3234
3249
  if (userId) {
3235
3250
  input.createdBy = userId;
@@ -3243,15 +3258,15 @@ async function handler$h(plugin, ctx, options) {
3243
3258
 
3244
3259
  var createCollectionEntity = /*#__PURE__*/Object.freeze({
3245
3260
  __proto__: null,
3246
- code: code$h,
3247
- handler: handler$h
3261
+ code: code$i,
3262
+ handler: handler$i
3248
3263
  });
3249
3264
 
3250
- const code$g = "createCollectionEntitiesBatch";
3251
- async function handler$g(plugin, ctx, options) {
3265
+ const code$h = "createCollectionEntitiesBatch";
3266
+ async function handler$h(plugin, ctx, options) {
3252
3267
  const { logger, server, input } = ctx;
3253
3268
  const { defaultInput, fixedInput } = options;
3254
- logger.debug(`Running ${code$g} handler...`, { defaultInput, fixedInput, input });
3269
+ logger.debug(`Running ${code$h} handler...`, { defaultInput, fixedInput, input });
3255
3270
  const { entities } = input;
3256
3271
  if (!lodash.isArray(entities)) {
3257
3272
  throw new Error("input.entities should be an array.");
@@ -3274,31 +3289,35 @@ async function handler$g(plugin, ctx, options) {
3274
3289
 
3275
3290
  var createCollectionEntitiesBatch = /*#__PURE__*/Object.freeze({
3276
3291
  __proto__: null,
3277
- code: code$g,
3278
- handler: handler$g
3292
+ code: code$h,
3293
+ handler: handler$h
3279
3294
  });
3280
3295
 
3281
- const code$f = "updateCollectionEntityById";
3282
- async function handler$f(plugin, ctx, options) {
3296
+ const code$g = "updateCollectionEntityById";
3297
+ async function handler$g(plugin, ctx, options) {
3283
3298
  const { logger, server, input } = ctx;
3284
3299
  const { defaultInput, fixedInput } = options;
3285
3300
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3286
- logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, mergedInput });
3301
+ logger.debug(`Running ${code$g} handler...`, { defaultInput, fixedInput, mergedInput });
3302
+ const operation = mergedInput.$operation;
3303
+ if (operation) {
3304
+ delete mergedInput.$operation;
3305
+ }
3287
3306
  const entityManager = server.getEntityManager(options.singularCode);
3288
- const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput }, plugin);
3307
+ const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput, operation }, plugin);
3289
3308
  ctx.output = output;
3290
3309
  }
3291
3310
 
3292
3311
  var updateCollectionEntityById = /*#__PURE__*/Object.freeze({
3293
3312
  __proto__: null,
3294
- code: code$f,
3295
- handler: handler$f
3313
+ code: code$g,
3314
+ handler: handler$g
3296
3315
  });
3297
3316
 
3298
- const code$e = "deleteCollectionEntityById";
3299
- async function handler$e(plugin, ctx, options) {
3317
+ const code$f = "deleteCollectionEntityById";
3318
+ async function handler$f(plugin, ctx, options) {
3300
3319
  const { logger, server, input } = ctx;
3301
- logger.debug(`Running ${code$e} handler...`);
3320
+ logger.debug(`Running ${code$f} handler...`);
3302
3321
  const entityManager = server.getEntityManager(options.singularCode);
3303
3322
  await entityManager.deleteById(input.id, plugin);
3304
3323
  ctx.status = 200;
@@ -3307,16 +3326,16 @@ async function handler$e(plugin, ctx, options) {
3307
3326
 
3308
3327
  var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
3309
3328
  __proto__: null,
3310
- code: code$e,
3311
- handler: handler$e
3329
+ code: code$f,
3330
+ handler: handler$f
3312
3331
  });
3313
3332
 
3314
- const code$d = "addEntityRelations";
3315
- async function handler$d(plugin, ctx, options) {
3333
+ const code$e = "addEntityRelations";
3334
+ async function handler$e(plugin, ctx, options) {
3316
3335
  const { logger, server, input } = ctx;
3317
3336
  const { defaultInput, fixedInput } = options;
3318
3337
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3319
- logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, mergedInput });
3338
+ logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, mergedInput });
3320
3339
  const entityManager = server.getEntityManager(options.singularCode);
3321
3340
  await entityManager.addRelations(mergedInput, plugin);
3322
3341
  ctx.output = {};
@@ -3324,16 +3343,16 @@ async function handler$d(plugin, ctx, options) {
3324
3343
 
3325
3344
  var addEntityRelations = /*#__PURE__*/Object.freeze({
3326
3345
  __proto__: null,
3327
- code: code$d,
3328
- handler: handler$d
3346
+ code: code$e,
3347
+ handler: handler$e
3329
3348
  });
3330
3349
 
3331
- const code$c = "removeEntityRelations";
3332
- async function handler$c(plugin, ctx, options) {
3350
+ const code$d = "removeEntityRelations";
3351
+ async function handler$d(plugin, ctx, options) {
3333
3352
  const { logger, server, input } = ctx;
3334
3353
  const { defaultInput, fixedInput } = options;
3335
3354
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3336
- logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
3355
+ logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, mergedInput });
3337
3356
  const entityManager = server.getEntityManager(options.singularCode);
3338
3357
  await entityManager.removeRelations(mergedInput, plugin);
3339
3358
  ctx.output = {};
@@ -3341,16 +3360,16 @@ async function handler$c(plugin, ctx, options) {
3341
3360
 
3342
3361
  var removeEntityRelations = /*#__PURE__*/Object.freeze({
3343
3362
  __proto__: null,
3344
- code: code$c,
3345
- handler: handler$c
3363
+ code: code$d,
3364
+ handler: handler$d
3346
3365
  });
3347
3366
 
3348
- const code$b = "queryDatabase";
3349
- async function handler$b(plugin, ctx, options) {
3367
+ const code$c = "queryDatabase";
3368
+ async function handler$c(plugin, ctx, options) {
3350
3369
  const { logger, server, input } = ctx;
3351
3370
  const { sql, querySingle, defaultInput, fixedInput } = options;
3352
3371
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3353
- logger.debug(`Running ${code$b} handler...`, { defaultInput, fixedInput, mergedInput });
3372
+ logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
3354
3373
  const result = await server.queryDatabaseObject(sql, mergedInput);
3355
3374
  if (querySingle) {
3356
3375
  ctx.output = lodash.first(result);
@@ -3362,8 +3381,8 @@ async function handler$b(plugin, ctx, options) {
3362
3381
 
3363
3382
  var queryDatabase = /*#__PURE__*/Object.freeze({
3364
3383
  __proto__: null,
3365
- code: code$b,
3366
- handler: handler$b
3384
+ code: code$c,
3385
+ handler: handler$c
3367
3386
  });
3368
3387
 
3369
3388
  /**
@@ -3534,17 +3553,17 @@ async function sendSourceResponse(proxyCtx, targetRes) {
3534
3553
  srcRes.body = targetRes.body;
3535
3554
  }
3536
3555
 
3537
- const code$a = "httpProxy";
3538
- async function handler$a(plugin, ctx, options) {
3556
+ const code$b = "httpProxy";
3557
+ async function handler$b(plugin, ctx, options) {
3539
3558
  const { logger } = ctx;
3540
- logger.debug(`Running ${code$a} handler...`);
3559
+ logger.debug(`Running ${code$b} handler...`);
3541
3560
  await doProxy(ctx.routerContext, options);
3542
3561
  }
3543
3562
 
3544
3563
  var httpProxy = /*#__PURE__*/Object.freeze({
3545
3564
  __proto__: null,
3546
- code: code$a,
3547
- handler: handler$a
3565
+ code: code$b,
3566
+ handler: handler$b
3548
3567
  });
3549
3568
 
3550
3569
  /**
@@ -3783,8 +3802,8 @@ async function generateSn$2(server, input) {
3783
3802
  return sequenceNumbers;
3784
3803
  }
3785
3804
 
3786
- const code$9 = "generateSn";
3787
- async function handler$9(plugin, ctx, options) {
3805
+ const code$a = "generateSn";
3806
+ async function handler$a(plugin, ctx, options) {
3788
3807
  const { server, routerContext } = ctx;
3789
3808
  const input = ctx.input;
3790
3809
  if (options?.ruleCode) {
@@ -3801,11 +3820,11 @@ async function handler$9(plugin, ctx, options) {
3801
3820
 
3802
3821
  var generateSn$1 = /*#__PURE__*/Object.freeze({
3803
3822
  __proto__: null,
3804
- code: code$9,
3805
- handler: handler$9
3823
+ code: code$a,
3824
+ handler: handler$a
3806
3825
  });
3807
3826
 
3808
- var pluginActionHandlers$2 = [
3827
+ var pluginActionHandlers$3 = [
3809
3828
  generateSn$1,
3810
3829
  ];
3811
3830
 
@@ -3898,7 +3917,7 @@ var SequenceAutoIncrementRecord = {
3898
3917
  ],
3899
3918
  };
3900
3919
 
3901
- var pluginModels$1 = [
3920
+ var pluginModels$2 = [
3902
3921
  SequenceRule,
3903
3922
  SequenceAutoIncrementRecord,
3904
3923
  ];
@@ -3917,7 +3936,7 @@ var generateSn = {
3917
3936
  ],
3918
3937
  };
3919
3938
 
3920
- var pluginRoutes$2 = [
3939
+ var pluginRoutes$3 = [
3921
3940
  generateSn,
3922
3941
  ];
3923
3942
 
@@ -3941,24 +3960,24 @@ class SequencePlugin {
3941
3960
  return [];
3942
3961
  }
3943
3962
  async registerActionHandlers(server) {
3944
- for (const actionHandler of pluginActionHandlers$2) {
3963
+ for (const actionHandler of pluginActionHandlers$3) {
3945
3964
  server.registerActionHandler(this, actionHandler);
3946
3965
  }
3947
3966
  }
3948
3967
  async configureModels(server, applicationConfig) {
3949
- server.appendApplicationConfig({ models: pluginModels$1 });
3968
+ server.appendApplicationConfig({ models: pluginModels$2 });
3950
3969
  }
3951
3970
  async configureRoutes(server, applicationConfig) {
3952
- server.appendApplicationConfig({ routes: pluginRoutes$2 });
3971
+ server.appendApplicationConfig({ routes: pluginRoutes$3 });
3953
3972
  }
3954
3973
  async onApplicationLoaded(server, applicationConfig) {
3955
3974
  const models = server.getApplicationConfig().models;
3956
3975
  for (const model of models) {
3957
3976
  for (const property of model.properties) {
3958
- const sequenceConfig = property.config?.sequence;
3959
- if (sequenceConfig) {
3977
+ const propertySequenceConfig = property.config?.sequence;
3978
+ if (propertySequenceConfig) {
3960
3979
  const ruleCode = getSequenceRuleCode(model, property);
3961
- const ruleConfig = sequenceConfig.ruleConfig;
3980
+ const ruleConfig = propertySequenceConfig.config;
3962
3981
  const sequenceRuleDataAccessor = server.getDataAccessor({
3963
3982
  singularCode: "sequence_rule",
3964
3983
  });
@@ -3972,7 +3991,7 @@ class SequencePlugin {
3972
3991
  ],
3973
3992
  });
3974
3993
  if (sequenceRule) {
3975
- if (JSON.stringify(sequenceRule.config) !== JSON.stringify(ruleConfig)) {
3994
+ if (lodash.isEqual(sequenceRule.config, ruleConfig)) {
3976
3995
  await sequenceRuleDataAccessor.updateById(sequenceRule.id, {
3977
3996
  config: ruleConfig,
3978
3997
  });
@@ -3989,21 +4008,20 @@ class SequencePlugin {
3989
4008
  }
3990
4009
  }
3991
4010
  async beforeCreateEntity(server, model, options) {
3992
- debugger;
3993
4011
  const entity = options.entity;
3994
4012
  for (const property of model.properties) {
3995
4013
  const sequenceConfig = property.config?.sequence;
3996
4014
  const propertyValue = entity[property.code];
3997
4015
  if (sequenceConfig &&
3998
- sequenceConfig.autoGenerate &&
3999
- (lodash.isUndefined(propertyValue) || lodash.isNull(propertyValue))) {
4016
+ sequenceConfig.enabled &&
4017
+ isNullOrUndefined(propertyValue)) {
4000
4018
  const ruleCode = getSequenceRuleCode(model, property);
4001
- const sns = await generateSn$2(server, {
4019
+ const numbers = await generateSn$2(server, {
4002
4020
  ruleCode,
4003
4021
  amount: 1,
4004
4022
  parameters: entity,
4005
4023
  });
4006
- entity[property.code] = sns[0];
4024
+ entity[property.code] = numbers[0];
4007
4025
  }
4008
4026
  }
4009
4027
  }
@@ -4192,8 +4210,8 @@ class WebhooksPlugin {
4192
4210
  }
4193
4211
  }
4194
4212
 
4195
- const code$8 = "changePassword";
4196
- async function handler$8(plugin, ctx, options) {
4213
+ const code$9 = "changePassword";
4214
+ async function handler$9(plugin, ctx, options) {
4197
4215
  const { server, input, routerContext } = ctx;
4198
4216
  const { id, oldPassword, newPassword } = input;
4199
4217
  const userId = routerContext.state.userId;
@@ -4235,12 +4253,12 @@ async function handler$8(plugin, ctx, options) {
4235
4253
 
4236
4254
  var changePassword$1 = /*#__PURE__*/Object.freeze({
4237
4255
  __proto__: null,
4238
- code: code$8,
4239
- handler: handler$8
4256
+ code: code$9,
4257
+ handler: handler$9
4240
4258
  });
4241
4259
 
4242
- const code$7 = "createSession";
4243
- async function handler$7(plugin, ctx, options) {
4260
+ const code$8 = "createSession";
4261
+ async function handler$8(plugin, ctx, options) {
4244
4262
  const { server, input, routerContext } = ctx;
4245
4263
  const { response } = routerContext;
4246
4264
  const { account, password } = input;
@@ -4283,12 +4301,12 @@ async function handler$7(plugin, ctx, options) {
4283
4301
 
4284
4302
  var createSession = /*#__PURE__*/Object.freeze({
4285
4303
  __proto__: null,
4286
- code: code$7,
4287
- handler: handler$7
4304
+ code: code$8,
4305
+ handler: handler$8
4288
4306
  });
4289
4307
 
4290
- const code$6 = "deleteSession";
4291
- async function handler$6(plugin, ctx, options) {
4308
+ const code$7 = "deleteSession";
4309
+ async function handler$7(plugin, ctx, options) {
4292
4310
  const { server, input, routerContext } = ctx;
4293
4311
  const { response } = routerContext;
4294
4312
  setCookie(response.headers, {
@@ -4301,12 +4319,12 @@ async function handler$6(plugin, ctx, options) {
4301
4319
 
4302
4320
  var deleteSession = /*#__PURE__*/Object.freeze({
4303
4321
  __proto__: null,
4304
- code: code$6,
4305
- handler: handler$6
4322
+ code: code$7,
4323
+ handler: handler$7
4306
4324
  });
4307
4325
 
4308
- const code$5 = "getMyProfile";
4309
- async function handler$5(plugin, ctx, options) {
4326
+ const code$6 = "getMyProfile";
4327
+ async function handler$6(plugin, ctx, options) {
4310
4328
  const { server, input, routerContext } = ctx;
4311
4329
  const userId = routerContext.state.userId;
4312
4330
  if (!userId) {
@@ -4336,12 +4354,12 @@ async function handler$5(plugin, ctx, options) {
4336
4354
 
4337
4355
  var getMyProfile$2 = /*#__PURE__*/Object.freeze({
4338
4356
  __proto__: null,
4339
- code: code$5,
4340
- handler: handler$5
4357
+ code: code$6,
4358
+ handler: handler$6
4341
4359
  });
4342
4360
 
4343
- const code$4 = "resetPassword";
4344
- async function handler$4(plugin, ctx, options) {
4361
+ const code$5 = "resetPassword";
4362
+ async function handler$5(plugin, ctx, options) {
4345
4363
  const { server, input, routerContext } = ctx;
4346
4364
  const { userId, password } = input;
4347
4365
  const userDataAccessor = server.getDataAccessor({
@@ -4369,11 +4387,11 @@ async function handler$4(plugin, ctx, options) {
4369
4387
 
4370
4388
  var resetPassword$1 = /*#__PURE__*/Object.freeze({
4371
4389
  __proto__: null,
4372
- code: code$4,
4373
- handler: handler$4
4390
+ code: code$5,
4391
+ handler: handler$5
4374
4392
  });
4375
4393
 
4376
- var pluginActionHandlers$1 = [
4394
+ var pluginActionHandlers$2 = [
4377
4395
  changePassword$1,
4378
4396
  createSession,
4379
4397
  deleteSession,
@@ -4436,7 +4454,7 @@ var AccessToken = {
4436
4454
  ],
4437
4455
  };
4438
4456
 
4439
- var pluginModels = [
4457
+ var pluginModels$1 = [
4440
4458
  AccessToken,
4441
4459
  ];
4442
4460
 
@@ -4510,7 +4528,7 @@ var signout$1 = {
4510
4528
  ],
4511
4529
  };
4512
4530
 
4513
- var pluginRoutes$1 = [
4531
+ var pluginRoutes$2 = [
4514
4532
  changePassword,
4515
4533
  getMyProfile$1,
4516
4534
  resetPassword,
@@ -4538,15 +4556,15 @@ class AuthPlugin {
4538
4556
  return [];
4539
4557
  }
4540
4558
  async registerActionHandlers(server) {
4541
- for (const actionHandler of pluginActionHandlers$1) {
4559
+ for (const actionHandler of pluginActionHandlers$2) {
4542
4560
  server.registerActionHandler(this, actionHandler);
4543
4561
  }
4544
4562
  }
4545
4563
  async configureModels(server, applicationConfig) {
4546
- server.appendApplicationConfig({ models: pluginModels });
4564
+ server.appendApplicationConfig({ models: pluginModels$1 });
4547
4565
  }
4548
4566
  async configureRoutes(server, applicationConfig) {
4549
- server.appendApplicationConfig({ routes: pluginRoutes$1 });
4567
+ server.appendApplicationConfig({ routes: pluginRoutes$2 });
4550
4568
  }
4551
4569
  async onPrepareRouteContext(server, routeContext) {
4552
4570
  const request = routeContext.request;
@@ -4602,8 +4620,8 @@ async function appendFile(path, data) {
4602
4620
  });
4603
4621
  }
4604
4622
 
4605
- const code$3 = "downloadDocument";
4606
- async function handler$3(plugin, ctx, options) {
4623
+ const code$4 = "downloadDocument";
4624
+ async function handler$4(plugin, ctx, options) {
4607
4625
  const { server, applicationConfig, routerContext, input } = ctx;
4608
4626
  const { request, response } = routerContext;
4609
4627
  const documentDataAccessor = ctx.server.getDataAccessor({
@@ -4631,12 +4649,12 @@ async function handler$3(plugin, ctx, options) {
4631
4649
 
4632
4650
  var downloadDocumentActionHandler = /*#__PURE__*/Object.freeze({
4633
4651
  __proto__: null,
4634
- code: code$3,
4635
- handler: handler$3
4652
+ code: code$4,
4653
+ handler: handler$4
4636
4654
  });
4637
4655
 
4638
- const code$2 = "downloadFile";
4639
- async function handler$2(plugin, ctx, options) {
4656
+ const code$3 = "downloadFile";
4657
+ async function handler$3(plugin, ctx, options) {
4640
4658
  const { server, applicationConfig, routerContext, input } = ctx;
4641
4659
  const { request, response } = routerContext;
4642
4660
  const dataAccessor = ctx.server.getDataAccessor({
@@ -4656,12 +4674,12 @@ async function handler$2(plugin, ctx, options) {
4656
4674
 
4657
4675
  var downloadFileActionHandler = /*#__PURE__*/Object.freeze({
4658
4676
  __proto__: null,
4659
- code: code$2,
4660
- handler: handler$2
4677
+ code: code$3,
4678
+ handler: handler$3
4661
4679
  });
4662
4680
 
4663
- const code$1 = "uploadFile";
4664
- async function handler$1(plugin, ctx, options) {
4681
+ const code$2 = "uploadFile";
4682
+ async function handler$2(plugin, ctx, options) {
4665
4683
  const { server, applicationConfig, routerContext, input } = ctx;
4666
4684
  let file = input.file || input.files;
4667
4685
  if (lodash.isArray(file)) {
@@ -4682,8 +4700,8 @@ async function handler$1(plugin, ctx, options) {
4682
4700
 
4683
4701
  var uploadFileActionHandler = /*#__PURE__*/Object.freeze({
4684
4702
  __proto__: null,
4685
- code: code$1,
4686
- handler: handler$1
4703
+ code: code$2,
4704
+ handler: handler$2
4687
4705
  });
4688
4706
 
4689
4707
  var getMyProfile = {
@@ -4728,7 +4746,7 @@ var signout = {
4728
4746
  ],
4729
4747
  };
4730
4748
 
4731
- var pluginRoutes = [
4749
+ var pluginRoutes$1 = [
4732
4750
  getMyProfile,
4733
4751
  signin,
4734
4752
  signout,
@@ -4759,23 +4777,23 @@ class FileManager {
4759
4777
  server.registerActionHandler(this, uploadFileActionHandler);
4760
4778
  }
4761
4779
  async configureRoutes(server, applicationConfig) {
4762
- server.appendApplicationConfig({ routes: pluginRoutes });
4780
+ server.appendApplicationConfig({ routes: pluginRoutes$1 });
4763
4781
  }
4764
4782
  }
4765
4783
 
4766
- const code = "runServerOperation";
4767
- async function handler(plugin, ctx, options) {
4784
+ const code$1 = "runServerOperation";
4785
+ async function handler$1(plugin, ctx, options) {
4768
4786
  const { operation } = options;
4769
4787
  await operation(ctx);
4770
4788
  }
4771
4789
 
4772
4790
  var runServerOperation = /*#__PURE__*/Object.freeze({
4773
4791
  __proto__: null,
4774
- code: code,
4775
- handler: handler
4792
+ code: code$1,
4793
+ handler: handler$1
4776
4794
  });
4777
4795
 
4778
- var pluginActionHandlers = [
4796
+ var pluginActionHandlers$1 = [
4779
4797
  runServerOperation,
4780
4798
  ];
4781
4799
 
@@ -4804,7 +4822,7 @@ class ServerOperationPlugin {
4804
4822
  async registerMiddlewares(server) {
4805
4823
  }
4806
4824
  async registerActionHandlers(server) {
4807
- for (const actionHandler of pluginActionHandlers) {
4825
+ for (const actionHandler of pluginActionHandlers$1) {
4808
4826
  server.registerActionHandler(this, actionHandler);
4809
4827
  }
4810
4828
  }
@@ -4848,6 +4866,263 @@ class ServerOperationPlugin {
4848
4866
  }
4849
4867
  }
4850
4868
 
4869
+ async function getStateMachineNextSnapshot(server, options) {
4870
+ debugger;
4871
+ const { machineConfig, currentState, event } = options;
4872
+ machineConfig.initial = currentState;
4873
+ const machine = xstate.createMachine(machineConfig);
4874
+ const snapshot = xstate.getInitialSnapshot(machine);
4875
+ if (!snapshot.can(event)) {
4876
+ throw new Error(`'${event.type}' action is not allowed at '${currentState}' state.`);
4877
+ }
4878
+ const nextSnapshot = xstate.getNextSnapshot(machine, snapshot, event);
4879
+ return nextSnapshot;
4880
+ }
4881
+
4882
+ const code = "sendStateMachineEvent";
4883
+ async function handler(plugin, ctx, options) {
4884
+ const { server, routerContext } = ctx;
4885
+ const { response } = routerContext;
4886
+ const input = ctx.input;
4887
+ if (options?.code) {
4888
+ input.code = options.code;
4889
+ }
4890
+ if (!input.code) {
4891
+ throw new Error(`State machine code is required when sending event.`);
4892
+ }
4893
+ const stateMachineDataAccessor = server.getDataAccessor({
4894
+ singularCode: "state_machine",
4895
+ });
4896
+ const stateMachine = await stateMachineDataAccessor.findOne({
4897
+ filters: [
4898
+ {
4899
+ operator: "eq",
4900
+ field: "code",
4901
+ value: input.code,
4902
+ }
4903
+ ]
4904
+ });
4905
+ if (!stateMachine) {
4906
+ throw new Error(`State machine with code '${input.code}' was not found.`);
4907
+ }
4908
+ stateMachine.config.id = input.code;
4909
+ const snapshot = await getStateMachineNextSnapshot(server, {
4910
+ machineConfig: stateMachine.config,
4911
+ context: input.context,
4912
+ currentState: input.currentState,
4913
+ event: input.event,
4914
+ });
4915
+ response.json({
4916
+ state: snapshot.value,
4917
+ });
4918
+ }
4919
+
4920
+ var sendStateMachineEvent$1 = /*#__PURE__*/Object.freeze({
4921
+ __proto__: null,
4922
+ code: code,
4923
+ handler: handler
4924
+ });
4925
+
4926
+ var pluginActionHandlers = [
4927
+ sendStateMachineEvent$1,
4928
+ ];
4929
+
4930
+ var StateMachine = {
4931
+ maintainedBy: "stateMachinePlugin",
4932
+ namespace: "svc",
4933
+ name: "state_machine",
4934
+ singularCode: "state_machine",
4935
+ pluralCode: "state_machines",
4936
+ schema: "public",
4937
+ tableName: "state_machines",
4938
+ properties: [
4939
+ {
4940
+ name: "id",
4941
+ code: "id",
4942
+ columnName: "id",
4943
+ type: "integer",
4944
+ required: true,
4945
+ autoIncrement: true,
4946
+ },
4947
+ {
4948
+ name: "code",
4949
+ code: "code",
4950
+ columnName: "code",
4951
+ type: "text",
4952
+ required: true,
4953
+ },
4954
+ {
4955
+ name: "description",
4956
+ code: "description",
4957
+ columnName: "description",
4958
+ type: "text",
4959
+ required: false,
4960
+ },
4961
+ {
4962
+ name: "config",
4963
+ code: "config",
4964
+ columnName: "config",
4965
+ type: "json",
4966
+ required: false,
4967
+ },
4968
+ ],
4969
+ };
4970
+
4971
+ var pluginModels = [
4972
+ StateMachine,
4973
+ ];
4974
+
4975
+ var sendStateMachineEvent = {
4976
+ namespace: "svc",
4977
+ name: "svc.sendStateMachineEvent",
4978
+ code: "svc.sendStateMachineEvent",
4979
+ type: "RESTful",
4980
+ method: "POST",
4981
+ endpoint: "/svc/sendStateMachineEvent",
4982
+ actions: [
4983
+ {
4984
+ code: "sendStateMachineEvent",
4985
+ },
4986
+ ],
4987
+ };
4988
+
4989
+ var pluginRoutes = [
4990
+ sendStateMachineEvent,
4991
+ ];
4992
+
4993
+ /**
4994
+ * State machine plugin
4995
+ */
4996
+ class StateMachinePlugin {
4997
+ get code() {
4998
+ return "stateMachinePlugin";
4999
+ }
5000
+ get description() {
5001
+ return null;
5002
+ }
5003
+ get extendingAbilities() {
5004
+ return [];
5005
+ }
5006
+ get configurableTargets() {
5007
+ return [];
5008
+ }
5009
+ get configurations() {
5010
+ return [];
5011
+ }
5012
+ async registerActionHandlers(server) {
5013
+ for (const actionHandler of pluginActionHandlers) {
5014
+ server.registerActionHandler(this, actionHandler);
5015
+ }
5016
+ }
5017
+ async configureModels(server, applicationConfig) {
5018
+ server.appendApplicationConfig({ models: pluginModels });
5019
+ }
5020
+ async configureRoutes(server, applicationConfig) {
5021
+ server.appendApplicationConfig({ routes: pluginRoutes });
5022
+ }
5023
+ async onApplicationLoaded(server, applicationConfig) {
5024
+ const models = server.getApplicationConfig().models;
5025
+ for (const model of models) {
5026
+ for (const property of model.properties) {
5027
+ const propertyStateMachineConfig = property.config?.stateMachine;
5028
+ if (propertyStateMachineConfig) {
5029
+ const stateMachineCode = getStateMachineCode(model, property);
5030
+ const stateMachineConfig = propertyStateMachineConfig.config;
5031
+ const stateMachineDataAccessor = server.getDataAccessor({
5032
+ singularCode: "state_machine",
5033
+ });
5034
+ const stateMachine = await stateMachineDataAccessor.findOne({
5035
+ filters: [
5036
+ {
5037
+ operator: "eq",
5038
+ field: "code",
5039
+ value: stateMachineCode,
5040
+ },
5041
+ ],
5042
+ });
5043
+ if (stateMachine) {
5044
+ if (!lodash.isEqual(stateMachine.config, stateMachineConfig)) {
5045
+ await stateMachineDataAccessor.updateById(stateMachine.id, {
5046
+ config: stateMachineConfig,
5047
+ });
5048
+ }
5049
+ }
5050
+ else {
5051
+ await stateMachineDataAccessor.create({
5052
+ code: stateMachineCode,
5053
+ config: stateMachineConfig,
5054
+ });
5055
+ }
5056
+ }
5057
+ }
5058
+ }
5059
+ }
5060
+ /**
5061
+ * 创建实体前的处理。
5062
+ * 当属性启用了状态机管理,如创建实体时没有指定该属性的状态值,则应将该属性设置为 stateMachine.config.initial 。
5063
+ * @param server
5064
+ * @param model
5065
+ * @param options
5066
+ */
5067
+ async beforeCreateEntity(server, model, options) {
5068
+ for (const property of model.properties) {
5069
+ const isStateMachineEnabled = lodash.get(property.config, "stateMachine.enabled", false);
5070
+ if (isStateMachineEnabled && isNullOrUndefined(options.entity[property.code])) {
5071
+ const initialState = lodash.get(property.config, "stateMachine.config.initial", null);
5072
+ if (initialState) {
5073
+ options.entity[property.code] = initialState;
5074
+ }
5075
+ }
5076
+ }
5077
+ }
5078
+ /**
5079
+ * 更新实体前的处理。
5080
+ * 1. 对所有启用了状态机管理,且 transferControl 为 true 的属性,应禁止直接更新这些属性
5081
+ * 2. 当更新实体时指定了operation,则查找启用了状态机管理的属性。
5082
+ * 如果一个模型中存在多个属性启用了状态机管理,则以 options.stateProperty 中指定的为准。
5083
+ * 对于该状态属性应用 options.operation,使其转换到下一状态。
5084
+ * @param server
5085
+ * @param model
5086
+ * @param options
5087
+ */
5088
+ async beforeUpdateEntity(server, model, options, currentEntity) {
5089
+ const entity = options.entityToSave;
5090
+ for (const property of model.properties) {
5091
+ const isStateMachineEnabled = lodash.get(property.config, "stateMachine.enabled", false);
5092
+ const isTransferControlEnabled = lodash.get(property.config, "stateMachine.transferControl", false);
5093
+ if (isStateMachineEnabled && isTransferControlEnabled && !isNullOrUndefined(entity[property.code])) {
5094
+ throw new Error(`You're not allowed to change '${property.code}' property directly when transfer control is enabled, do an operation instead.`);
5095
+ }
5096
+ }
5097
+ if (!options.operation) {
5098
+ return;
5099
+ }
5100
+ const stateMachineEnabledProperties = lodash.filter(model.properties, (property) => lodash.get(property.config, "stateMachine.enabled", false));
5101
+ let stateMachineEnabledProperty = lodash.first(stateMachineEnabledProperties);
5102
+ if (options.stateProperty) {
5103
+ stateMachineEnabledProperty = lodash.find(stateMachineEnabledProperties, (property) => property.code === options.stateProperty);
5104
+ }
5105
+ if (!stateMachineEnabledProperty) {
5106
+ throw new Error(`State machine property not found.`);
5107
+ }
5108
+ const machineConfig = lodash.get(stateMachineEnabledProperty.config, "stateMachine.config", null);
5109
+ if (!machineConfig) {
5110
+ throw new Error(`State machine of property '${stateMachineEnabledProperty.code}' not configured.`);
5111
+ }
5112
+ machineConfig.id = getStateMachineCode(model, stateMachineEnabledProperty);
5113
+ const nextSnapshot = await getStateMachineNextSnapshot(server, {
5114
+ machineConfig,
5115
+ context: {},
5116
+ currentState: currentEntity[stateMachineEnabledProperty.code],
5117
+ event: options.operation,
5118
+ });
5119
+ entity[stateMachineEnabledProperty.code] = nextSnapshot.value;
5120
+ }
5121
+ }
5122
+ function getStateMachineCode(model, property) {
5123
+ return `propertyStateMachine.${model.namespace}.${model.singularCode}.${property.code}`;
5124
+ }
5125
+
4851
5126
  class EntityWatchPlugin {
4852
5127
  #createEventEmitters;
4853
5128
  #updateEventEmitters;
@@ -5048,6 +5323,7 @@ exports.RouteContext = RouteContext;
5048
5323
  exports.RouteManagePlugin = RouteManager;
5049
5324
  exports.SequencePlugin = SequencePlugin;
5050
5325
  exports.ServerOperationPlugin = ServerOperationPlugin;
5326
+ exports.StateMachinePlugin = StateMachinePlugin;
5051
5327
  exports.WebhooksPlugin = WebhooksPlugin;
5052
5328
  exports.bootstrapApplicationConfig = bootstrapApplicationConfig$1;
5053
5329
  exports.createJwt = createJwt;