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 +115 -48
- package/dist/index.cjs.map +3 -3
- package/dist/index.mjs +96 -29
- package/dist/index.mjs.map +2 -2
- package/package.json +1 -1
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,120 @@ var CrudBase = class {
|
|
|
2026
2025
|
if (!await this.repo.exists({ where })) {
|
|
2027
2026
|
throw404();
|
|
2028
2027
|
}
|
|
2028
|
+
const isAtomicObject = (v) => {
|
|
2029
|
+
if (!v || typeof v !== "object") return true;
|
|
2030
|
+
if (v instanceof Date) return true;
|
|
2031
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(v)) return true;
|
|
2032
|
+
if (ArrayBuffer.isView(v) || v instanceof ArrayBuffer) return true;
|
|
2033
|
+
return false;
|
|
2034
|
+
};
|
|
2035
|
+
const cloneForSnapshot = (v) => {
|
|
2036
|
+
if (v == null) return v;
|
|
2037
|
+
if (typeof v !== "object") return v;
|
|
2038
|
+
if (v instanceof Date) return new Date(v.getTime());
|
|
2039
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(v))
|
|
2040
|
+
return Buffer.from(v);
|
|
2041
|
+
if (ArrayBuffer.isView(v)) return v.slice?.() ?? v;
|
|
2042
|
+
if (v instanceof ArrayBuffer) return v.slice(0);
|
|
2043
|
+
const sc = globalThis.structuredClone;
|
|
2044
|
+
if (typeof sc === "function") {
|
|
2045
|
+
try {
|
|
2046
|
+
return sc(v);
|
|
2047
|
+
} catch {
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
if (Array.isArray(v)) return v.map(cloneForSnapshot);
|
|
2051
|
+
const out = {};
|
|
2052
|
+
for (const k of Object.keys(v))
|
|
2053
|
+
out[k] = cloneForSnapshot(v[k]);
|
|
2054
|
+
return out;
|
|
2055
|
+
};
|
|
2056
|
+
const deepEqual = (a, b) => {
|
|
2057
|
+
if (a === b) return true;
|
|
2058
|
+
if (a == null || b == null) return a === b;
|
|
2059
|
+
if (a instanceof Date && b instanceof Date)
|
|
2060
|
+
return a.getTime() === b.getTime();
|
|
2061
|
+
if (typeof Buffer !== "undefined" && Buffer.isBuffer(a) && Buffer.isBuffer(b)) {
|
|
2062
|
+
return a.equals(b);
|
|
2063
|
+
}
|
|
2064
|
+
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
2065
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
2066
|
+
const ua = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
|
|
2067
|
+
const ub = new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
|
2068
|
+
for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
|
|
2069
|
+
return true;
|
|
2070
|
+
}
|
|
2071
|
+
if (a instanceof ArrayBuffer && b instanceof ArrayBuffer) {
|
|
2072
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
2073
|
+
const ua = new Uint8Array(a);
|
|
2074
|
+
const ub = new Uint8Array(b);
|
|
2075
|
+
for (let i = 0; i < ua.length; i++) if (ua[i] !== ub[i]) return false;
|
|
2076
|
+
return true;
|
|
2077
|
+
}
|
|
2078
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
2079
|
+
if (Array.isArray(a) || Array.isArray(b)) {
|
|
2080
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return false;
|
|
2081
|
+
if (a.length !== b.length) return false;
|
|
2082
|
+
for (let i = 0; i < a.length; i++)
|
|
2083
|
+
if (!deepEqual(a[i], b[i])) return false;
|
|
2084
|
+
return true;
|
|
2085
|
+
}
|
|
2086
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
2087
|
+
for (const k of keys) {
|
|
2088
|
+
if (!deepEqual(a[k], b[k])) return false;
|
|
2089
|
+
}
|
|
2090
|
+
return true;
|
|
2091
|
+
}
|
|
2092
|
+
return false;
|
|
2093
|
+
};
|
|
2029
2094
|
const op = async (repo) => {
|
|
2030
2095
|
const ent = await repo.findOne({
|
|
2031
2096
|
lock: { mode: "pessimistic_write", tables: [repo.metadata.tableName] },
|
|
2032
2097
|
...options.find || {},
|
|
2033
2098
|
where
|
|
2034
2099
|
});
|
|
2035
|
-
if (!ent)
|
|
2036
|
-
|
|
2100
|
+
if (!ent) throw404();
|
|
2101
|
+
const columns = repo.metadata.columns.map(
|
|
2102
|
+
(c) => c.propertyName
|
|
2103
|
+
);
|
|
2104
|
+
const snapshot = {};
|
|
2105
|
+
const snapshotHasKey = {};
|
|
2106
|
+
for (const key of columns) {
|
|
2107
|
+
snapshotHasKey[key] = Object.prototype.hasOwnProperty.call(ent, key);
|
|
2108
|
+
snapshot[key] = cloneForSnapshot(ent[key]);
|
|
2037
2109
|
}
|
|
2038
|
-
const
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
if (
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2110
|
+
const flush = async () => {
|
|
2111
|
+
const patch = {};
|
|
2112
|
+
for (const key of columns) {
|
|
2113
|
+
const hasNow = Object.prototype.hasOwnProperty.call(ent, key);
|
|
2114
|
+
if (!hasNow) {
|
|
2115
|
+
if (snapshotHasKey[key]) {
|
|
2116
|
+
patch[key] = null;
|
|
2117
|
+
snapshotHasKey[key] = true;
|
|
2118
|
+
snapshot[key] = null;
|
|
2119
|
+
}
|
|
2120
|
+
continue;
|
|
2046
2121
|
}
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2122
|
+
const current = ent[key];
|
|
2123
|
+
const before = snapshot[key];
|
|
2124
|
+
if (!deepEqual(before, current)) {
|
|
2125
|
+
patch[key] = current;
|
|
2126
|
+
snapshotHasKey[key] = true;
|
|
2127
|
+
snapshot[key] = cloneForSnapshot(current);
|
|
2052
2128
|
}
|
|
2053
2129
|
}
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
if (Object.keys(changes).length) {
|
|
2057
|
-
const currentChanges = { ...changes };
|
|
2058
|
-
Object.assign(initial, changes);
|
|
2059
|
-
changes = {};
|
|
2060
|
-
await repo.update({ id }, currentChanges);
|
|
2130
|
+
if (Object.keys(patch).length) {
|
|
2131
|
+
await repo.update({ id }, patch);
|
|
2061
2132
|
}
|
|
2062
2133
|
};
|
|
2063
|
-
const result = await cb(
|
|
2134
|
+
const result = await cb(ent, { repo, flush });
|
|
2064
2135
|
await flush();
|
|
2065
2136
|
return result;
|
|
2066
2137
|
};
|
|
2067
2138
|
const res = await (options.repo ? op(options.repo) : this.repo.manager.transaction(
|
|
2068
2139
|
(tdb) => op(tdb.getRepository(this.entityClass))
|
|
2069
2140
|
));
|
|
2070
|
-
|
|
2071
|
-
return new BlankReturnMessageDto2(200, "success");
|
|
2072
|
-
} else {
|
|
2073
|
-
return new GenericReturnMessageDto(200, "success", res);
|
|
2074
|
-
}
|
|
2141
|
+
return res == null ? new BlankReturnMessageDto2(200, "success") : new GenericReturnMessageDto(200, "success", res);
|
|
2075
2142
|
}
|
|
2076
2143
|
async _loadFullTextIndex() {
|
|
2077
2144
|
const fields = reflector.getArray(
|