nicot 1.2.9 → 1.2.11

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
@@ -800,6 +800,7 @@ var createQueryArrayify = (newWrapper, singleFallbackWrapper) => createQueryWrap
800
800
  if (items.length === 1 && singleFallbackWrapper) {
801
801
  const singleRes = singleFallbackWrapper(entityExpr, varExpr, info);
802
802
  if (singleRes) {
803
+ info.mutateValue(items[0]);
803
804
  return singleRes;
804
805
  }
805
806
  }
@@ -1477,7 +1478,6 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
1477
1478
 
1478
1479
  // src/crud-base.ts
1479
1480
  var import_p_queue = __toESM(require("p-queue"));
1480
- var import_nfkit = require("nfkit");
1481
1481
  var Relation = (name, options = {}) => {
1482
1482
  return { name, inner: false, ...options };
1483
1483
  };
@@ -2106,52 +2106,119 @@ var CrudBase = class {
2106
2106
  if (!await this.repo.exists({ where })) {
2107
2107
  throw404();
2108
2108
  }
2109
+ const isPlainObject = (v) => {
2110
+ if (!v || typeof v !== "object") return false;
2111
+ const proto = Object.getPrototypeOf(v);
2112
+ return proto === Object.prototype || proto === null;
2113
+ };
2114
+ const cloneAtomicOrJson = (v) => {
2115
+ if (v == null) return v;
2116
+ if (typeof v !== "object") return v;
2117
+ if (v instanceof Date) return new Date(v.getTime());
2118
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(v)) {
2119
+ return Buffer.from(v);
2120
+ }
2121
+ if (ArrayBuffer.isView(v)) {
2122
+ const ctor = v.constructor;
2123
+ return new ctor(v.slice?.() ?? v);
2124
+ }
2125
+ if (v instanceof ArrayBuffer) return v.slice(0);
2126
+ if (Array.isArray(v) || isPlainObject(v)) {
2127
+ const sc = globalThis.structuredClone;
2128
+ if (typeof sc === "function") return sc(v);
2129
+ return JSON.parse(JSON.stringify(v));
2130
+ }
2131
+ return v;
2132
+ };
2133
+ const deepEqual = (a, b) => {
2134
+ if (a === b) return true;
2135
+ if (a == null || b == null) return a === b;
2136
+ if (a instanceof Date && b instanceof Date)
2137
+ return a.getTime() === b.getTime();
2138
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(a) && Buffer.isBuffer(b)) {
2139
+ return a.equals(b);
2140
+ }
2141
+ if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
2142
+ if (a.byteLength !== b.byteLength) return false;
2143
+ const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
2144
+ const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
2145
+ for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
2146
+ return true;
2147
+ }
2148
+ if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
2149
+ if (a.byteLength !== b.byteLength) return false;
2150
+ const ua = new Uint8Array(a);
2151
+ const ub = new Uint8Array(b);
2152
+ for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
2153
+ return true;
2154
+ }
2155
+ if (Array.isArray(a) || Array.isArray(b)) {
2156
+ if (!Array.isArray(a) || !Array.isArray(b)) return false;
2157
+ if (a.length !== b.length) return false;
2158
+ for (let i = 0; i < a.length; i++)
2159
+ if (!deepEqual(a[i], b[i])) return false;
2160
+ return true;
2161
+ }
2162
+ if (typeof a === "object" && typeof b === "object") {
2163
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
2164
+ for (const k of keys) {
2165
+ if (!deepEqual(a[k], b[k])) return false;
2166
+ }
2167
+ return true;
2168
+ }
2169
+ return false;
2170
+ };
2109
2171
  const op = async (repo) => {
2110
2172
  const ent = await repo.findOne({
2111
2173
  lock: { mode: "pessimistic_write", tables: [repo.metadata.tableName] },
2112
2174
  ...options.find || {},
2113
2175
  where
2114
2176
  });
2115
- if (!ent) {
2116
- throw404();
2177
+ if (!ent) throw404();
2178
+ const columns = repo.metadata.columns.map(
2179
+ (c) => c.propertyName
2180
+ );
2181
+ const snapshot = {};
2182
+ const snapshotHasKey = {};
2183
+ for (const key of columns) {
2184
+ snapshotHasKey[key] = Object.prototype.hasOwnProperty.call(
2185
+ ent,
2186
+ key
2187
+ );
2188
+ snapshot[key] = cloneAtomicOrJson(ent[key]);
2117
2189
  }
2118
- const initial = { ...ent };
2119
- let changes = {};
2120
- const entProxy = (0, import_nfkit.observeDiff)(ent, (change) => {
2121
- if (change.type === "delete") {
2122
- if (initial[change.key] === null) {
2123
- delete changes[change.key];
2124
- } else {
2125
- changes[change.key] = null;
2190
+ const flush = async () => {
2191
+ const patch = {};
2192
+ for (const key of columns) {
2193
+ const hasNow = Object.prototype.hasOwnProperty.call(ent, key);
2194
+ if (!hasNow) {
2195
+ if (snapshotHasKey[key]) {
2196
+ patch[key] = null;
2197
+ snapshotHasKey[key] = true;
2198
+ snapshot[key] = null;
2199
+ }
2200
+ continue;
2126
2201
  }
2127
- } else {
2128
- if (change.newValue !== initial[change.key]) {
2129
- changes[change.key] = change.newValue;
2130
- } else {
2131
- delete changes[change.key];
2202
+ const current = ent[key];
2203
+ const before = snapshot[key];
2204
+ if (!deepEqual(before, current)) {
2205
+ patch[key] = current;
2206
+ snapshotHasKey[key] = true;
2207
+ snapshot[key] = cloneAtomicOrJson(current);
2132
2208
  }
2133
2209
  }
2134
- });
2135
- const flush = async () => {
2136
- if (Object.keys(changes).length) {
2137
- const currentChanges = { ...changes };
2138
- Object.assign(initial, changes);
2139
- changes = {};
2140
- await repo.update({ id }, currentChanges);
2210
+ if (Object.keys(patch).length) {
2211
+ await repo.update({ id }, patch);
2141
2212
  }
2142
2213
  };
2143
- const result = await cb(entProxy, { repo, flush });
2214
+ const result = await cb(ent, { repo, flush });
2144
2215
  await flush();
2145
2216
  return result;
2146
2217
  };
2147
2218
  const res = await (options.repo ? op(options.repo) : this.repo.manager.transaction(
2148
2219
  (tdb) => op(tdb.getRepository(this.entityClass))
2149
2220
  ));
2150
- if (res == null) {
2151
- return new import_nesties8.BlankReturnMessageDto(200, "success");
2152
- } else {
2153
- return new import_nesties8.GenericReturnMessageDto(200, "success", res);
2154
- }
2221
+ return res == null ? new import_nesties8.BlankReturnMessageDto(200, "success") : new import_nesties8.GenericReturnMessageDto(200, "success", res);
2155
2222
  }
2156
2223
  async _loadFullTextIndex() {
2157
2224
  const fields = reflector.getArray(
@@ -2348,7 +2415,7 @@ var MutatorPipe = class {
2348
2415
  };
2349
2416
 
2350
2417
  // src/restful.ts
2351
- var import_nfkit2 = require("nfkit");
2418
+ var import_nfkit = require("nfkit");
2352
2419
  var getCurrentLevelRelations = (relations) => relations.filter((r) => !r.includes("."));
2353
2420
  var getNextLevelRelations = (relations, enteringField) => relations.filter((r) => r.includes(".") && r.startsWith(`${enteringField}.`)).map((r) => r.split(".").slice(1).join("."));
2354
2421
  var _RestfulFactory = class _RestfulFactory {
@@ -2965,58 +3032,58 @@ var _RestfulFactory = class _RestfulFactory {
2965
3032
  }
2966
3033
  };
2967
3034
  __decorateClass([
2968
- (0, import_nfkit2.Memorize)()
3035
+ (0, import_nfkit.Memorize)()
2969
3036
  ], _RestfulFactory.prototype, "fieldsToOmit", 1);
2970
3037
  __decorateClass([
2971
- (0, import_nfkit2.Memorize)()
3038
+ (0, import_nfkit.Memorize)()
2972
3039
  ], _RestfulFactory.prototype, "fieldsInCreateToOmit", 1);
2973
3040
  __decorateClass([
2974
- (0, import_nfkit2.Memorize)()
3041
+ (0, import_nfkit.Memorize)()
2975
3042
  ], _RestfulFactory.prototype, "createDto", 1);
2976
3043
  __decorateClass([
2977
- (0, import_nfkit2.Memorize)()
3044
+ (0, import_nfkit.Memorize)()
2978
3045
  ], _RestfulFactory.prototype, "fieldsInUpdateToOmit", 1);
2979
3046
  __decorateClass([
2980
- (0, import_nfkit2.Memorize)()
3047
+ (0, import_nfkit.Memorize)()
2981
3048
  ], _RestfulFactory.prototype, "updateDto", 1);
2982
3049
  __decorateClass([
2983
- (0, import_nfkit2.Memorize)()
3050
+ (0, import_nfkit.Memorize)()
2984
3051
  ], _RestfulFactory.prototype, "importDto", 1);
2985
3052
  __decorateClass([
2986
- (0, import_nfkit2.Memorize)()
3053
+ (0, import_nfkit.Memorize)()
2987
3054
  ], _RestfulFactory.prototype, "fieldsInGetToOmit", 1);
2988
3055
  __decorateClass([
2989
- (0, import_nfkit2.Memorize)()
3056
+ (0, import_nfkit.Memorize)()
2990
3057
  ], _RestfulFactory.prototype, "queryableFields", 1);
2991
3058
  __decorateClass([
2992
- (0, import_nfkit2.Memorize)()
3059
+ (0, import_nfkit.Memorize)()
2993
3060
  ], _RestfulFactory.prototype, "findAllDto", 1);
2994
3061
  __decorateClass([
2995
- (0, import_nfkit2.Memorize)()
3062
+ (0, import_nfkit.Memorize)()
2996
3063
  ], _RestfulFactory.prototype, "findAllCursorPaginatedDto", 1);
2997
3064
  __decorateClass([
2998
- (0, import_nfkit2.Memorize)()
3065
+ (0, import_nfkit.Memorize)()
2999
3066
  ], _RestfulFactory.prototype, "entityResultDto", 1);
3000
3067
  __decorateClass([
3001
- (0, import_nfkit2.Memorize)()
3068
+ (0, import_nfkit.Memorize)()
3002
3069
  ], _RestfulFactory.prototype, "entityCreateResultDto", 1);
3003
3070
  __decorateClass([
3004
- (0, import_nfkit2.Memorize)()
3071
+ (0, import_nfkit.Memorize)()
3005
3072
  ], _RestfulFactory.prototype, "entityReturnMessageDto", 1);
3006
3073
  __decorateClass([
3007
- (0, import_nfkit2.Memorize)()
3074
+ (0, import_nfkit.Memorize)()
3008
3075
  ], _RestfulFactory.prototype, "entityCreateReturnMessageDto", 1);
3009
3076
  __decorateClass([
3010
- (0, import_nfkit2.Memorize)()
3077
+ (0, import_nfkit.Memorize)()
3011
3078
  ], _RestfulFactory.prototype, "entityArrayReturnMessageDto", 1);
3012
3079
  __decorateClass([
3013
- (0, import_nfkit2.Memorize)()
3080
+ (0, import_nfkit.Memorize)()
3014
3081
  ], _RestfulFactory.prototype, "entityCursorPaginationReturnMessageDto", 1);
3015
3082
  __decorateClass([
3016
- (0, import_nfkit2.Memorize)()
3083
+ (0, import_nfkit.Memorize)()
3017
3084
  ], _RestfulFactory.prototype, "importReturnMessageDto", 1);
3018
3085
  __decorateClass([
3019
- (0, import_nfkit2.Memorize)()
3086
+ (0, import_nfkit.Memorize)()
3020
3087
  ], _RestfulFactory.prototype, "idType", 1);
3021
3088
  var RestfulFactory = _RestfulFactory;
3022
3089