nicot 1.2.5 → 1.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1473,6 +1473,7 @@ 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
+ var import_nfkit = require("nfkit");
1476
1477
  var Relation = (name, options = {}) => {
1477
1478
  return { name, inner: false, ...options };
1478
1479
  };
@@ -2079,13 +2080,72 @@ var CrudBase = class {
2079
2080
  })
2080
2081
  );
2081
2082
  }
2082
- async exists(id) {
2083
+ async exists(id, cond = {}) {
2083
2084
  const bindingEnt = await this.getBindingPartialEntity();
2084
- const ent = await this.repo.findOne({
2085
- where: { id, ...bindingEnt },
2086
- select: ["id"]
2085
+ return this.repo.exists({
2086
+ where: { id, ...bindingEnt, ...cond }
2087
2087
  });
2088
- return !!ent;
2088
+ }
2089
+ async operation(id, cb, extraOptions = {}) {
2090
+ const bindingEnt = await this.getBindingPartialEntity();
2091
+ const where = {
2092
+ id,
2093
+ ...bindingEnt,
2094
+ ...extraOptions.where || {}
2095
+ };
2096
+ const throw404 = () => {
2097
+ throw new import_nesties8.BlankReturnMessageDto(
2098
+ 404,
2099
+ `${this.entityName} ID ${id} not found.`
2100
+ ).toException();
2101
+ };
2102
+ if (!await this.repo.exists({ where })) {
2103
+ throw404();
2104
+ }
2105
+ const res = await this.repo.manager.transaction(async (tdb) => {
2106
+ const repo = tdb.getRepository(this.entityClass);
2107
+ const ent = await repo.findOne({
2108
+ lock: { mode: "pessimistic_write" },
2109
+ ...extraOptions,
2110
+ where
2111
+ });
2112
+ if (!ent) {
2113
+ throw404();
2114
+ }
2115
+ const initial = { ...ent };
2116
+ let changes = {};
2117
+ const entProxy = (0, import_nfkit.observeDiff)(ent, (change) => {
2118
+ if (change.type === "delete") {
2119
+ if (initial[change.key] === null) {
2120
+ delete changes[change.key];
2121
+ } else {
2122
+ changes[change.key] = null;
2123
+ }
2124
+ } else {
2125
+ if (change.newValue !== initial[change.key]) {
2126
+ changes[change.key] = change.newValue;
2127
+ } else {
2128
+ delete changes[change.key];
2129
+ }
2130
+ }
2131
+ });
2132
+ const flush = async () => {
2133
+ if (Object.keys(changes).length) {
2134
+ const currentChanges = { ...changes };
2135
+ Object.assign(initial, changes);
2136
+ changes = {};
2137
+ await repo.update({ id }, currentChanges);
2138
+ }
2139
+ };
2140
+ const result = await cb(entProxy, { repo, flush });
2141
+ await flush();
2142
+ return result;
2143
+ });
2144
+ if (res == null) {
2145
+ return new import_nesties8.BlankReturnMessageDto(200, "success");
2146
+ } else {
2147
+ return new import_nesties8.GenericReturnMessageDto(200, "success", res);
2148
+ }
2089
2149
  }
2090
2150
  async _loadFullTextIndex() {
2091
2151
  const fields = reflector.getArray(
@@ -2664,10 +2724,7 @@ var _RestfulFactory = class _RestfulFactory {
2664
2724
  }),
2665
2725
  (0, import_swagger6.ApiBody)({ type: this.createDto }),
2666
2726
  (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
- })
2727
+ (0, import_nesties10.ApiError)(400, `The ${this.entityClassName} is not valid`)
2671
2728
  ]);
2672
2729
  }
2673
2730
  createParam() {
@@ -2682,10 +2739,10 @@ var _RestfulFactory = class _RestfulFactory {
2682
2739
  }),
2683
2740
  (0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
2684
2741
  (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
- })
2742
+ (0, import_nesties10.ApiError)(
2743
+ 400,
2744
+ `The ${this.entityClassName} with the given id was not found`
2745
+ )
2689
2746
  ]);
2690
2747
  }
2691
2748
  idParam() {
@@ -2741,19 +2798,13 @@ var _RestfulFactory = class _RestfulFactory {
2741
2798
  }),
2742
2799
  (0, import_swagger6.ApiParam)({ name: "id", type: this.idType, required: true }),
2743
2800
  (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
- })
2801
+ (0, import_nesties10.ApiBlankResponse)(),
2802
+ (0, import_nesties10.ApiError)(
2803
+ 404,
2804
+ `The ${this.entityClassName} with the given id was not found`
2805
+ ),
2806
+ (0, import_nesties10.ApiError)(400, `The ${this.entityClassName} is not valid`),
2807
+ (0, import_nesties10.ApiError)(500, "Internal error")
2757
2808
  ]);
2758
2809
  }
2759
2810
  updateParam() {
@@ -2768,20 +2819,17 @@ var _RestfulFactory = class _RestfulFactory {
2768
2819
  ...extras
2769
2820
  }),
2770
2821
  (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
- })
2822
+ (0, import_nesties10.ApiBlankResponse)(),
2823
+ (0, import_nesties10.ApiError)(
2824
+ 404,
2825
+ `The ${this.entityClassName} with the given id was not found`
2826
+ ),
2827
+ (0, import_nesties10.ApiError)(500, "Internal error")
2780
2828
  ]);
2781
2829
  }
2782
2830
  import(extras = {}) {
2783
2831
  return (0, import_nesties10.MergeMethodDecorators)([
2784
- (0, import_common3.Post)("import"),
2832
+ this.usePrefix(import_common3.Post, "import"),
2785
2833
  (0, import_common3.HttpCode)(200),
2786
2834
  (0, import_swagger6.ApiOperation)({
2787
2835
  summary: `Import ${this.entityClassName}`,
@@ -2789,10 +2837,22 @@ var _RestfulFactory = class _RestfulFactory {
2789
2837
  }),
2790
2838
  (0, import_swagger6.ApiBody)({ type: this.importDto }),
2791
2839
  (0, import_swagger6.ApiOkResponse)({ type: this.importReturnMessageDto }),
2792
- (0, import_swagger6.ApiInternalServerErrorResponse)({
2793
- type: import_nesties10.BlankReturnMessageDto,
2794
- description: "Internal error"
2795
- })
2840
+ (0, import_nesties10.ApiError)(500, "Internal error")
2841
+ ]);
2842
+ }
2843
+ operation(operationName, options = {}) {
2844
+ return (0, import_nesties10.MergeMethodDecorators)([
2845
+ this.usePrefix(import_common3.Post, `:id/${operationName}`),
2846
+ (0, import_common3.HttpCode)(200),
2847
+ (0, import_swagger6.ApiOperation)({
2848
+ summary: `${(0, import_lodash5.upperFirst)(operationName)} a ${this.entityClassName} by id`,
2849
+ ...options.operationExtras || {}
2850
+ }),
2851
+ options.returnType ? (0, import_nesties10.ApiTypeResponse)(options.returnType) : (0, import_nesties10.ApiBlankResponse)(),
2852
+ (0, import_nesties10.ApiError)(
2853
+ 404,
2854
+ `The ${this.entityClassName} with the given id was not found`
2855
+ )
2796
2856
  ]);
2797
2857
  }
2798
2858
  baseController(routeOptions = {}) {