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.mjs CHANGED
@@ -1045,6 +1045,7 @@ import { camelCase } from "typeorm/util/StringUtils";
1045
1045
  import _3, { omit } from "lodash";
1046
1046
  import {
1047
1047
  BlankReturnMessageDto as BlankReturnMessageDto2,
1048
+ GenericReturnMessageDto,
1048
1049
  PaginatedReturnMessageDto,
1049
1050
  ReturnMessageDto
1050
1051
  } from "nesties";
@@ -1393,6 +1394,7 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
1393
1394
 
1394
1395
  // src/crud-base.ts
1395
1396
  import PQueue from "p-queue";
1397
+ import { observeDiff } from "nfkit";
1396
1398
  var Relation = (name, options = {}) => {
1397
1399
  return { name, inner: false, ...options };
1398
1400
  };
@@ -1999,13 +2001,72 @@ var CrudBase = class {
1999
2001
  })
2000
2002
  );
2001
2003
  }
2002
- async exists(id) {
2004
+ async exists(id, cond = {}) {
2003
2005
  const bindingEnt = await this.getBindingPartialEntity();
2004
- const ent = await this.repo.findOne({
2005
- where: { id, ...bindingEnt },
2006
- select: ["id"]
2006
+ return this.repo.exists({
2007
+ where: { id, ...bindingEnt, ...cond }
2007
2008
  });
2008
- return !!ent;
2009
+ }
2010
+ async operation(id, cb, extraOptions = {}) {
2011
+ const bindingEnt = await this.getBindingPartialEntity();
2012
+ const where = {
2013
+ id,
2014
+ ...bindingEnt,
2015
+ ...extraOptions.where || {}
2016
+ };
2017
+ const throw404 = () => {
2018
+ throw new BlankReturnMessageDto2(
2019
+ 404,
2020
+ `${this.entityName} ID ${id} not found.`
2021
+ ).toException();
2022
+ };
2023
+ if (!await this.repo.exists({ where })) {
2024
+ throw404();
2025
+ }
2026
+ const res = await this.repo.manager.transaction(async (tdb) => {
2027
+ const repo = tdb.getRepository(this.entityClass);
2028
+ const ent = await repo.findOne({
2029
+ lock: { mode: "pessimistic_write" },
2030
+ ...extraOptions,
2031
+ where
2032
+ });
2033
+ if (!ent) {
2034
+ throw404();
2035
+ }
2036
+ const initial = { ...ent };
2037
+ let changes = {};
2038
+ const entProxy = observeDiff(ent, (change) => {
2039
+ if (change.type === "delete") {
2040
+ if (initial[change.key] === null) {
2041
+ delete changes[change.key];
2042
+ } else {
2043
+ changes[change.key] = null;
2044
+ }
2045
+ } else {
2046
+ if (change.newValue !== initial[change.key]) {
2047
+ changes[change.key] = change.newValue;
2048
+ } else {
2049
+ delete changes[change.key];
2050
+ }
2051
+ }
2052
+ });
2053
+ const flush = async () => {
2054
+ if (Object.keys(changes).length) {
2055
+ const currentChanges = { ...changes };
2056
+ Object.assign(initial, changes);
2057
+ changes = {};
2058
+ await repo.update({ id }, currentChanges);
2059
+ }
2060
+ };
2061
+ const result = await cb(entProxy, { repo, flush });
2062
+ await flush();
2063
+ return result;
2064
+ });
2065
+ if (res == null) {
2066
+ return new BlankReturnMessageDto2(200, "success");
2067
+ } else {
2068
+ return new GenericReturnMessageDto(200, "success", res);
2069
+ }
2009
2070
  }
2010
2071
  async _loadFullTextIndex() {
2011
2072
  const fields = reflector.getArray(
@@ -2067,18 +2128,17 @@ import {
2067
2128
  Query
2068
2129
  } from "@nestjs/common";
2069
2130
  import {
2070
- BlankReturnMessageDto as BlankReturnMessageDto3,
2071
2131
  MergeMethodDecorators,
2072
2132
  PaginatedReturnMessageDto as PaginatedReturnMessageDto2,
2073
2133
  ReturnMessageDto as ReturnMessageDto2,
2074
2134
  getApiProperty as getApiProperty2,
2075
- DataPipe
2135
+ DataPipe,
2136
+ ApiTypeResponse,
2137
+ ApiBlankResponse,
2138
+ ApiError
2076
2139
  } from "nesties";
2077
2140
  import {
2078
- ApiBadRequestResponse,
2079
2141
  ApiBody,
2080
- ApiInternalServerErrorResponse,
2081
- ApiNotFoundResponse,
2082
2142
  ApiOkResponse,
2083
2143
  ApiOperation,
2084
2144
  ApiParam,
@@ -2613,10 +2673,7 @@ var _RestfulFactory = class _RestfulFactory {
2613
2673
  }),
2614
2674
  ApiBody({ type: this.createDto }),
2615
2675
  ApiOkResponse({ type: this.entityCreateReturnMessageDto }),
2616
- ApiBadRequestResponse({
2617
- type: BlankReturnMessageDto3,
2618
- description: `The ${this.entityClassName} is not valid`
2619
- })
2676
+ ApiError(400, `The ${this.entityClassName} is not valid`)
2620
2677
  ]);
2621
2678
  }
2622
2679
  createParam() {
@@ -2631,10 +2688,10 @@ var _RestfulFactory = class _RestfulFactory {
2631
2688
  }),
2632
2689
  ApiParam({ name: "id", type: this.idType, required: true }),
2633
2690
  ApiOkResponse({ type: this.entityReturnMessageDto }),
2634
- ApiNotFoundResponse({
2635
- type: BlankReturnMessageDto3,
2636
- description: `The ${this.entityClassName} with the given id was not found`
2637
- })
2691
+ ApiError(
2692
+ 400,
2693
+ `The ${this.entityClassName} with the given id was not found`
2694
+ )
2638
2695
  ]);
