@danielhritcu/zenstack-orm 3.5.15 → 3.5.16

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 CHANGED
@@ -3252,6 +3252,7 @@ var BaseOperationHandler = class {
3252
3252
  const sourceField = vr.relation;
3253
3253
  const isSelfReferencing = field === sourceField;
3254
3254
  const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3255
+ const discriminatorTarget = vr.where ? this.resolveFilteredRelationDiscriminatorTarget(model, vr.targetModel, vr.where) : void 0;
3255
3256
  if (isSelfReferencing) {
3256
3257
  plans.push({
3257
3258
  kind: "filtered",
@@ -3260,6 +3261,7 @@ var BaseOperationHandler = class {
3260
3261
  sourceModel: vr.targetModel,
3261
3262
  single: vr.single,
3262
3263
  discriminatorWhere: vr.where,
3264
+ discriminatorTarget,
3263
3265
  introducedSourceField: false,
3264
3266
  nestedPlan
3265
3267
  });
@@ -3287,6 +3289,7 @@ var BaseOperationHandler = class {
3287
3289
  sourceModel: vr.targetModel,
3288
3290
  single: vr.single,
3289
3291
  introducedSourceField: selection[sourceField] === void 0,
3292
+ discriminatorTarget,
3290
3293
  nestedPlan
3291
3294
  });
3292
3295
  }
@@ -3384,6 +3387,24 @@ var BaseOperationHandler = class {
3384
3387
  plan: combinedPlan
3385
3388
  };
3386
3389
  }
3390
+ resolveFilteredRelationDiscriminatorTarget(parentModel, relationModel, where) {
3391
+ const keys = Object.keys(where);
3392
+ const parentMatches = keys.every((key) => {
3393
+ const field = getField(this.schema, parentModel, key);
3394
+ return !!field && !field.relation;
3395
+ });
3396
+ if (parentMatches) {
3397
+ return "parent";
3398
+ }
3399
+ const relationMatches = keys.every((key) => {
3400
+ const field = getField(this.schema, relationModel, key);
3401
+ return !!field && !field.relation;
3402
+ });
3403
+ if (relationMatches) {
3404
+ return "relation";
3405
+ }
3406
+ return "parent";
3407
+ }
3387
3408
  applyVirtualRelationPlan(data, plan) {
3388
3409
  if (!plan || data == null) return data;
3389
3410
  if (Array.isArray(data)) return data.map((item) => this.applyVirtualRelationPlan(item, plan));
@@ -3396,11 +3417,17 @@ var BaseOperationHandler = class {
3396
3417
  if (sel.kind === "actual") {
3397
3418
  result[sel.field] = sel.nestedPlan ? this.applyVirtualRelationPlan(sourceValue, sel.nestedPlan) : sourceValue;
3398
3419
  } else if (sel.kind === "filtered") {
3399
- let value = sel.single ? Array.isArray(sourceValue) ? sourceValue[0] ?? null : sourceValue ?? null : sourceValue;
3420
+ let value = sourceValue;
3400
3421
  if (sel.discriminatorWhere && value != null) {
3401
- const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3402
- if (!matches) value = null;
3422
+ if (sel.discriminatorTarget === "relation") {
3423
+ const matchesRelation = /* @__PURE__ */ __name((item) => item != null && typeof item === "object" && Object.entries(sel.discriminatorWhere).every(([key, expected]) => item[key] === expected), "matchesRelation");
3424
+ value = Array.isArray(value) ? value.filter(matchesRelation) : matchesRelation(value) ? value : null;
3425
+ } else {
3426
+ const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3427
+ if (!matches) value = null;
3428
+ }
3403
3429
  }
3430
+ value = sel.single ? Array.isArray(value) ? value[0] ?? null : value ?? null : value;
3404
3431
  if (sel.nestedPlan) {
3405
3432
  value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3406
3433
  }