@ruiapp/rapid-core 0.1.26 → 0.1.27

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 (50) 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 +1 -0
  4. package/dist/index.js +502 -67
  5. package/dist/plugins/sequence/SequencePlugin.d.ts +18 -0
  6. package/dist/plugins/sequence/SequenceService.d.ts +15 -0
  7. package/dist/plugins/sequence/actionHandlers/generateSn.d.ts +7 -0
  8. package/dist/plugins/sequence/actionHandlers/index.d.ts +3 -0
  9. package/dist/plugins/sequence/models/SequenceAutoIncrementRecord.d.ts +3 -0
  10. package/dist/plugins/sequence/models/SequenceRule.d.ts +3 -0
  11. package/dist/plugins/sequence/models/index.d.ts +2 -0
  12. package/dist/plugins/sequence/routes/generateSn.d.ts +12 -0
  13. package/dist/plugins/sequence/routes/index.d.ts +12 -0
  14. package/dist/plugins/sequence/segment-utility.d.ts +1 -0
  15. package/dist/plugins/sequence/segments/autoIncrement.d.ts +5 -0
  16. package/dist/plugins/sequence/segments/dayOfMonth.d.ts +5 -0
  17. package/dist/plugins/sequence/segments/index.d.ts +8 -0
  18. package/dist/plugins/sequence/segments/literal.d.ts +5 -0
  19. package/dist/plugins/sequence/segments/month.d.ts +5 -0
  20. package/dist/plugins/sequence/segments/parameter.d.ts +5 -0
  21. package/dist/plugins/sequence/segments/year.d.ts +5 -0
  22. package/dist/plugins/sequence/sequence-types.d.ts +8 -0
  23. package/dist/server.d.ts +2 -1
  24. package/dist/types.d.ts +43 -0
  25. package/package.json +2 -1
  26. package/src/core/pluginManager.ts +13 -1
  27. package/src/core/response.ts +1 -0
  28. package/src/core/server.ts +4 -1
  29. package/src/dataAccess/entityManager.ts +3 -0
  30. package/src/index.ts +1 -0
  31. package/src/plugins/sequence/SequencePlugin.ts +122 -0
  32. package/src/plugins/sequence/SequenceService.ts +72 -0
  33. package/src/plugins/sequence/actionHandlers/generateSn.ts +36 -0
  34. package/src/plugins/sequence/actionHandlers/index.ts +6 -0
  35. package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -0
  36. package/src/plugins/sequence/models/SequenceRule.ts +42 -0
  37. package/src/plugins/sequence/models/index.ts +7 -0
  38. package/src/plugins/sequence/routes/generateSn.ts +15 -0
  39. package/src/plugins/sequence/routes/index.ts +5 -0
  40. package/src/plugins/sequence/segment-utility.ts +11 -0
  41. package/src/plugins/sequence/segments/autoIncrement.ts +77 -0
  42. package/src/plugins/sequence/segments/dayOfMonth.ts +16 -0
  43. package/src/plugins/sequence/segments/index.ts +16 -0
  44. package/src/plugins/sequence/segments/literal.ts +9 -0
  45. package/src/plugins/sequence/segments/month.ts +16 -0
  46. package/src/plugins/sequence/segments/parameter.ts +17 -0
  47. package/src/plugins/sequence/segments/year.ts +16 -0
  48. package/src/plugins/sequence/sequence-types.ts +10 -0
  49. package/src/server.ts +5 -0
  50. package/src/types.ts +62 -0
package/dist/index.js CHANGED
@@ -8,6 +8,7 @@ var Router = require('koa-tree-router');
8
8
  var qs = require('qs');
9
9
  var jsonwebtoken = require('jsonwebtoken');
10
10
  var crypto = require('crypto');
11
+ var dayjs = require('dayjs');
11
12
  var path = require('path');
12
13
  var fs = require('fs');
13
14
  var uuid = require('uuid');
@@ -17,6 +18,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
17
18
  var Router__default = /*#__PURE__*/_interopDefaultLegacy(Router);
18
19
  var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
19
20
  var crypto__default = /*#__PURE__*/_interopDefaultLegacy(crypto);
21
+ var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
20
22
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
21
23
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
22
24
 
@@ -545,6 +547,14 @@ class PluginManager {
545
547
  }
546
548
  }
547
549
  }
550
+ /** 在创建实体前调用。 */
551
+ async beforeCreateEntity(model, options) {
552
+ for (const plugin of this.#plugins) {
553
+ if (plugin.beforeCreateEntity) {
554
+ await plugin.beforeCreateEntity(this.#server, model, options);
555
+ }
556
+ }
557
+ }
548
558
  }
