@zenstackhq/orm 3.8.0 → 3.8.2
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 +50 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -1
- package/dist/index.d.mts +19 -3
- package/dist/index.mjs +238 -189
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -232,6 +232,7 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
|
232
232
|
getField: () => getField,
|
|
233
233
|
getIdFields: () => getIdFields,
|
|
234
234
|
getIdValues: () => getIdValues,
|
|
235
|
+
getManyToManyJoinTable: () => getManyToManyJoinTable,
|
|
235
236
|
getManyToManyRelation: () => getManyToManyRelation,
|
|
236
237
|
getModel: () => getModel,
|
|
237
238
|
getModelFields: () => getModelFields,
|
|
@@ -257,8 +258,25 @@ var query_utils_exports = /* @__PURE__ */ __exportAll({
|
|
|
257
258
|
function hasModel(schema, model) {
|
|
258
259
|
return Object.keys(schema.models).map((k) => k.toLowerCase()).includes(model.toLowerCase());
|
|
259
260
|
}
|
|
261
|
+
const schemaLookupCache = /* @__PURE__ */ new WeakMap();
|
|
262
|
+
function getSchemaLookupCache(schema) {
|
|
263
|
+
let cache = schemaLookupCache.get(schema);
|
|
264
|
+
if (!cache) {
|
|
265
|
+
cache = {
|
|
266
|
+
model: /* @__PURE__ */ new Map(),
|
|
267
|
+
m2mRelation: /* @__PURE__ */ new Map()
|
|
268
|
+
};
|
|
269
|
+
schemaLookupCache.set(schema, cache);
|
|
270
|
+
}
|
|
271
|
+
return cache;
|
|
272
|
+
}
|
|
260
273
|
function getModel(schema, model) {
|
|
261
|
-
|
|
274
|
+
const cache = getSchemaLookupCache(schema);
|
|
275
|
+
const key = model.toLowerCase();
|
|
276
|
+
if (cache.model.has(key)) return cache.model.get(key);
|
|
277
|
+
const result = Object.values(schema.models).find((m) => m.name.toLowerCase() === key);
|
|
278
|
+
cache.model.set(key, result);
|
|
279
|
+
return result;
|
|
262
280
|
}
|
|
263
281
|
function getTypeDef(schema, type) {
|
|
264
282
|
return schema.typeDefs?.[type];
|
|
@@ -399,6 +417,37 @@ function makeDefaultOrderBy(schema, model) {
|
|
|
399
417
|
return requireIdFields(schema, model).map((f) => ({ [f]: "asc" }));
|
|
400
418
|
}
|
|
401
419
|
function getManyToManyRelation(schema, model, field) {
|
|
420
|
+
const cache = getSchemaLookupCache(schema);
|
|
421
|
+
const key = `${model} ${field}`;
|
|
422
|
+
if (cache.m2mRelation.has(key)) return cache.m2mRelation.get(key);
|
|
423
|
+
const result = computeManyToManyRelation(schema, model, field);
|
|
424
|
+
cache.m2mRelation.set(key, result);
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Resolve the relation endpoints for an implicit many-to-many join table by its table
|
|
429
|
+
* name, or `undefined` if the table is not an implicit m2m join table. The join-table
|
|
430
|
+
* index is built once per schema (single pass) and reused, making this an O(1) lookup.
|
|
431
|
+
* See issue #2715.
|
|
432
|
+
*/
|
|
433
|
+
function getManyToManyJoinTable(schema, joinTableName) {
|
|
434
|
+
const cache = getSchemaLookupCache(schema);
|
|
435
|
+
if (!cache.m2mJoinTable) {
|
|
436
|
+
const map = /* @__PURE__ */ new Map();
|
|
437
|
+
for (const model of Object.values(schema.models)) for (const field of Object.values(model.fields)) {
|
|
438
|
+
const m2m = getManyToManyRelation(schema, model.name, field.name);
|
|
439
|
+
if (m2m?.joinTable && !map.has(m2m.joinTable)) map.set(m2m.joinTable, {
|
|
440
|
+
model: model.name,
|
|
441
|
+
field: field.name,
|
|
442
|
+
otherModel: m2m.otherModel,
|
|
443
|
+
otherField: m2m.otherField
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
cache.m2mJoinTable = map;
|
|
447
|
+
}
|
|
448
|
+
return cache.m2mJoinTable.get(joinTableName);
|
|
449
|
+
}
|
|
450
|
+
function computeManyToManyRelation(schema, model, field) {
|
|
402
451
|
const fieldDef = requireField(schema, model, field);
|
|
403
452
|
if (!fieldDef.array || !fieldDef.relation?.opposite) return;
|
|
404
453
|
const realModel = fieldDef.originModel ?? model;
|