@guren/orm 2.15.0 → 2.15.1
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.d.ts +14 -1
- package/dist/index.js +171 -16
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -84,6 +84,8 @@ declare const SEAL_SCOPES: unique symbol;
|
|
|
84
84
|
declare const READ_TRANSFORMS: unique symbol;
|
|
85
85
|
/** Rows as the adapter read them, for the joins a relation loader makes. */
|
|
86
86
|
declare const RAW_RESULTS: unique symbol;
|
|
87
|
+
/** A model's own relation definitions, for the serializer, which cannot import `Model`. */
|
|
88
|
+
declare const RELATION_DEFINITIONS: unique symbol;
|
|
87
89
|
/** Explicit physical deletion, used by SoftDeletes.forceDelete. */
|
|
88
90
|
declare const PHYSICAL_DELETE: unique symbol;
|
|
89
91
|
/** A static delete run inside the model's lifecycle, shared with SoftDeletes. */
|
|
@@ -629,9 +631,14 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
629
631
|
* that is refused; the builder repeats the check for its own callers.
|
|
630
632
|
*/
|
|
631
633
|
private static assertFiltersSurvived;
|
|
632
|
-
/**
|
|
634
|
+
/**
|
|
635
|
+
* Applies hidden/visible filtering, accessors and appends, then each loaded
|
|
636
|
+
* relation's own model's hidden/visible to its rows, nested ones included.
|
|
637
|
+
*/
|
|
633
638
|
static serialize<T extends typeof Model>(this: T, record: TRecordFor<T>): PlainObject;
|
|
634
639
|
static serializeMany<T extends typeof Model>(this: T, records: TRecordFor<T>[]): PlainObject[];
|
|
640
|
+
/** @internal The serializer's view of the relations, symbol-keyed like `[READ_TRANSFORMS]`. */
|
|
641
|
+
static [RELATION_DEFINITIONS](): ReadonlyMap<string, RelationDefinition>;
|
|
635
642
|
static applyCasts<T extends PlainObject>(record: T): T;
|
|
636
643
|
/** The one read-transform pass for a record, in order: casts then accessors. */
|
|
637
644
|
protected static applyReadTransforms<T extends PlainObject>(record: T): T;
|
|
@@ -667,6 +674,12 @@ declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
|
667
674
|
/** @throws ModelNotFoundException (404) when no record matches. */
|
|
668
675
|
static findOrFail<T extends typeof Model>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T>>;
|
|
669
676
|
static findWith<T extends typeof Model, K extends RelationPath<T>>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], relations: K | readonly K[], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<(TRecordFor<T> & RelationTypePick<T, K | readonly K[]>) | null>;
|
|
677
|
+
/**
|
|
678
|
+
* `find()` with its relations loaded ahead of the read transforms: a loader
|
|
679
|
+
* matches rows on raw key values, which a cast or an accessor on this model
|
|
680
|
+
* would have rewritten.
|
|
681
|
+
*/
|
|
682
|
+
private static findRawWith;
|
|
670
683
|
/**
|
|
671
684
|
* Relations accept dot-notation paths (`comments.author`).
|
|
672
685
|
* @throws ModelNotFoundException when no record matches.
|
package/dist/index.js
CHANGED
|
@@ -100,6 +100,8 @@ const SEAL_SCOPES = Symbol("guren.orm.sealScopes");
|
|
|
100
100
|
const READ_TRANSFORMS = Symbol("guren.orm.readTransforms");
|
|
101
101
|
/** Rows as the adapter read them, for the joins a relation loader makes. */
|
|
102
102
|
const RAW_RESULTS = Symbol("guren.orm.rawResults");
|
|
103
|
+
/** A model's own relation definitions, for the serializer, which cannot import `Model`. */
|
|
104
|
+
const RELATION_DEFINITIONS = Symbol("guren.orm.relationDefinitions");
|
|
103
105
|
/** Model-specific bulk deletion, preserving the builder's scopes. */
|
|
104
106
|
const BULK_DELETE = Symbol("guren.orm.bulkDelete");
|
|
105
107
|
/** Explicit physical deletion, used by SoftDeletes.forceDelete. */
|
|
@@ -247,9 +249,38 @@ async function loadRelatedRecords(related, keys, clause, queryOptions, constrain
|
|
|
247
249
|
async function countByChunks(values, size, count) {
|
|
248
250
|
return new Map(await loadByChunks(values, size, async (chunk) => Array.from(await count(chunk))));
|
|
249
251
|
}
|
|
252
|
+
const resolvedReferences = /* @__PURE__ */ new WeakMap();
|
|
253
|
+
function isModelClass(reference) {
|
|
254
|
+
return typeof reference.newQuery === "function";
|
|
255
|
+
}
|
|
250
256
|
async function resolveModelReference(reference) {
|
|
251
|
-
if (
|
|
252
|
-
|
|
257
|
+
if (isModelClass(reference)) return reference;
|
|
258
|
+
const model = await reference();
|
|
259
|
+
resolvedReferences.set(reference, model);
|
|
260
|
+
return model;
|
|
261
|
+
}
|
|
262
|
+
const morphTargets = /* @__PURE__ */ new WeakMap();
|
|
263
|
+
function rememberMorphTarget(row, model) {
|
|
264
|
+
morphTargets.set(row, model);
|
|
265
|
+
}
|
|
266
|
+
function knownMorphTarget(row) {
|
|
267
|
+
return row !== null && typeof row === "object" ? morphTargets.get(row) : void 0;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The model a reference names. A thunk no load has resolved is called: rows can
|
|
271
|
+
* reach the serializer without a load in this process (a cache, a job payload).
|
|
272
|
+
* An asynchronous one yields `undefined` this time and is remembered once it settles.
|
|
273
|
+
*/
|
|
274
|
+
function knownModelReference(reference) {
|
|
275
|
+
if (isModelClass(reference)) return reference;
|
|
276
|
+
const known = resolvedReferences.get(reference);
|
|
277
|
+
if (known) return known;
|
|
278
|
+
const result = reference();
|
|
279
|
+
if (isModelClass(result)) {
|
|
280
|
+
resolvedReferences.set(reference, result);
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
result.then((model) => resolvedReferences.set(reference, model), () => void 0);
|
|
253
284
|
}
|
|
254
285
|
//#endregion
|
|
255
286
|
//#region src/relation-loading.ts
|
|
@@ -420,6 +451,7 @@ async function loadMorphTo(morphMap, records, definition, queryOptions, constrai
|
|
|
420
451
|
const idMap = /* @__PURE__ */ new Map();
|
|
421
452
|
for (const r of results) idMap.set(r.id, { ...r });
|
|
422
453
|
applyRelatedReadTransforms(modelClass, Array.from(idMap.values()), projected);
|
|
454
|
+
for (const row of idMap.values()) rememberMorphTarget(row, modelClass);
|
|
423
455
|
resolved.set(type, idMap);
|
|
424
456
|
}
|
|
425
457
|
for (const record of records) {
|
|
@@ -1787,6 +1819,112 @@ function serializeRecord(record, options) {
|
|
|
1787
1819
|
function serializeRecords(records, options) {
|
|
1788
1820
|
return records.map((r) => serializeRecord(r, options));
|
|
1789
1821
|
}
|
|
1822
|
+
/**
|
|
1823
|
+
* Filters the relation rows on `serialized` (a copy `serializeRecord` made of
|
|
1824
|
+
* `source`) by each related model's own `hidden`/`visible`, at every depth.
|
|
1825
|
+
* Only keys present on the rows are visited; untouched values keep their
|
|
1826
|
+
* references, and no input is written to.
|
|
1827
|
+
*/
|
|
1828
|
+
function serializeLoadedRelations(model, source, serialized, walk) {
|
|
1829
|
+
const definitions = model[RELATION_DEFINITIONS]();
|
|
1830
|
+
if (definitions.size === 0) return serialized;
|
|
1831
|
+
for (const key in serialized) {
|
|
1832
|
+
const definition = definitions.get(key);
|
|
1833
|
+
if (definition === void 0) continue;
|
|
1834
|
+
const value = serialized[key];
|
|
1835
|
+
const filtered = filterRelationValue(relatedModelOf(definition, source, value, walk.morphMap), value, walk);
|
|
1836
|
+
if (filtered !== value) serialized[key] = filtered;
|
|
1837
|
+
}
|
|
1838
|
+
return serialized;
|
|
1839
|
+
}
|
|
1840
|
+
/**
|
|
1841
|
+
* The related model; an asynchronous reference no load has resolved yields
|
|
1842
|
+
* `undefined`. A morphTo target is the model its load
|
|
1843
|
+
* resolved, since the holder's casts or accessors may rewrite the type column
|
|
1844
|
+
* afterwards; a row no load tagged falls back to that column.
|
|
1845
|
+
*/
|
|
1846
|
+
function relatedModelOf(definition, holder, value, morphMap) {
|
|
1847
|
+
if (definition.type !== "morphTo") return knownModelReference(definition.related);
|
|
1848
|
+
const loaded = knownMorphTarget(value);
|
|
1849
|
+
if (loaded) return loaded;
|
|
1850
|
+
const type = holder[`${definition.morphName}Type`];
|
|
1851
|
+
return typeof type === "string" && morphMap && Object.hasOwn(morphMap, type) ? morphMap[type] : void 0;
|
|
1852
|
+
}
|
|
1853
|
+
function hasColumnFilter(model) {
|
|
1854
|
+
return (model.visible?.length ?? 0) > 0 || (model.hidden?.length ?? 0) > 0;
|
|
1855
|
+
}
|
|
1856
|
+
function isRow(value) {
|
|
1857
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1858
|
+
}
|
|
1859
|
+
function holdsRelation(definitions, row) {
|
|
1860
|
+
for (const key in row) if (definitions.has(key)) return true;
|
|
1861
|
+
return false;
|
|
1862
|
+
}
|
|
1863
|
+
function filterRelationValue(model, value, walk) {
|
|
1864
|
+
if (!model || value === null || typeof value !== "object") return value;
|
|
1865
|
+
const definitions = model[RELATION_DEFINITIONS]();
|
|
1866
|
+
const filtered = hasColumnFilter(model);
|
|
1867
|
+
if (!filtered && definitions.size === 0) return value;
|
|
1868
|
+
if (!Array.isArray(value)) return isRow(value) ? filterRow(model, definitions, filtered, value, walk) : value;
|
|
1869
|
+
let copy;
|
|
1870
|
+
for (let i = 0; i < value.length; i++) {
|
|
1871
|
+
const item = value[i];
|
|
1872
|
+
const next = isRow(item) ? filterRow(model, definitions, filtered, item, walk) : item;
|
|
1873
|
+
if (copy) copy.push(next);
|
|
1874
|
+
else if (next !== item) {
|
|
1875
|
+
copy = value.slice(0, i);
|
|
1876
|
+
copy.push(next);
|
|
1877
|
+
}
|
|
1878
|
+
}
|
|
1879
|
+
return copy ?? value;
|
|
1880
|
+
}
|
|
1881
|
+
function filterRow(model, definitions, filtered, row, walk) {
|
|
1882
|
+
if (!filtered && !holdsRelation(definitions, row)) return row;
|
|
1883
|
+
walk.memo ??= /* @__PURE__ */ new Map();
|
|
1884
|
+
let rows = walk.memo.get(model);
|
|
1885
|
+
if (!rows) {
|
|
1886
|
+
rows = /* @__PURE__ */ new Map();
|
|
1887
|
+
walk.memo.set(model, rows);
|
|
1888
|
+
}
|
|
1889
|
+
const seen = rows.get(row);
|
|
1890
|
+
if (seen) {
|
|
1891
|
+
if (!seen.done && seen.output === row) seen.output = { ...row };
|
|
1892
|
+
return seen.output;
|
|
1893
|
+
}
|
|
1894
|
+
const entry = {
|
|
1895
|
+
output: filtered ? filterColumns(model, row) : row,
|
|
1896
|
+
done: false
|
|
1897
|
+
};
|
|
1898
|
+
rows.set(row, entry);
|
|
1899
|
+
for (const key in entry.output) {
|
|
1900
|
+
const definition = definitions.get(key);
|
|
1901
|
+
if (definition === void 0) continue;
|
|
1902
|
+
const value = row[key];
|
|
1903
|
+
const next = filterRelationValue(relatedModelOf(definition, row, value, walk.morphMap), value, walk);
|
|
1904
|
+
if (next === value) continue;
|
|
1905
|
+
if (entry.output === row) entry.output = { ...row };
|
|
1906
|
+
entry.output[key] = next;
|
|
1907
|
+
}
|
|
1908
|
+
entry.done = true;
|
|
1909
|
+
return entry.output;
|
|
1910
|
+
}
|
|
1911
|
+
/**
|
|
1912
|
+
* A related row's own columns, by the rule `serializeRecord` applies to the
|
|
1913
|
+
* serialized record: `visible` keeps only the keys it names (relations
|
|
1914
|
+
* included), in loaded order; otherwise `hidden` keys go. Appends are not computed.
|
|
1915
|
+
*/
|
|
1916
|
+
function filterColumns(model, row) {
|
|
1917
|
+
const hidden = model.hidden;
|
|
1918
|
+
const visible = model.visible;
|
|
1919
|
+
const kept = {};
|
|
1920
|
+
if (visible && visible.length > 0) {
|
|
1921
|
+
for (const key in row) if (visible.includes(key)) kept[key] = row[key];
|
|
1922
|
+
return kept;
|
|
1923
|
+
}
|
|
1924
|
+
if (!hidden?.some((key) => key in row)) return row;
|
|
1925
|
+
for (const key in row) if (!hidden.includes(key)) kept[key] = row[key];
|
|
1926
|
+
return kept;
|
|
1927
|
+
}
|
|
1790
1928
|
//#endregion
|
|
1791
1929
|
//#region src/MassAssignmentException.ts
|
|
1792
1930
|
/**
|
|
@@ -1973,22 +2111,32 @@ var Model = class Model {
|
|
|
1973
2111
|
if (!everyFilterDropped(where)) return;
|
|
1974
2112
|
throw new Error(`${this.name}: refusing to ${operation} unfiltered — every value in the where clause was undefined.`);
|
|
1975
2113
|
}
|
|
1976
|
-
/**
|
|
2114
|
+
/**
|
|
2115
|
+
* Applies hidden/visible filtering, accessors and appends, then each loaded
|
|
2116
|
+
* relation's own model's hidden/visible to its rows, nested ones included.
|
|
2117
|
+
*/
|
|
1977
2118
|
static serialize(record) {
|
|
1978
|
-
|
|
2119
|
+
const serialized = serializeRecord(record, {
|
|
1979
2120
|
hidden: this.hidden,
|
|
1980
2121
|
visible: this.visible,
|
|
1981
2122
|
appends: this.appends,
|
|
1982
2123
|
accessors: this.accessors
|
|
1983
2124
|
});
|
|
2125
|
+
return serializeLoadedRelations(this, record, serialized, { morphMap: Model.morphMap });
|
|
1984
2126
|
}
|
|
1985
2127
|
static serializeMany(records) {
|
|
1986
|
-
|
|
2128
|
+
const options = {
|
|
1987
2129
|
hidden: this.hidden,
|
|
1988
2130
|
visible: this.visible,
|
|
1989
2131
|
appends: this.appends,
|
|
1990
2132
|
accessors: this.accessors
|
|
1991
|
-
}
|
|
2133
|
+
};
|
|
2134
|
+
const walk = { morphMap: Model.morphMap };
|
|
2135
|
+
return records.map((record) => serializeLoadedRelations(this, record, serializeRecord(record, options), walk));
|
|
2136
|
+
}
|
|
2137
|
+
/** @internal The serializer's view of the relations, symbol-keyed like `[READ_TRANSFORMS]`. */
|
|
2138
|
+
static [RELATION_DEFINITIONS]() {
|
|
2139
|
+
return this.getRelationDefinitions();
|
|
1992
2140
|
}
|
|
1993
2141
|
static applyCasts(record) {
|
|
1994
2142
|
const castDefs = this.casts;
|
|
@@ -2123,21 +2271,28 @@ var Model = class Model {
|
|
|
2123
2271
|
return record;
|
|
2124
2272
|
}
|
|
2125
2273
|
static async findWith(id, relations, key, queryOptions) {
|
|
2126
|
-
const record = await this.find(id, key, queryOptions);
|
|
2127
|
-
if (record == null) return null;
|
|
2128
2274
|
const relationList = normalizeRelations(relations);
|
|
2129
|
-
|
|
2275
|
+
return relationList.length === 0 ? await this.find(id, key, queryOptions) : await this.findRawWith(id, relationList, key, queryOptions);
|
|
2276
|
+
}
|
|
2277
|
+
/**
|
|
2278
|
+
* `find()` with its relations loaded ahead of the read transforms: a loader
|
|
2279
|
+
* matches rows on raw key values, which a cast or an accessor on this model
|
|
2280
|
+
* would have rewritten.
|
|
2281
|
+
*/
|
|
2282
|
+
static async findRawWith(id, relationList, key = "id", queryOptions) {
|
|
2283
|
+
if (id === void 0) return null;
|
|
2284
|
+
const where = { [key]: id };
|
|
2285
|
+
if (this.hasScopes()) return this.newQuery(queryOptions).where(where).with(...relationList).first();
|
|
2286
|
+
const record = await this.getAdapter().findUnique(this.resolveTable(), where, queryOptions);
|
|
2287
|
+
if (record == null) return null;
|
|
2130
2288
|
const copy = { ...record };
|
|
2131
2289
|
await this.loadRelationsInto([copy], relationList, queryOptions);
|
|
2132
|
-
return copy;
|
|
2290
|
+
return this.applyReadTransforms(copy);
|
|
2133
2291
|
}
|
|
2134
2292
|
static async findWithOrFail(id, relations, key, queryOptions) {
|
|
2135
|
-
const record = await this.
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
const copy = { ...record };
|
|
2139
|
-
await this.loadRelationsInto([copy], relationList, queryOptions);
|
|
2140
|
-
return copy;
|
|
2293
|
+
const record = await this.findWith(id, relations, key, queryOptions);
|
|
2294
|
+
if (record == null) throw new ModelNotFoundException(this.name, id, key ?? "id");
|
|
2295
|
+
return record;
|
|
2141
2296
|
}
|
|
2142
2297
|
static async first(where, queryOptions) {
|
|
2143
2298
|
if (everyFilterDropped(where)) return null;
|