@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.js CHANGED
@@ -3203,6 +3203,7 @@ var BaseOperationHandler = class {
3203
3203
  const sourceField = vr.relation;
3204
3204
  const isSelfReferencing = field === sourceField;
3205
3205
  const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3206
+ const discriminatorTarget = vr.where ? this.resolveFilteredRelationDiscriminatorTarget(model, vr.targetModel, vr.where) : void 0;
3206
3207
  if (isSelfReferencing) {
3207
3208
  plans.push({
3208
3209
  kind: "filtered",
@@ -3211,6 +3212,7 @@ var BaseOperationHandler = class {
3211
3212
  sourceModel: vr.targetModel,
3212
3213
  single: vr.single,
3213
3214
  discriminatorWhere: vr.where,
3215
+ discriminatorTarget,
3214
3216
  introducedSourceField: false,
3215
3217
  nestedPlan
3216
3218
  });
@@ -3238,6 +3240,7 @@ var BaseOperationHandler = class {
3238
3240
  sourceModel: vr.targetModel,
3239
3241
  single: vr.single,
3240
3242
  introducedSourceField: selection[sourceField] === void 0,
3243
+ discriminatorTarget,
3241
3244
  nestedPlan
3242
3245
  });
3243
3246
  }
@@ -3335,6 +3338,24 @@ var BaseOperationHandler = class {
3335
3338
  plan: combinedPlan
3336
3339
  };
3337
3340
  }
3341
+ resolveFilteredRelationDiscriminatorTarget(parentModel, relationModel, where) {
3342
+ const keys = Object.keys(where);
3343
+ const parentMatches = keys.every((key) => {
3344
+ const field = getField(this.schema, parentModel, key);
3345
+ return !!field && !field.relation;
3346
+ });
3347
+ if (parentMatches) {
3348
+ return "parent";
3349
+ }
3350
+ const relationMatches = keys.every((key) => {
3351
+ const field = getField(this.schema, relationModel, key);
3352
+ return !!field && !field.relation;
3353
+ });
3354
+ if (relationMatches) {
3355
+ return "relation";
3356
+ }
3357
+ return "parent";
3358
+ }
3338
3359
  applyVirtualRelationPlan(data, plan) {
3339
3360
  if (!plan || data == null) return data;
3340
3361
  if (Array.isArray(data)) return data.map((item) => this.applyVirtualRelationPlan(item, plan));
@@ -3347,11 +3368,17 @@ var BaseOperationHandler = class {
3347
3368
  if (sel.kind === "actual") {
3348
3369
  result[sel.field] = sel.nestedPlan ? this.applyVirtualRelationPlan(sourceValue, sel.nestedPlan) : sourceValue;
3349
3370
  } else if (sel.kind === "filtered") {
3350
- let value = sel.single ? Array.isArray(sourceValue) ? sourceValue[0] ?? null : sourceValue ?? null : sourceValue;
3371
+ let value = sourceValue;
3351
3372
  if (sel.discriminatorWhere && value != null) {
3352
- const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3353
- if (!matches) value = null;
3373
+ if (sel.discriminatorTarget === "relation") {
3374
+ const matchesRelation = /* @__PURE__ */ __name((item) => item != null && typeof item === "object" && Object.entries(sel.discriminatorWhere).every(([key, expected]) => item[key] === expected), "matchesRelation");
3375
+ value = Array.isArray(value) ? value.filter(matchesRelation) : matchesRelation(value) ? value : null;
3376
+ } else {
3377
+ const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3378
+ if (!matches) value = null;
3379
+ }
3354
3380
  }
3381
+ value = sel.single ? Array.isArray(value) ? value[0] ?? null : value ?? null : value;
3355
3382
  if (sel.nestedPlan) {
3356
3383
  value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3357
3384
  }