nicot 1.2.10 → 1.2.12

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