@ruiapp/rapid-core 0.1.25 → 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 +506 -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 +44 -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 +11 -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 +67 -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);
@@ -1951,6 +1962,10 @@ async function replaceFiltersWithFiltersOperator(server, model, filters) {
1951
1962
  }
1952
1963
  }
1953
1964
  else {
1965
+ const property = lodash.find(model.properties, (property) => property.code === filter.field);
1966
+ if (property) {
1967
+ filter.field = property.columnName || property.code;
1968
+ }
1954
1969
  replacedFilters.push(filter);
1955
1970
  }
1956
1971
  }
@@ -2276,6 +2291,7 @@ class EntityManager {
2276
2291
  }
2277
2292
  async createEntity(options, plugin) {
2278
2293
  const model = this.getModel();
2294
+ await this.#server.beforeCreateEntity(model, options);
2279
2295
  const newEntity = await createEntity(this.#server, this.#dataAccessor, options);
2280
2296
  this.#server.emitEvent("entity.create", {
2281
2297
  namespace: model.namespace,
@@ -2604,6 +2620,9 @@ class RapidServer {
2604
2620
  async beforeRunRouteActions(handlerContext) {
2605
2621
  await this.#pluginManager.beforeRunRouteActions(handlerContext);
2606
2622
  }
2623
+ async beforeCreateEntity(model, options) {
2624
+ await this.#pluginManager.beforeCreateEntity(model, options);
2625
+ }
2607
2626
  }
2608
2627
 
2609
2628
  // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
@@ -2753,32 +2772,32 @@ async function generateJwtSecretKey() {
2753
2772
  return encode(exportedKey);
2754
2773
  }
2755
2774
 
2756
- const code$k = "listMetaModels";
2757
- async function handler$k(plugin, ctx, options) {
2775
+ const code$l = "listMetaModels";
2776
+ async function handler$l(plugin, ctx, options) {
2758
2777
  const { applicationConfig } = ctx;
2759
2778
  ctx.output = { list: applicationConfig.models };
2760
2779
  }
2761
2780
 
2762
2781
  var listMetaModels = /*#__PURE__*/Object.freeze({
2763
2782
  __proto__: null,
2764
- code: code$k,
2765
- handler: handler$k
2783
+ code: code$l,
2784
+ handler: handler$l
2766
2785
  });
2767
2786
 
2768
- const code$j = "listMetaRoutes";
2769
- async function handler$j(plugin, ctx, options) {
2787
+ const code$k = "listMetaRoutes";
2788
+ async function handler$k(plugin, ctx, options) {
2770
2789
  const { applicationConfig } = ctx;
2771
2790
  ctx.output = { list: applicationConfig.routes };
2772
2791
  }
2773
2792
 
2774
2793
  var listMetaRoutes = /*#__PURE__*/Object.freeze({
2775
2794
  __proto__: null,
2776
- code: code$j,
2777
- handler: handler$j
2795
+ code: code$k,
2796
+ handler: handler$k
2778
2797
  });
2779
2798
 
2780
- const code$i = "getMetaModelDetail";
2781
- async function handler$i(plugin, ctx, options) {
2799
+ const code$j = "getMetaModelDetail";
2800
+ async function handler$j(plugin, ctx, options) {
2782
2801
  const { server, input } = ctx;
2783
2802
  const model = server.getModel(input);
2784
2803
  ctx.output = model;
@@ -2786,8 +2805,8 @@ async function handler$i(plugin, ctx, options) {
2786
2805
 
2787
2806
  var getMetaModelDetail = /*#__PURE__*/Object.freeze({
2788
2807
  __proto__: null,
2789
- code: code$i,
2790
- handler: handler$i
2808
+ code: code$j,
2809
+ handler: handler$j
2791
2810
  });
2792
2811
 
2793
2812
  /**
@@ -3139,9 +3158,9 @@ function transformFilterWithSubFilters(filter) {
3139
3158
  return filter;
3140
3159
  }
3141
3160
 
3142
- const code$h = "findCollectionEntities";
3143
- async function handler$h(plugin, ctx, options) {
3144
- 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) => {
3145
3164
  input.filters = removeFiltersWithNullValue(input.filters);
3146
3165
  const entities = await entityManager.findEntities(input);
3147
3166
  const result = { list: entities };
@@ -3156,14 +3175,14 @@ async function handler$h(plugin, ctx, options) {
3156
3175
 
3157
3176
  var findCollectionEntities = /*#__PURE__*/Object.freeze({
3158
3177
  __proto__: null,
3159
- code: code$h,
3160
- handler: handler$h
3178
+ code: code$i,
3179
+ handler: handler$i
3161
3180
  });
3162
3181
 
3163
- const code$g = "findCollectionEntityById";
3164
- async function handler$g(plugin, ctx, options) {
3182
+ const code$h = "findCollectionEntityById";
3183
+ async function handler$h(plugin, ctx, options) {
3165
3184
  const { logger, server, input } = ctx;
3166
- logger.debug(`Running ${code$g} handler...`, { input });
3185
+ logger.debug(`Running ${code$h} handler...`, { input });
3167
3186
  const { id } = input;
3168
3187
  const entityManager = server.getEntityManager(options.singularCode);
3169
3188
  const entity = await entityManager.findById(id);
@@ -3175,13 +3194,13 @@ async function handler$g(plugin, ctx, options) {
3175
3194
 
3176
3195
  var findCollectionEntityById = /*#__PURE__*/Object.freeze({
3177
3196
  __proto__: null,
3178
- code: code$g,
3179
- handler: handler$g
3197
+ code: code$h,
3198
+ handler: handler$h
3180
3199
  });
3181
3200
 
3182
- const code$f = "countCollectionEntities";
3183
- async function handler$f(plugin, ctx, options) {
3184
- 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) => {
3185
3204
  input.filters = removeFiltersWithNullValue(input.filters);
3186
3205
  return entityManager.count(input);
3187
3206
  });
@@ -3189,16 +3208,16 @@ async function handler$f(plugin, ctx, options) {
3189
3208
 
3190
3209
  var countCollectionEntities = /*#__PURE__*/Object.freeze({
3191
3210
  __proto__: null,
3192
- code: code$f,
3193
- handler: handler$f
3211
+ code: code$g,
3212
+ handler: handler$g
3194
3213
  });
3195
3214
 
3196
- const code$e = "createCollectionEntity";
3197
- async function handler$e(plugin, ctx, options) {
3215
+ const code$f = "createCollectionEntity";
3216
+ async function handler$f(plugin, ctx, options) {
3198
3217
  const { logger, server, input } = ctx;
3199
3218
  const { defaultInput, fixedInput } = options;
3200
3219
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3201
- logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, mergedInput });
3220
+ logger.debug(`Running ${code$f} handler...`, { defaultInput, fixedInput, mergedInput });
3202
3221
  const userId = ctx.routerContext.state?.userId;
3203
3222
  if (userId) {
3204
3223
  input.createdBy = userId;
@@ -3212,15 +3231,15 @@ async function handler$e(plugin, ctx, options) {
3212
3231
 
3213
3232
  var createCollectionEntity = /*#__PURE__*/Object.freeze({
3214
3233
  __proto__: null,
3215
- code: code$e,
3216
- handler: handler$e
3234
+ code: code$f,
3235
+ handler: handler$f
3217
3236
  });
3218
3237
 
3219
- const code$d = "createCollectionEntitiesBatch";
3220
- async function handler$d(plugin, ctx, options) {
3238
+ const code$e = "createCollectionEntitiesBatch";
3239
+ async function handler$e(plugin, ctx, options) {
3221
3240
  const { logger, server, input } = ctx;
3222
3241
  const { defaultInput, fixedInput } = options;
3223
- logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, input });
3242
+ logger.debug(`Running ${code$e} handler...`, { defaultInput, fixedInput, input });
3224
3243
  const { entities } = input;
3225
3244
  if (!lodash.isArray(entities)) {
3226
3245
  throw new Error("input.entities should be an array.");
@@ -3243,16 +3262,16 @@ async function handler$d(plugin, ctx, options) {
3243
3262
 
3244
3263
  var createCollectionEntitiesBatch = /*#__PURE__*/Object.freeze({
3245
3264
  __proto__: null,
3246
- code: code$d,
3247
- handler: handler$d
3265
+ code: code$e,
3266
+ handler: handler$e
3248
3267
  });
3249
3268
 
3250
- const code$c = "updateCollectionEntityById";
3251
- async function handler$c(plugin, ctx, options) {
3269
+ const code$d = "updateCollectionEntityById";
3270
+ async function handler$d(plugin, ctx, options) {
3252
3271
  const { logger, server, input } = ctx;
3253
3272
  const { defaultInput, fixedInput } = options;
3254
3273
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3255
- logger.debug(`Running ${code$c} handler...`, { defaultInput, fixedInput, mergedInput });
3274
+ logger.debug(`Running ${code$d} handler...`, { defaultInput, fixedInput, mergedInput });
3256
3275
  const entityManager = server.getEntityManager(options.singularCode);
3257
3276
  const output = await entityManager.updateEntityById({ id: mergedInput.id, entityToSave: mergedInput }, plugin);
3258
3277
  ctx.output = output;
@@ -3260,14 +3279,14 @@ async function handler$c(plugin, ctx, options) {
3260
3279
 
3261
3280
  var updateCollectionEntityById = /*#__PURE__*/Object.freeze({
3262
3281
  __proto__: null,
3263
- code: code$c,
3264
- handler: handler$c
3282
+ code: code$d,
3283
+ handler: handler$d
3265
3284
  });
3266
3285
 
3267
- const code$b = "deleteCollectionEntityById";
3268
- async function handler$b(plugin, ctx, options) {
3286
+ const code$c = "deleteCollectionEntityById";
3287
+ async function handler$c(plugin, ctx, options) {
3269
3288
  const { logger, server, input } = ctx;
3270
- logger.debug(`Running ${code$b} handler...`);
3289
+ logger.debug(`Running ${code$c} handler...`);
3271
3290
  const entityManager = server.getEntityManager(options.singularCode);
3272
3291
  await entityManager.deleteById(input.id, plugin);
3273
3292
  ctx.status = 200;
@@ -3276,16 +3295,16 @@ async function handler$b(plugin, ctx, options) {
3276
3295
 
3277
3296
  var deleteCollectionEntityById = /*#__PURE__*/Object.freeze({
3278
3297
  __proto__: null,
3279
- code: code$b,
3280
- handler: handler$b
3298
+ code: code$c,
3299
+ handler: handler$c
3281
3300
  });
3282
3301
 
3283
- const code$a = "addEntityRelations";
3284
- async function handler$a(plugin, ctx, options) {
3302
+ const code$b = "addEntityRelations";
3303
+ async function handler$b(plugin, ctx, options) {
3285
3304
  const { logger, server, input } = ctx;
3286
3305
  const { defaultInput, fixedInput } = options;
3287
3306
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3288
- logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3307
+ logger.debug(`Running ${code$b} handler...`, { defaultInput, fixedInput, mergedInput });
3289
3308
  const entityManager = server.getEntityManager(options.singularCode);
3290
3309
  await entityManager.addRelations(mergedInput, plugin);
3291
3310
  ctx.output = {};
@@ -3293,16 +3312,16 @@ async function handler$a(plugin, ctx, options) {
3293
3312
 
3294
3313
  var addEntityRelations = /*#__PURE__*/Object.freeze({
3295
3314
  __proto__: null,
3296
- code: code$a,
3297
- handler: handler$a
3315
+ code: code$b,
3316
+ handler: handler$b
3298
3317
  });
3299
3318
 
3300
- const code$9 = "removeEntityRelations";
3301
- async function handler$9(plugin, ctx, options) {
3319
+ const code$a = "removeEntityRelations";
3320
+ async function handler$a(plugin, ctx, options) {
3302
3321
  const { logger, server, input } = ctx;
3303
3322
  const { defaultInput, fixedInput } = options;
3304
3323
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3305
- logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
3324
+ logger.debug(`Running ${code$a} handler...`, { defaultInput, fixedInput, mergedInput });
3306
3325
  const entityManager = server.getEntityManager(options.singularCode);
3307
3326
  await entityManager.removeRelations(mergedInput, plugin);
3308
3327
  ctx.output = {};
@@ -3310,16 +3329,16 @@ async function handler$9(plugin, ctx, options) {
3310
3329
 
3311
3330
  var removeEntityRelations = /*#__PURE__*/Object.freeze({
3312
3331
  __proto__: null,
3313
- code: code$9,
3314
- handler: handler$9
3332
+ code: code$a,
3333
+ handler: handler$a
3315
3334
  });
3316
3335
 
3317
- const code$8 = "queryDatabase";
3318
- async function handler$8(plugin, ctx, options) {
3336
+ const code$9 = "queryDatabase";
3337
+ async function handler$9(plugin, ctx, options) {
3319
3338
  const { logger, server, input } = ctx;
3320
3339
  const { sql, querySingle, defaultInput, fixedInput } = options;
3321
3340
  const mergedInput = mergeInput(defaultInput, input, fixedInput);
3322
- logger.debug(`Running ${code$8} handler...`, { defaultInput, fixedInput, mergedInput });
3341
+ logger.debug(`Running ${code$9} handler...`, { defaultInput, fixedInput, mergedInput });
3323
3342
  const result = await server.queryDatabaseObject(sql, mergedInput);
3324
3343
  if (querySingle) {
3325
3344
  ctx.output = lodash.first(result);
@@ -3331,8 +3350,8 @@ async function handler$8(plugin, ctx, options) {
3331
3350
 
3332
3351
  var queryDatabase = /*#__PURE__*/Object.freeze({
3333
3352
  __proto__: null,
3334
- code: code$8,
3335
- handler: handler$8
3353
+ code: code$9,
3354
+ handler: handler$9
3336
3355
  });
3337
3356
 
3338
3357
  /**
@@ -3503,17 +3522,17 @@ async function sendSourceResponse(proxyCtx, targetRes) {
3503
3522
  srcRes.body = targetRes.body;
3504
3523
  }
3505
3524
 
3506
- const code$7 = "httpProxy";
3507
- async function handler$7(plugin, ctx, options) {
3525
+ const code$8 = "httpProxy";
3526
+ async function handler$8(plugin, ctx, options) {
3508
3527
  const { logger } = ctx;
3509
- logger.debug(`Running ${code$7} handler...`);
3528
+ logger.debug(`Running ${code$8} handler...`);
3510
3529
  await doProxy(ctx.routerContext, options);
3511
3530
  }
3512
3531
 
3513
3532
  var httpProxy = /*#__PURE__*/Object.freeze({
3514
3533
  __proto__: null,
3515
- code: code$7,
3516
- handler: handler$7
3534
+ code: code$8,
3535
+ handler: handler$8
3517
3536
  });
3518
3537
 
3519
3538
  /**
@@ -3562,6 +3581,425 @@ class RouteManager {
3562
3581
  }
3563
3582
  }
3564
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
+
3565
4003
  var pluginConfig = {
3566
4004
  models: [
3567
4005
  {
@@ -4480,6 +4918,7 @@ exports.RapidRequest = RapidRequest;
4480
4918
  exports.RapidServer = RapidServer;
4481
4919
  exports.RouteContext = RouteContext;
4482
4920
  exports.RouteManagePlugin = RouteManager;
4921
+ exports.SequencePlugin = SequencePlugin;
4483
4922
  exports.ServerOperationPlugin = ServerOperationPlugin;
4484
4923
  exports.WebhooksPlugin = WebhooksPlugin;
4485
4924
  exports.bootstrapApplicationConfig = bootstrapApplicationConfig$1;