2639
2696
  }
2640
2697
  idParam() {
@@ -2690,19 +2747,13 @@ var _RestfulFactory = class _RestfulFactory {
2690
2747
  }),
2691
2748
  ApiParam({ name: "id", type: this.idType, required: true }),
2692
2749
  ApiBody({ type: this.updateDto }),
2693
- ApiOkResponse({ type: BlankReturnMessageDto3 }),
2694
- ApiNotFoundResponse({
2695
- type: BlankReturnMessageDto3,
2696
- description: `The ${this.entityClassName} with the given id was not found`
2697
- }),
2698
- ApiBadRequestResponse({
2699
- type: BlankReturnMessageDto3,
2700
- description: `The ${this.entityClassName} is not valid`
2701
- }),
2702
- ApiInternalServerErrorResponse({
2703
- type: BlankReturnMessageDto3,
2704
- description: "Internal error"
2705
- })
2750
+ ApiBlankResponse(),
2751
+ ApiError(
2752
+ 404,
2753
+ `The ${this.entityClassName} with the given id was not found`
2754
+ ),
2755
+ ApiError(400, `The ${this.entityClassName} is not valid`),
2756
+ ApiError(500, "Internal error")
2706
2757
  ]);
2707
2758
  }
2708
2759
  updateParam() {
@@ -2717,20 +2768,17 @@ var _RestfulFactory = class _RestfulFactory {
2717
2768
  ...extras
2718
2769
  }),
2719
2770
  ApiParam({ name: "id", type: this.idType, required: true }),
2720
- ApiOkResponse({ type: BlankReturnMessageDto3 }),
2721
- ApiNotFoundResponse({
2722
- type: BlankReturnMessageDto3,
2723
- description: `The ${this.entityClassName} with the given id was not found`
2724
- }),
2725
- ApiInternalServerErrorResponse({
2726
- type: BlankReturnMessageDto3,
2727
- description: "Internal error"
2728
- })
2771
+ ApiBlankResponse(),
2772
+ ApiError(
2773
+ 404,
2774
+ `The ${this.entityClassName} with the given id was not found`
2775
+ ),
2776
+ ApiError(500, "Internal error")
2729
2777
  ]);
2730
2778
  }
2731
2779
  import(extras = {}) {
2732
2780
  return MergeMethodDecorators([
2733
- Post("import"),
2781
+ this.usePrefix(Post, "import"),
2734
2782
  HttpCode(200),
2735
2783
  ApiOperation({
2736
2784
  summary: `Import ${this.entityClassName}`,
@@ -2738,10 +2786,22 @@ var _RestfulFactory = class _RestfulFactory {
2738
2786
  }),
2739
2787
  ApiBody({ type: this.importDto }),
2740
2788
  ApiOkResponse({ type: this.importReturnMessageDto }),
2741
- ApiInternalServerErrorResponse({
2742
- type: BlankReturnMessageDto3,
2743
- description: "Internal error"
2744
- })
2789
+ ApiError(500, "Internal error")
2790
+ ]);
2791
+ }
2792
+ operation(operationName, options = {}) {
2793
+ return MergeMethodDecorators([
2794
+ this.usePrefix(Post, `:id/${operationName}`),
2795
+ HttpCode(200),
2796
+ ApiOperation({
2797
+ summary: `${upperFirst(operationName)} a ${this.entityClassName} by id`,
2798
+ ...options.operationExtras || {}
2799
+ }),
2800
+ options.returnType ? ApiTypeResponse(options.returnType) : ApiBlankResponse(),
2801
+ ApiError(
2802
+ 404,
2803
+ `The ${this.entityClassName} with the given id was not found`
2804
+ )
2745
2805
  ]);
2746
2806
  }
2747
2807
  baseController(routeOptions = {}) {