nicot 1.2.5 → 1.2.6

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.
package/dist/index.cjs CHANGED
@@ -1473,6 +1473,30 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
1473
1473
 
1474
1474
  // src/crud-base.ts
1475
1475
  var import_p_queue = __toESM(require("p-queue"));
1476
+
1477
+ // src/utility/observe-diff.ts
1478
+ var observeDiff = (obj, cb) => {
1479
+ return new Proxy(obj, {
1480
+ set(target, key, value) {
1481
+ const oldValue = target[key];
1482
+ const type = Object.prototype.hasOwnProperty.call(target, key) ? "update" : "add";
1483
+ target[key] = value;
1484
+ cb({ type, key, oldValue, newValue: value });
1485
+ return true;
1486
+ },
1487
+ deleteProperty(target, key) {
1488
+ if (Object.prototype.hasOwnProperty.call(target, key)) {
1489
+ const oldValue = target[key];
1490
+ delete target[key];
1491
+ cb({ type: "delete", key, oldValue, newValue: void 0 });
1492
+ return true;
1493
+ }
1494
+ return false;
1495
+ }
1496
+ });
1497
+ };
1498
+
1499
+ // src/crud-base.ts
1476
1500
  var Relation = (name, options = {}) => {
1477
1501
  return { name, inner: false, ...options };
1478
1502
  };
@@ -2079,13 +2103,72 @@ var CrudBase = class {
2079
2103
  })
2080
2104
  );
2081
2105
  }
2082
- async exists(id) {
2106
+ async exists(id, cond = {}) {
2083
2107
  const bindingEnt = await this.getBindingPartialEntity();
2084
- const ent = await this.repo.findOne({
2085
- where: { id, ...bindingEnt },
2086
- select: ["id"]
2108
+ return this.repo.exists({
2109
+ where: { id, ...bindingEnt, ...cond }
2087
2110
  });
2088
- return !!ent;
2111
+ }
2112
+ async operation(id, cb, extraOptions = {}) {
2113
+ const bindingEnt = await this.getBindingPartialEntity();
2114
+ const where = {
2115
+ id,
2116
+ ...bindingEnt,
2117
+ ...extraOptions.where || {}
2118
+ };
2119
+ const throw404 = () => {
2120
+ throw new import_nesties8.BlankReturnMessageDto(
2121
+ 404,
2122
+ `${this.entityName} ID ${id} not found.`
2123
+ ).toException();
2124
+ };
2125
+ if (!await this.repo.exists({ where })) {
2126
+ throw404();
2127
+ }
2128
+ const res = await this.repo.manager.transaction(async (tdb) => {
2129
+ const repo = tdb.getRepository(this.entityClass);
2130
+ const ent = await repo.findOne({
2131
+ lock: { mode: "pessimistic_write" },
2132
+ ...extraOptions,
2133
+ where
2134
+ });
2135
+ if (!ent) {
2136
+ throw404();
2137
+ }
2138
+ const initial = { ...ent };
2139
+ let changes = {};
2140
+ const entProxy = observeDiff(ent, (change) => {
2141
+ if (change.type === "delete") {
2142
+ if (initial[change.key] === null) {
2143
+ delete changes[change.key];
2144
+ } else {
2145
+ changes[change.key] = null;
2146
+ }
2147
+ } else {
2148
+ if (change.newValue !== initial[change.key]) {
2149
+ changes[change.key] = change.newValue;
2150
+ } else {
2151
+ delete changes[change.key];
2152
+ }
2153
+ }
2154
+ });
2155
+ const flush = async () => {
2156
+ if (Object.keys(changes).length) {
2157
+ const currentChanges = { ...changes };
2158
+ Object.assign(initial, changes);
2159
+ changes = {};
2160
+ await repo.update({ id }, currentChanges);
2161
+ }
2162
+ };
2163
+ const result = await cb(entProxy, { repo, flush });
2164
+ await flush();
2165
+ return result;
2166
+ });
2167
+ if (res == null) {
2168
+ return new import_nesties8.BlankReturnMessageDto(200, "success");
2169
+ } else {
2170
+ return new import_nesties8.GenericReturnMessageDto(200, "success", res);
2171
+ }
2089
2172
  }
2090
2173
  async _loadFullTextIndex() {
2091
2174
  const fields = reflector.getArray(
@@ -2664,10 +2747,7 @@ var _RestfulFactory = class _RestfulFactory {
2664
2747
  }),
2665
2748
  (0, import_swagger6.ApiBody)({ type: this.createDto }),
2666
2749
  (0, import_swagger6.ApiOkResponse)({ type: this.entityCreateReturnMessageDto }),
2667
- (0, import_swagger6.ApiBadRequestResponse)({
2668
- type: import_nesties10.BlankReturnMessageDto,
2669
- description: `The ${this.entityClassName} is not valid`
2670
- })
2750
+ (0, import_nesties10.ApiError)(400, `The ${this.entityClassName} is not valid`)
2671
2751
  ]);
2672
2752
  }
2673
2753
  createParam() {
@@ -2682,10 +2762,10 @@ var _RestfulFactory = class _RestfulFactory {
2682
2762
  }),
2683
2763
  (0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
2684
2764
  (0, import_swagger6.ApiOkResponse)({ type: this.entityReturnMessageDto }),
2685
- (0, import_swagger6.ApiNotFoundResponse)({
2686
- type: import_nesties10.BlankReturnMessageDto,
2687
- description: `The ${this.entityClassName} with the given id was not found`
2688
- })
2765
+ (0, import_nesties10.ApiError)(
2766
+ 400,
2767
+ `The ${this.entityClassName} with the given id was not found`
2768
+ )
2689
2769
  ]);
