nicot 1.2.10 → 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.mjs CHANGED
@@ -1397,7 +1397,6 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
1397
1397
 
1398
1398
  // src/crud-base.ts
1399
1399
  import PQueue from "p-queue";
1400
- import { observeDiff } from "nfkit";
1401
1400
  var Relation = (name, options = {}) => {
1402
1401
  return { name, inner: false, ...options };
1403
1402
  };
@@ -2026,52 +2025,119 @@ var CrudBase = class {
2026
2025
  if (!await this.repo.exists({ where })) {
2027
2026
  throw404();
2028
2027
  }
2028
+ const isPlainObject = (v) => {
2029
+ if (!v || typeof v !== "object") return false;
2030
+ const proto = Object.getPrototypeOf(v);
2031
+ return proto === Object.prototype || proto === null;
2032
+ };
2033
+ const cloneAtomicOrJson = (v) => {
2034
+ if (v == null) return v;
2035
+ if (typeof v !== "object") return v;
2036
+ if (v instanceof Date) return new Date(v.getTime());
2037
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(v)) {
2038
+ return Buffer.from(v);
2039
+ }
2040
+ if (ArrayBuffer.isView(v)) {
2041
+ const ctor = v.constructor;
2042
+ return new ctor(v.slice?.() ?? v);
2043
+ }
2044
+ if (v instanceof ArrayBuffer) return v.slice(0);
2045
+ if (Array.isArray(v) || isPlainObject(v)) {
2046
+ const sc = globalThis.structuredClone;
2047
+ if (typeof sc === "function") return sc(v);
2048
+ return JSON.parse(JSON.stringify(v));
2049
+ }
2050
+ return v;
2051
+ };
2052
+ const deepEqual = (a, b) => {
2053
+ if (a === b) return true;
2054
+ if (a == null || b == null) return a === b;
2055
+ if (a instanceof Date && b instanceof Date)
2056
+ return a.getTime() === b.getTime();
2057
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(a) && Buffer.isBuffer(b)) {
2058
+ return a.equals(b);
2059
+ }
2060
+ if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
2061
+ if (a.byteLength !== b.byteLength) return false;
2062
+ const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
2063
+ const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
2064
+ for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
2065
+ return true;
2066
+ }
2067
+ if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
2068
+ if (a.byteLength !== b.byteLength) return false;
2069
+ const ua = new Uint8Array(a);
2070
+ const ub = new Uint8Array(b);
2071
+ for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
2072
+ return true;
2073
+ }
2074
+ if (Array.isArray(a) || Array.isArray(b)) {
2075
+ if (!Array.isArray(a) || !Array.isArray(b)) return false;
2076
+ if (a.length !== b.length) return false;
2077
+ for (let i = 0; i < a.length; i++)
2078
+ if (!deepEqual(a[i], b[i])) return false;
2079
+ return true;
2080
+ }
2081
+ if (typeof a === "object" && typeof b === "object") {
2082
+ const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
2083
+ for (const k of keys) {
2084
+ if (!deepEqual(a[k], b[k])) return false;
2085
+ }
2086
+ return true;
2087
+ }
2088
+ return false;
2089
+ };
2029
2090
  const op = async (repo) => {
2030
2091
  const ent = await repo.findOne({
2031
2092
  lock: { mode: "pessimistic_write", tables: [repo.metadata.tableName] },
2032
2093
  ...options.find || {},
2033
2094
  where
2034
2095
  });
2035
- if (!ent) {
2036
- throw404();
2096
+ if (!ent) throw404();
2097
+ const columns = repo.metadata.columns.map(
2098
+ (c) => c.propertyName
2099
+ );
2100
+ const snapshot = {};
2101
+ const snapshotHasKey = {};
2102
+ for (const key of columns) {
2103
+ snapshotHasKey[key] = Object.prototype.hasOwnProperty.call(
2104
+ ent,
2105
+ key
2106
+ );
2107
+ snapshot[key] = cloneAtomicOrJson(ent[key]);
2037
2108
  }
2038
- const initial = { ...ent };
2039
- let changes = {};
2040
- const entProxy = observeDiff(ent, (change) => {
2041
- if (change.type === "delete") {
2042
- if (initial[change.key] === null) {
2043
- delete changes[change.key];
2044
- } else {
2045
- changes[change.key] = null;
2109
+ const flush = async () => {
2110
+ const patch = {};
2111
+ for (const key of columns) {
2112
+ const hasNow = Object.prototype.hasOwnProperty.call(ent, key);
2113
+ if (!hasNow) {
2114
+ if (snapshotHasKey[key]) {
2115
+ patch[key] = null;
2116
+ snapshotHasKey[key] = true;
2117
+ snapshot[key] = null;
2118
+ }
2119
+ continue;
2046
2120
  }
2047
- } else {
2048
- if (change.newValue !== initial[change.key]) {
2049
- changes[change.key] = change.newValue;
2050
- } else {
2051
- delete changes[change.key];
2121
+ const current = ent[key];
2122
+ const before = snapshot[key];
2123
+ if (!deepEqual(before, current)) {
2124
+ patch[key] = current;
2125
+ snapshotHasKey[key] = true;
2126
+ snapshot[key] = cloneAtomicOrJson(current);
2052
2127
  }
2053
2128
  }
2054
- });
2055
- const flush = async () => {
2056
- if (Object.keys(changes).length) {
2057
- const currentChanges = { ...changes };
2058
- Object.assign(initial, changes);
2059
- changes = {};
2060
- await repo.update({ id }, currentChanges);
2129
+ if (Object.keys(patch).length) {
2130
+ await repo.update({ id }, patch);
2061
2131
  }
2062
2132
  };
2063
- const result = await cb(entProxy, { repo, flush });
2133
+ const result = await cb(ent, { repo, flush });
2064
2134
  await flush();
2065
2135
  return result;
2066
2136
  };
2067
2137
  const res = await (options.repo ? op(options.repo) : this.repo.manager.transaction(
2068
2138
  (tdb) => op(tdb.getRepository(this.entityClass))
2069
2139
  ));
2070
- if (res == null) {
2071
- return new BlankReturnMessageDto2(200, "success");
2072
- } else {
2073
- return new GenericReturnMessageDto(200, "success", res);
2074
- }
2140
+ return res == null ? new BlankReturnMessageDto2(200, "success") : new GenericReturnMessageDto(200, "success", res);
2075
2141
  }
2076
2142
  async _loadFullTextIndex() {
2077
2143
  const fields = reflector.getArray(