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.mjs CHANGED
@@ -708,6 +708,7 @@ var createQueryArrayify = (newWrapper, singleFallbackWrapper) => createQueryWrap
708
708
  if (items.length === 1 && singleFallbackWrapper) {
709
709
  const singleRes = singleFallbackWrapper(entityExpr, varExpr, info);
710
710
  if (singleRes) {
711
+ info.mutateValue(items[0]);
711
712
  return singleRes;
712
713
  }
713
714
  }
@@ -1396,7 +1397,6 @@ async function getPaginatedResult(qb, entityClass, entityAliasName, take, cursor
1396
1397
 
1397
1398
  // src/crud-base.ts
1398
1399
  import PQueue from "p-queue";
1399
- import { observeDiff } from "nfkit";
1400
1400
  var Relation = (name, options = {}) => {
1401
1401
  return { name, inner: false, ...options };
1402
1402
  };
@@ -2025,52 +2025,119 @@ var CrudBase = class {
2025
2025
  if (!await this.repo.exists({ where })) {
2026
2026
  throw404();
2027
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
+ };
2028
2090
  const op = async (repo) => {
2029
2091
  const ent = await repo.findOne({
2030
2092
  lock: { mode: "pessimistic_write", tables: [repo.metadata.tableName] },
2031
2093
  ...options.find || {},
2032
2094
  where
2033
2095
  });
2034
- if (!ent) {
2035
- 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]);
2036
2108
  }
2037
- const initial = { ...ent };
2038
- let changes = {};
2039
- const entProxy = observeDiff(ent, (change) => {
2040
- if (change.type === "delete") {
2041
- if (initial[change.key] === null) {
2042
- delete changes[change.key];
2043
- } else {
2044
- 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;
2045
2120
  }
2046
- } else {
2047
- if (change.newValue !== initial[change.key]) {
2048
- changes[change.key] = change.newValue;
2049
- } else {
2050
- 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);
2051
2127
  }
2052
2128
  }
2053
- });
2054
- const flush = async () => {
2055
- if (Object.keys(changes).length) {
2056
- const currentChanges = { ...changes };
2057
- Object.assign(initial, changes);
2058
- changes = {};
2059
- await repo.update({ id }, currentChanges);
2129
+ if (Object.keys(patch).length) {
2130
+ await repo.update({ id }, patch);
2060
2131
  }
2061
2132
  };
2062
- const result = await cb(entProxy, { repo, flush });
2133
+ const result = await cb(ent, { repo, flush });
2063
2134
  await flush();
2064
2135
  return result;
2065
2136
  };
2066
2137
  const res = await (options.repo ? op(options.repo) : this.repo.manager.transaction(
2067
2138
  (tdb) => op(tdb.getRepository(this.entityClass))
2068
2139
  ));
2069
- if (res == null) {
2070
- return new BlankReturnMessageDto2(200, "success");
2071
- } else {
2072
- return new GenericReturnMessageDto(200, "success", res);
2073
- }
2140
+ return res == null ? new BlankReturnMessageDto2(200, "success") : new GenericReturnMessageDto(200, "success", res);
2074
2141
  }
2075
2142
  async _loadFullTextIndex() {
2076
2143
  const fields = reflector.getArray(