549
559
 
550
560
  class EventManager {
@@ -672,6 +682,7 @@ class RapidResponse {
672
682
  }
673
683
  json(obj, status, headers) {
674
684
  const body = JSON.stringify(obj);
685
+ this.headers.set("Content-Type", "application/json");
675
686
  const responseHeaders = new Headers(this.headers);
676
687
  if (headers) {
677
688
  mergeHeaders(responseHeaders, headers);
@@ -2280,6 +2291,7 @@ class EntityManager {
2280
2291
  }
2281
2292
  async createEntity(options, plugin) {
2282
2293
  const model = this.getModel();
2294
+ await this.#server.beforeCreateEntity(model, options);
2283
2295
  const newEntity = await createEntity(this.#server, this.#dataAccessor, options);
2284
2296
  this.#server.emitEvent("entity.create", {
2285
2297
  namespace: model.namespace,
@@ -2608,6 +2620,9 @@ class RapidServer {
2608
2620
  async beforeRunRouteActions(handlerContext) {
2609
2621
  await this.#pluginManager.beforeRunRouteActions(handlerContext);
2610
2622
  }
2623
+ async beforeCreateEntity(model, options) {
2624
+ await this.#pluginManager.beforeCreateEntity(model, options);
2625
+ }
2611
2626
  }
2612
2627
 
2613
2628
  // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
@@ -2757,32 +2772,32 @@ async function generateJwtSecretKey() {
2757
2772
  return encode(exportedKey);
2758
2773
  }
2759
2774
 
2760
- const code$k = "listMetaModels";
2761
- async function handler$k(plugin, ctx, options) {
2775
+ const code$l = "listMetaModels";
2776
+ async function handler$l(plugin, ctx, options) {
2762
2777
  const { applicationConfig } = ctx;
2763
2778
  ctx.output = { list: applicationConfig.models };
2764
2779
  }
2765
2780
 
2766
2781
  var listMetaModels = /*#__PURE__*/Object.freeze({
2767
2782
  __proto__: null,
2768
- code: code$k,
2769
- handler: handler$k
2783
+ code: code$l,
2784
+ handler: handler$l
2770
2785
  });
2771
2786
 
2772
- const code$j = "listMetaRoutes";
2773
- async function handler$j(plugin, ctx, options) {
2787
+ const code$k = "listMetaRoutes";
2788
+ async function handler$k(plugin, ctx, options) {
2774
2789
  const { applicationConfig } = ctx;
2775
2790
  ctx.output = { list: applicationConfig.routes };
2776
2791
  }
2777
2792
 
2778
2793
  var listMetaRoutes = /*#__PURE__*/Object.freeze({
2779
2794
  __proto__: null,
2780
- code: code$j,
2781
- handler: handler$j
2795
+ code: code$k,
2796
+ handler: handler$k
2782
2797
  });
2783
2798
 
2784
- const code$i = "getMetaModelDetail";
2785
- async function handler$i(plugin, ctx, options) {
2799
+ const code$j = "getMetaModelDetail";
2800
+ async function handler$j(plugin, ctx, options) {
2786
2801
  const { server, input } = ctx;
2787
2802
  const model = server.getModel(input);
2788
2803
  ctx.output = model;
@@ -2790,8 +2805,8 @@ async function handler$i(plugin, ctx, options) {
2790
2805
 
2791
2806
  var getMetaModelDetail = /*#__PURE__*/Object.freeze({
2792
2807
  __proto__: null,
2793
- code: code$i,
2794
- handler: handler$i
2808
+ code: code$j,
2809
+ handler: handler$j
2795
2810
  });
2796
2811
 
2797
2812
  /**
@@ -3143,9 +3158,9 @@ function transformFilterWithSubFilters(filter) {
3143
3158
  return filter;
3144
3159
  }
3145
3160
 
3146
- const code$h = "findCollectionEntities";
3147
- async function handler$h(plugin, ctx, options) {
3148
- await runCollectionEntityActionHandler(ctx, options, code$h, async (entityManager, input) => {
3161
+ const code$i = "findCollectionEntities";
3162
+ async function handler$i(plugin, ctx, options) {
3163
+ await runCollectionEntityActionHandler(ctx, options, code$i, async (entityManager, input) => {
3149
3164
  input.filters = removeFiltersWithNullValue(input.filters);
3150
3165
  const entities = await entityManager.findEntities(input);
3151
3166
  const result = { list: entities };
@@ -3160,14 +3175,14 @@ async function handler$h(plugin, ctx, options) {
3160
3175
 
3161
3176
  var findCollectionEntities = /*#__PURE__*/Object.freeze({
3162
3177
  __proto__: null,
3163
- code: code$h,
3164
- handler: handler$h
3178
+ code: code$i,
3179
+ handler: handler$i
3165
3180
  });
3166
3181
 
3167
- const code$g = "findCollectionEntityById";
3168
- async function handler$g(plugin, ctx, options) {
3182
+ const code$h = "findCollectionEntityById";
3183
+ async function handler$h(plugin, ctx, options) {
3169
3184
  const { logger, server, input } = ctx;
3170
- logger.debug(`Running ${code$g} handler...`, { input });
3185
+ logger.debug(`Running ${code$h} handler...`, { input });
3171
3186
  const { id } = input;
3172
3187
  const entityManager = server.getEntityManager(options.singularCode);
3173
3188
  const entity = await entityManager.findById(id);
@@ -3179,13 +3194,13 @@ async function handler$g(plugin, ctx, options) {
3179
3194
 
3180
3195
  var findCollectionEntityById = /*#__PURE__*/Object.freeze({
3181
3196
  __proto__: null,
3182
- code: code$g,
3183
- handler: handler$g
3197
+ code: code$h,
3198
+ handler: handler$h
3184
3199
  });
3185
3200
 
3186
- const code$f = "countCollectionEntities";
3187
- async function handler$f(plugin, ctx, options) {
3188
- await runCollectionEntityActionHandler(ctx, options, code$f, (entityManager, input) => {
3201
+ const code$g = "countCollectionEntities";
3202
+ async function handler$g(plugin, ctx, options) {
3203
+ await runCollectionEntityActionHandler(ctx, options, code$g, (entityManager, input) => {
3189
3204
  input.filters = removeFiltersWithNullValue(input.filters);
3190
3205
  return entityManager.count(input);
3191
3206
  });
@@ -3193,16 +3208,16 @@ async function handler$f(plugin, ctx, options) {
3193
3208
 
3194
3209
  var countCollectionEntities = /*#__PURE__*/Object.freeze({
3195
3210
  __proto__: null,
3196
- code: code$f,
3197
- handler: handler$f
3211
+ code: code$g,
3212
+ handler: handler$g
3198
3213
  });
3199
3214
 
3200
- const code$e = "createCollectionEntity";
3201
- async function handler$e(plugin, ctx, options) {
3215
+ const code$f = "createCollectionEntity";
3216
+ async function handler$f(plugin, ctx, options) {
3202
3217
  const { logger, server, input } = ctx;
3203
3218
  const { defaultInput, fixedInput } = options;
3204
3219
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3205
- logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, mergedInput });
3220
+ logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, mergedInput });
3206
3221
  const userId = ctx.routerContext.state?.userId;
3207
3222
  if (userId) {
3208
3223
  input.createdBy = userId;
@@ -3216,15 +3231,15 @@ async function handler$e(plugin, ctx, options) {
3216
3231
 
3217
3232
  var createCollectionEntity = /*#__PURE__*/Object.freeze({
3218
3233
  __proto__: null,
3219
- code: code$e,
3220
- handler: handler$e
3234
+ code: code$f,
3235
+ handler: handler$f
3221
3236
  });
3222
3237
 
3223
- const code$d = "createCollectionEntitiesBatch";
3224
- async function handler$d(plugin, ctx, options) {
3238
+ const code$e = "createCollectionEntitiesBatch";
3239
+ async function handler$e(plugin, ctx, options) {
3225
3240
  const { logger, server, input } = ctx;
3226
3241
  const { defaultInput, fixedInput } = options;
3227
- logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, input });
3242
+ logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, input });
3228
3243
  const { entities } = input;
3229
3244
  if (!lodash.isArray(entities)) {
3230
3245
  throw new Error("input.entities should be an array.");
@@ -3247,16 +3262,16 @@ async function handler$d(plugin, ctx, options) {
3247
3262
 
3248
3263
  var createCollectionEntitiesBatch = /*#__PURE__*/Object.freeze({
3249
3264
  __proto__: null,
3250
- code: code$d,
3251
- handler: handler$d
3265
+ code: code$e,
3266
+ handler: handler$e
3252
3267
  });
3253
3268
 
3254
- const code$c = "updateCollectionEntityById";
3255
- async function handler$c(plugin, ctx, options) {
3269
+ const code$d = "updateCollectionEntityById";
3270
+ async function handler$d(plugin, ctx, options) {
3256
3271
  const { logger, server, input } = ctx;
3257
3272
  const { defaultInput, fixedInput } = options;
3258
3273
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3259
- logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
3274
+ logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, mergedInput });
3260
3275
  const entityManager = server.getEntityManager(options.singularCode);
3261
3276
  const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput }, plugin);
3262
3277
  ctx.output = output;
@@ -3264,14 +3279,14 @@ async function handler$c(plugin, ctx, options) {
3264
3279
 
3265
3280
  var updateCollectionEntityById = /*#__PURE__*/Object.freeze({
3266
3281
  __proto__: null,
3267
- code: code$c,
3268
- handler: handler$c
3282
+ code: code$d,
3283
+ handler: handler$d
3269
3284
  });
3270
3285
 
3271
- const code$b = "deleteCollectionEntityById";
3272
- async function handler$b(plugin, ctx, options) {
3286
+ const code$c = "deleteCollectionEntityById";
3287
+ async function handler$c(plugin, ctx, options) {
3273
3288
  const { logger, server, input } = ctx;
3274
- logger.debug(`Running ${code$b} handler...`);
3289
+ logger.debug(`Running ${code$c} handler...`);
3275
3290
  const entityManager = server.getEntityManager(options.singularCode);
3276
3291
  await entityManager.deleteById(input.id, plugin);
3277
3292
  ctx.status = 200;
@@ -3280,16 +3295,16 @@ async function handler$b(plugin, ctx, options) {
3280
3295
 
3281
3296
  var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
3282
3297
  __proto__: null,
3283
- code: code$b,
3284
- handler: handler$b
3298
+ code: code$c,
3299
+ handler: handler$c
3285
3300
  });
3286
3301
 
3287
- const code$a = "addEntityRelations";
3288
- async function handler$a(plugin, ctx, options) {
3302
+ const code$b = "addEntityRelations";
3303
+ async function handler$b(plugin, ctx, options) {
3289
3304
  const { logger, server, input } = ctx;
3290
3305
  const { defaultInput, fixedInput } = options;
3291
3306
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3292
- logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3307
+ logger.debug(`Running ${code$b} handler...`, { defaultInput, fixedInput, mergedInput });
3293
3308
  const entityManager = server.getEntityManager(options.singularCode);
3294
3309
  await entityManager.addRelations(mergedInput, plugin);
3295
3310
  ctx.output = {};
@@ -3297,16 +3312,16 @@ async function handler$a(plugin, ctx, options) {
3297
3312
 
3298
3313
  var addEntityRelations = /*#__PURE__*/Object.freeze({
3299
3314
  __proto__: null,
3300
- code: code$a,
3301
- handler: handler$a
3315
+ code: code$b,
3316
+ handler: handler$b
3302
3317
  });
3303
3318
 
3304
- const code$9 = "removeEntityRelations";
3305
- async function handler$9(plugin, ctx, options) {
3319
+ const code$a = "removeEntityRelations";
3320
+ async function handler$a(plugin, ctx, options) {
3306
3321
  const { logger, server, input } = ctx;
3307
3322
  const { defaultInput, fixedInput } = options;
3308
3323
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3309
- logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
3324
+ logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3310
3325
  const entityManager = server.getEntityManager(options.singularCode);
3311
3326
  await entityManager.removeRelations(mergedInput, plugin);
3312
3327
  ctx.output = {};
@@ -3314,16 +3329,16 @@ async function handler$9(plugin, ctx, options) {
3314
3329
 
3315
3330
  var removeEntityRelations = /*#__PURE__*/Object.freeze({
3316
3331
  __proto__: null,
3317
- code: code$9,
3318
- handler: handler$9
3332
+ code: code$a,
3333
+ handler: handler$a
3319
3334
  });
3320
3335
 
3321
- const code$8 = "queryDatabase";
3322
- async function handler$8(plugin, ctx, options) {
3336
+ const code$9 = "queryDatabase";
3337
+ async function handler$9(plugin, ctx, options) {
3323
3338
  const { logger, server, input } = ctx;
3324
3339
  const { sql, querySingle, defaultInput, fixedInput } = options;
3325
3340
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3326
- logger.debug(`Running ${code$8} handler...`, { defaultInput, fixedInput, mergedInput });
3341
+ logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
3327
3342
  const result = await server.queryDatabaseObject(sql, mergedInput);
3328
3343
  if (querySingle) {
3329
3344
  ctx.output = lodash.first(result);
@@ -3335,8 +3350,8 @@ async function handler$8(plugin, ctx, options) {
3335
3350
 
3336
3351
  var queryDatabase = /*#__PURE__*/Object.freeze({
3337
3352
  __proto__: null,
3338
- code: code$8,
3339
- handler: handler$8
3353
+ code: code$9,
3354
+ handler: handler$9
3340
3355
  });
3341
3356
 
3342
3357
  /**
@@ -3507,17 +3522,17 @@ async function sendSourceResponse(proxyCtx, targetRes) {
3507
3522
  srcRes.body = targetRes.body;
3508
3523
  }
3509
3524
 
3510
- const code$7 = "httpProxy";
3511
- async function handler$7(plugin, ctx, options) {
3525
+ const code$8 = "httpProxy";
3526
+ async function handler$8(plugin, ctx, options) {
3512
3527
  const { logger } = ctx;
3513
- logger.debug(`Running ${code$7} handler...`);
3528
+ logger.debug(`Running ${code$8} handler...`);
3514
3529
  await doProxy(ctx.routerContext, options);
3515
3530
  }
3516
3531
 
3517
3532
  var httpProxy = /*#__PURE__*/Object.freeze({
3518
3533
  __proto__: null,
3519
- code: code$7,
3520
- handler: handler$7
3534
+ code: code$8,
3535
+ handler: handler$8
3521
3536
  });
3522
3537
 
3523
3538
  /**
@@ -3566,6 +3581,425 @@ class RouteManager {
3566
3581
  }
3567
3582
  }
3568
3583
 
3584
+ const segmentType$5 = "literal";
3585
+ async function resolveSegmentValue$5(server, ruleCode, config, input) {
3586
+ return config.content || "";
3587
+ }
3588
+
3589
+ var literal = /*#__PURE__*/Object.freeze({
3590
+ __proto__: null,
3591
+ segmentType: segmentType$5,
3592
+ resolveSegmentValue: resolveSegmentValue$5
3593
+ });
3594
+
3595
+ function padSegment(segment, length, fillString) {
3596
+ if (!length) {
3597
+ return segment;
3598
+ }
3599
+ if (segment.length > length) {
3600
+ return segment.substring(segment.length - length);
3601
+ }
3602
+ else {
3603
+ return segment.padStart(length, fillString || " ");
3604
+ }
3605
+ }
3606
+
3607
+ const segmentType$4 = "year";
3608
+ async function resolveSegmentValue$4(server, ruleCode, config, input) {
3609
+ const segmentValue = (new Date).getFullYear().toString();
3610
+ return padSegment(segmentValue, config.length || 4, config.padding || "0");
3611
+ }
3612
+
3613
+ var year = /*#__PURE__*/Object.freeze({
3614
+ __proto__: null,
3615
+ segmentType: segmentType$4,
3616
+ resolveSegmentValue: resolveSegmentValue$4
3617
+ });
3618
+
3619
+ const segmentType$3 = "month";
3620
+ async function resolveSegmentValue$3(server, ruleCode, config, input) {
3621
+ const segmentValue = ((new Date).getMonth() + 1).toString();
3622
+ return padSegment(segmentValue, config.length || 2, config.padding || "0");
3623
+ }
3624
+
3625
+ var month = /*#__PURE__*/Object.freeze({
3626
+ __proto__: null,
3627
+ segmentType: segmentType$3,
3628
+ resolveSegmentValue: resolveSegmentValue$3
3629
+ });
3630
+
3631
+ const segmentType$2 = "dayOfMonth";
3632
+ async function resolveSegmentValue$2(server, ruleCode, config, input) {
3633
+ const segmentValue = (new Date).getDate().toString();
3634
+ return padSegment(segmentValue, config.length || 2, config.padding || "0");
3635
+ }
3636
+
3637
+ var dayOfMonth = /*#__PURE__*/Object.freeze({
3638
+ __proto__: null,
3639
+ segmentType: segmentType$2,
3640
+ resolveSegmentValue: resolveSegmentValue$2
3641
+ });
3642
+
3643
+ const segmentType$1 = "parameter";
3644
+ async function resolveSegmentValue$1(server, ruleCode, config, input) {
3645
+ const segmentValue = lodash.get(input.parameters, config.parameterName, "");
3646
+ return padSegment(segmentValue, config.length, config.padding);
3647
+ }
3648
+
3649
+ var parameter = /*#__PURE__*/Object.freeze({
3650
+ __proto__: null,
3651
+ segmentType: segmentType$1,
3652
+ resolveSegmentValue: resolveSegmentValue$1
3653
+ });
3654
+
3655
+ const segmentType = "autoIncrement";
3656
+ async function resolveSegmentValue(server, ruleCode, config, input) {
3657
+ const autoIncrementRecordDataAccessor = server.getDataAccessor({
3658
+ singularCode: "sequence_auto_increment_record",
3659
+ });
3660
+ const scope = lodash.get(input.parameters, config.scope, "");
3661
+ const autoIncrementRecord = await autoIncrementRecordDataAccessor.findOne({
3662
+ filters: [
3663
+ {
3664
+ operator: "eq",
3665
+ field: "rule_code",
3666
+ value: ruleCode,
3667
+ },
3668
+ {
3669
+ operator: "eq",
3670
+ field: "scope",
3671
+ value: scope,
3672
+ },
3673
+ ],
3674
+ });
3675
+ let now = dayjs__default["default"]();
3676
+ let nowString = now.format("YYYY-MM-DD HH:mm:ss.SSS Z");
3677
+ let nextValue = 1;
3678
+ if (autoIncrementRecord) {
3679
+ let shouldReset = false;
3680
+ const period = config.period || "forever";
3681
+ const lastUpdate = dayjs__default["default"](autoIncrementRecord.updated_at);
3682
+ if (period === "year") {
3683
+ if (now.year() > lastUpdate.year()) {
3684
+ shouldReset = true;
3685
+ }
3686
+ }
3687
+ else if (period === "month") {
3688
+ if (now.year() !== lastUpdate.year() || now.month() !== lastUpdate.month()) {
3689
+ shouldReset = true;
3690
+ }
3691
+ }
3692
+ else if (period === "day") {
3693
+ if (now.year() !== lastUpdate.year() || now.month() !== lastUpdate.month() || now.date() !== lastUpdate.date()) {
3694
+ shouldReset = true;
3695
+ }
3696
+ }
3697
+ if (!shouldReset) {
3698
+ nextValue = autoIncrementRecord.current_value + 1;
3699
+ }
3700
+ await autoIncrementRecordDataAccessor.updateById(autoIncrementRecord.id, {
3701
+ current_value: nextValue,
3702
+ updated_at: nowString,
3703
+ });
3704
+ }
3705
+ else {
3706
+ await autoIncrementRecordDataAccessor.create({
3707
+ rule_code: ruleCode,
3708
+ scope: scope,
3709
+ current_value: nextValue,
3710
+ updated_at: nowString,
3711
+ });
3712
+ }
3713
+ const segmentValue = nextValue.toString();
3714
+ return padSegment(segmentValue, config.length, config.padding || "0");
3715
+ }
3716
+
3717
+ var autoIncrement = /*#__PURE__*/Object.freeze({
3718
+ __proto__: null,
3719
+ segmentType: segmentType,
3720
+ resolveSegmentValue: resolveSegmentValue
3721
+ });
3722
+
3723
+ var segmentResolvers = [
3724
+ literal,
3725
+ year,
3726
+ month,
3727
+ dayOfMonth,
3728
+ parameter,
3729
+ autoIncrement,
3730
+ ];
3731
+
3732
+ async function generateSn$2(server, input) {
3733
+ const sequenceNumbers = [];
3734
+ const { ruleCode, parameters } = input;
3735
+ let { amount } = input;
3736
+ if (!amount) {
3737
+ amount = 1;
3738
+ }
3739
+ const sequenceRuleDataAccessor = server.getDataAccessor({
3740
+ singularCode: "sequence_rule",
3741
+ });
3742
+ const sequenceRule = await sequenceRuleDataAccessor.findOne({
3743
+ filters: [
3744
+ {
3745
+ operator: "eq",
3746
+ field: "code",
3747
+ value: ruleCode,
3748
+ }
3749
+ ]
3750
+ });
3751
+ if (!sequenceRule) {
3752
+ throw new Error(`Failed to generate sequence number. Sequence with code '${sequenceRule.code}' not found.`);
3753
+ }
3754
+ const sequenceConfig = sequenceRule.config;
3755
+ if (!sequenceConfig || !sequenceConfig.segments) {
3756
+ throw new Error("Failed to generate sequence number. Sequence not configured.");
3757
+ }
3758
+ for (let i = 0; i < amount; i++) {
3759
+ let sequenceNumber = "";
3760
+ for (const segmentConfig of sequenceConfig.segments) {
3761
+ const segmentResolver = lodash.find(segmentResolvers, (item) => item.segmentType === segmentConfig.type);
3762
+ if (!segmentResolver) {
3763
+ // TODO: deal with unkown segment type
3764
+ continue;
3765
+ }
3766
+ const segment = await segmentResolver.resolveSegmentValue(server, ruleCode, segmentConfig, input);
3767
+ sequenceNumber += segment;
3768
+ }
3769
+ sequenceNumbers.push(sequenceNumber);
3770
+ }
3771
+ return sequenceNumbers;
3772
+ }
3773
+
3774
+ const code$7 = "generateSn";
3775
+ async function handler$7(plugin, ctx, options) {
3776
+ const { server, routerContext } = ctx;
3777
+ const input = ctx.input;
3778
+ if (options?.ruleCode) {
3779
+ input.ruleCode = options.ruleCode;
3780
+ }
3781
+ if (!input.ruleCode) {
3782
+ throw new Error(`Rule code is required when generating sequence numbers.`);
3783
+ }
3784
+ const sequences = await generateSn$2(server, input);
3785
+ ctx.output = {
3786
+ sequences,
3787
+ };
3788
+ }
3789
+
3790
+ var generateSn$1 = /*#__PURE__*/Object.freeze({
3791
+ __proto__: null,
3792
+ code: code$7,
3793
+ handler: handler$7
3794
+ });
3795
+
3796
+ var pluginActionHandlers$2 = [
3797
+ generateSn$1,
3798
+ ];
3799
+
3800
+ var SequenceRule = {
3801
+ maintainedBy: "sequencePlugin",
3802
+ namespace: "svc",
3803
+ name: "sequence_rule",
3804
+ singularCode: "sequence_rule",
3805
+ pluralCode: "sequence_rules",
3806
+ schema: "public",
3807
+ tableName: "sequence_rules",
3808
+ properties: [
3809
+ {
3810
+ name: "id",
3811
+ code: "id",
3812
+ columnName: "id",
3813
+ type: "integer",
3814
+ required: true,
3815
+ autoIncrement: true,
3816
+ },
3817
+ {
3818
+ name: "code",
3819
+ code: "code",
3820
+ columnName: "code",
3821
+ type: "text",
3822
+ required: true,
3823
+ },
3824
+ {
3825
+ name: "description",
3826
+ code: "description",
3827
+ columnName: "description",
3828
+ type: "text",
3829
+ required: false,
3830
+ },
3831
+ {
3832
+ name: "config",
3833
+ code: "config",
3834
+ columnName: "config",
3835
+ type: "json",
3836
+ required: false,
3837
+ },
3838
+ ],
3839
+ };
3840
+
3841
+ var SequenceAutoIncrementRecord = {
3842
+ maintainedBy: "sequencePlugin",
3843
+ namespace: "svc",
3844
+ name: "sequence_auto_increment_record",
3845
+ singularCode: "sequence_auto_increment_record",
3846
+ pluralCode: "sequence_auto_increment_records",
3847
+ schema: "public",
3848
+ tableName: "sequence_auto_increment_records",
3849
+ properties: [
3850
+ {
3851
+ name: "id",
3852
+ code: "id",
3853
+ columnName: "id",
3854
+ type: "integer",
3855
+ required: true,
3856
+ autoIncrement: true,
3857
+ },
3858
+ {
3859
+ name: "ruleCode",
3860
+ code: "ruleCode",
3861
+ columnName: "rule_code",
3862
+ type: "text",
3863
+ required: true,
3864
+ },
3865
+ {
3866
+ name: "scope",
3867
+ code: "scope",
3868
+ columnName: "scope",
3869
+ type: "text",
3870
+ required: false,
3871
+ },
3872
+ {
3873
+ name: "currentValue",
3874
+ code: "currentValue",
3875
+ columnName: "current_value",
3876
+ type: "integer",
3877
+ required: true,
3878
+ },
3879
+ {
3880
+ name: "updatedAt",
3881
+ code: "updatedAt",
3882
+ columnName: "updated_at",
3883
+ type: "datetime",
3884
+ required: true,
3885
+ },
3886
+ ],
3887
+ };
3888
+
3889
+ var pluginModels$1 = [
3890
+ SequenceRule,
3891
+ SequenceAutoIncrementRecord,
3892
+ ];
3893
+
3894
+ var generateSn = {
3895
+ namespace: "svc",
3896
+ name: "svc.generateSn",
3897
+ code: "svc.generateSn",
3898
+ type: "RESTful",
3899
+ method: "POST",
3900
+ endpoint: "/svc/generateSn",
3901
+ actions: [
3902
+ {
3903
+ code: "generateSn",
3904
+ },
3905
+ ],
3906
+ };
3907
+
3908
+ var pluginRoutes$2 = [
3909
+ generateSn,
3910
+ ];
3911
+
3912
+ /**
3913
+ * Sequence plugin
3914
+ */
3915
+ class SequencePlugin {
3916
+ get code() {
3917
+ return "sequencePlugin";
3918
+ }
3919
+ get description() {
3920
+ return null;
3921
+ }
3922
+ get extendingAbilities() {
3923
+ return [];
3924
+ }
3925
+ get configurableTargets() {
3926
+ return [];
3927
+ }
3928
+ get configurations() {
3929
+ return [];
3930
+ }
3931
+ async registerActionHandlers(server) {
3932
+ for (const actionHandler of pluginActionHandlers$2) {
3933
+ server.registerActionHandler(this, actionHandler);
3934
+ }
3935
+ }
3936
+ async configureModels(server, applicationConfig) {
3937
+ server.appendApplicationConfig({ models: pluginModels$1 });
3938
+ }
3939
+ async configureRoutes(server, applicationConfig) {
3940
+ server.appendApplicationConfig({ routes: pluginRoutes$2 });
3941
+ }
3942
+ async onApplicationLoaded(server, applicationConfig) {
3943
+ const models = server.getApplicationConfig().models;
3944
+ for (const model of models) {
3945
+ for (const property of model.properties) {
3946
+ const sequenceConfig = property.config?.sequence;
3947
+ if (sequenceConfig) {
3948
+ const ruleCode = getSequenceRuleCode(model, property);
3949
+ const ruleConfig = sequenceConfig.ruleConfig;
3950
+ const sequenceRuleDataAccessor = server.getDataAccessor({
3951
+ singularCode: "sequence_rule",
3952
+ });
3953
+ const sequenceRule = await sequenceRuleDataAccessor.findOne({
3954
+ filters: [
3955
+ {
3956
+ operator: "eq",
3957
+ field: "code",
3958
+ value: ruleCode,
3959
+ },
3960
+ ],
3961
+ });
3962
+ if (sequenceRule) {
3963
+ if (JSON.stringify(sequenceRule.config) !== JSON.stringify(ruleConfig)) {
3964
+ await sequenceRuleDataAccessor.updateById(sequenceRule.id, {
3965
+ config: ruleConfig,
3966
+ });
3967
+ }
3968
+ }
3969
+ else {
3970
+ await sequenceRuleDataAccessor.create({
3971
+ code: ruleCode,
3972
+ config: ruleConfig,
3973
+ });
3974
+ }
3975
+ }
3976
+ }
3977
+ }
3978
+ }
3979
+ async beforeCreateEntity(server, model, options) {
3980
+ debugger;
3981
+ const entity = options.entity;
3982
+ for (const property of model.properties) {
3983
+ const sequenceConfig = property.config?.sequence;
3984
+ const propertyValue = entity[property.code];
3985
+ if (sequenceConfig &&
3986
+ sequenceConfig.autoGenerate &&
3987
+ (lodash.isUndefined(propertyValue) || lodash.isNull(propertyValue))) {
3988
+ const ruleCode = getSequenceRuleCode(model, property);
3989
+ const sns = await generateSn$2(server, {
3990
+ ruleCode,
3991
+ amount: 1,
3992
+ parameters: entity,
3993
+ });
3994
+ entity[property.code] = sns[0];
3995
+ }
3996
+ }
3997
+ }
3998
+ }
3999
+ function getSequenceRuleCode(model, property) {
4000
+ return `propertyAutoGenerate.${model.namespace}.${model.singularCode}.${property.code}`;
4001
+ }
4002
+
3569
4003
  var pluginConfig = {
3570
4004
  models: [
3571
4005
  {
@@ -4484,6 +4918,7 @@ exports.RapidRequest = RapidRequest;
4484
4918
  exports.RapidServer = RapidServer;
4485
4919
  exports.RouteContext = RouteContext;
4486
4920
  exports.RouteManagePlugin = RouteManager;
4921
+ exports.SequencePlugin = SequencePlugin;
4487
4922
  exports.ServerOperationPlugin = ServerOperationPlugin;
4488
4923
  exports.WebhooksPlugin = WebhooksPlugin;
4489
4924
  exports.bootstrapApplicationConfig = bootstrapApplicationConfig$1;