2690
2770
  }
2691
2771
  idParam() {
@@ -2741,19 +2821,13 @@ var _RestfulFactory = class _RestfulFactory {
2741
2821
  }),
2742
2822
  (0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
2743
2823
  (0, import_swagger6.ApiBody)({ type: this.updateDto }),
2744
- (0, import_swagger6.ApiOkResponse)({ type: import_nesties10.BlankReturnMessageDto }),
2745
- (0, import_swagger6.ApiNotFoundResponse)({
2746
- type: import_nesties10.BlankReturnMessageDto,
2747
- description: `The ${this.entityClassName} with the given id was not found`
2748
- }),
2749
- (0, import_swagger6.ApiBadRequestResponse)({
2750
- type: import_nesties10.BlankReturnMessageDto,
2751
- description: `The ${this.entityClassName} is not valid`
2752
- }),
2753
- (0, import_swagger6.ApiInternalServerErrorResponse)({
2754
- type: import_nesties10.BlankReturnMessageDto,
2755
- description: "Internal error"
2756
- })
2824
+ (0, import_nesties10.ApiBlankResponse)(),
2825
+ (0, import_nesties10.ApiError)(
2826
+ 404,
2827
+ `The ${this.entityClassName} with the given id was not found`
2828
+ ),
2829
+ (0, import_nesties10.ApiError)(400, `The ${this.entityClassName} is not valid`),
2830
+ (0, import_nesties10.ApiError)(500, "Internal error")
2757
2831
  ]);
2758
2832
  }
2759
2833
  updateParam() {
@@ -2768,20 +2842,17 @@ var _RestfulFactory = class _RestfulFactory {
2768
2842
  ...extras
2769
2843
  }),
2770
2844
  (0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
2771
- (0, import_swagger6.ApiOkResponse)({ type: import_nesties10.BlankReturnMessageDto }),
2772
- (0, import_swagger6.ApiNotFoundResponse)({
2773
- type: import_nesties10.BlankReturnMessageDto,
2774
- description: `The ${this.entityClassName} with the given id was not found`
2775
- }),
2776
- (0, import_swagger6.ApiInternalServerErrorResponse)({
2777
- type: import_nesties10.BlankReturnMessageDto,
2778
- description: "Internal error"
2779
- })
2845
+ (0, import_nesties10.ApiBlankResponse)(),
2846
+ (0, import_nesties10.ApiError)(
2847
+ 404,
2848
+ `The ${this.entityClassName} with the given id was not found`
2849
+ ),
2850
+ (0, import_nesties10.ApiError)(500, "Internal error")
2780
2851
  ]);
2781
2852
  }
2782
2853
  import(extras = {}) {
2783
2854
  return (0, import_nesties10.MergeMethodDecorators)([
2784
- (0, import_common3.Post)("import"),
2855
+ this.usePrefix(import_common3.Post, "import"),
2785
2856
  (0, import_common3.HttpCode)(200),
2786
2857
  (0, import_swagger6.ApiOperation)({
2787
2858
  summary: `Import ${this.entityClassName}`,
@@ -2789,10 +2860,22 @@ var _RestfulFactory = class _RestfulFactory {
2789
2860
  }),
2790
2861
  (0, import_swagger6.ApiBody)({ type: this.importDto }),
2791
2862
  (0, import_swagger6.ApiOkResponse)({ type: this.importReturnMessageDto }),
2792
- (0, import_swagger6.ApiInternalServerErrorResponse)({
2793
- type: import_nesties10.BlankReturnMessageDto,
2794
- description: "Internal error"
2795
- })
2863
+ (0, import_nesties10.ApiError)(500, "Internal error")
2864
+ ]);
2865
+ }
2866
+ operation(operationName, options = {}) {
2867
+ return (0, import_nesties10.MergeMethodDecorators)([
2868
+ this.usePrefix(import_common3.Post, `:id/${operationName}`),
2869
+ (0, import_common3.HttpCode)(200),
2870
+ (0, import_swagger6.ApiOperation)({
2871
+ summary: `${(0, import_lodash5.upperFirst)(operationName)} a ${this.entityClassName} by id`,
2872
+ ...options.operationExtras || {}
2873
+ }),
2874
+ options.returnType ? (0, import_nesties10.ApiTypeResponse)(options.returnType) : (0, import_nesties10.ApiBlankResponse)(),
2875
+ (0, import_nesties10.ApiError)(
2876
+ 404,
2877
+ `The ${this.entityClassName} with the given id was not found`
2878
+ )
2796
2879
  ]);
2797
2880
  }
2798
2881
  baseController(routeOptions = {}) {