@danielhritcu/zenstack-orm 3.5.14 → 3.5.15

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
@@ -3251,6 +3251,7 @@ var BaseOperationHandler = class {
3251
3251
  if (vr.kind === "filtered") {
3252
3252
  const sourceField = vr.relation;
3253
3253
  const isSelfReferencing = field === sourceField;
3254
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3254
3255
  if (isSelfReferencing) {
3255
3256
  plans.push({
3256
3257
  kind: "filtered",
@@ -3259,11 +3260,12 @@ var BaseOperationHandler = class {
3259
3260
  sourceModel: vr.targetModel,
3260
3261
  single: vr.single,
3261
3262
  discriminatorWhere: vr.where,
3262
- introducedSourceField: false
3263
+ introducedSourceField: false,
3264
+ nestedPlan
3263
3265
  });
3264
3266
  } else {
3265
- const nestedArgs = value === true ? {} : {
3266
- ...value
3267
+ const nestedArgs = rewrittenValue === true ? {} : {
3268
+ ...rewrittenValue
3267
3269
  };
3268
3270
  const where = nestedArgs.where ? {
3269
3271
  ...nestedArgs.where,
@@ -3284,15 +3286,17 @@ var BaseOperationHandler = class {
3284
3286
  sourceField,
3285
3287
  sourceModel: vr.targetModel,
3286
3288
  single: vr.single,
3287
- introducedSourceField: selection[sourceField] === void 0
3289
+ introducedSourceField: selection[sourceField] === void 0,
3290
+ nestedPlan
3288
3291
  });
3289
3292
  }
3290
3293
  } else if (vr.kind === "through") {
3291
3294
  const path = vr.path;
3292
3295
  const [sourceField, ...restPath] = path;
3293
3296
  if (!sourceField) continue;
3294
- let throughValue = value === true ? true : {
3295
- ...value
3297
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3298
+ let throughValue = rewrittenValue === true ? true : {
3299
+ ...rewrittenValue
3296
3300
  };
3297
3301
  for (let i = restPath.length - 1; i >= 0; i--) {
3298
3302
  throughValue = {
@@ -3309,7 +3313,27 @@ var BaseOperationHandler = class {
3309
3313
  sourceField,
3310
3314
  sourceModel: vr.targetModel,
3311
3315
  path: restPath,
3312
- introducedSourceField: selection[sourceField] === void 0
3316
+ introducedSourceField: selection[sourceField] === void 0,
3317
+ nestedPlan
3318
+ });
3319
+ }
3320
+ }
3321
+ for (const [field, value] of Object.entries(nextSelection)) {
3322
+ const fieldDef = getField(this.schema, model, field);
3323
+ if (!fieldDef?.relation || value === true || !value || typeof value !== "object") {
3324
+ continue;
3325
+ }
3326
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(fieldDef.type, value, virtualRelations);
3327
+ if (rewrittenValue !== value) {
3328
+ nextSelection[field] = rewrittenValue;
3329
+ }
3330
+ if (nestedPlan) {
3331
+ plans.push({
3332
+ kind: "actual",
3333
+ field,
3334
+ sourceField: field,
3335
+ sourceModel: fieldDef.type,
3336
+ nestedPlan
3313
3337
  });
3314
3338
  }
3315
3339
  }
@@ -3320,6 +3344,46 @@ var BaseOperationHandler = class {
3320
3344
  } : void 0
3321
3345
  };
3322
3346
  }
3347
+ rewriteNestedVirtualRelationArgs(model, value, virtualRelations) {
3348
+ if (value === true || !value || typeof value !== "object" || Array.isArray(value)) {
3349
+ return {
3350
+ value
3351
+ };
3352
+ }
3353
+ let changed = false;
3354
+ const nextValue = {
3355
+ ...value
3356
+ };
3357
+ let combinedPlan;
3358
+ const mergePlan = /* @__PURE__ */ __name((plan) => {
3359
+ if (!plan) return;
3360
+ if (!combinedPlan) {
3361
+ combinedPlan = {
3362
+ selections: [
3363
+ ...plan.selections
3364
+ ]
3365
+ };
3366
+ } else {
3367
+ combinedPlan.selections.push(...plan.selections);
3368
+ }
3369
+ }, "mergePlan");
3370
+ if (nextValue["select"] && typeof nextValue["select"] === "object") {
3371
+ const { selection, plan } = this.rewriteSelectionMap(model, nextValue["select"], virtualRelations);
3372
+ nextValue["select"] = selection;
3373
+ changed = true;
3374
+ mergePlan(plan);
3375
+ }
3376
+ if (nextValue["include"] && typeof nextValue["include"] === "object") {
3377
+ const { selection, plan } = this.rewriteSelectionMap(model, nextValue["include"], virtualRelations);
3378
+ nextValue["include"] = selection;
3379
+ changed = true;
3380
+ mergePlan(plan);
3381
+ }
3382
+ return {
3383
+ value: changed ? nextValue : value,
3384
+ plan: combinedPlan
3385
+ };
3386
+ }
3323
3387
  applyVirtualRelationPlan(data, plan) {
3324
3388
  if (!plan || data == null) return data;
3325
3389
  if (Array.isArray(data)) return data.map((item) => this.applyVirtualRelationPlan(item, plan));
@@ -3329,15 +3393,24 @@ var BaseOperationHandler = class {
3329
3393
  };
3330
3394
  for (const sel of plan.selections) {
3331
3395
  const sourceValue = this.normalizeVirtualRelationValue(result[sel.sourceField]);
3332
- if (sel.kind === "filtered") {
3396
+ if (sel.kind === "actual") {
3397
+ result[sel.field] = sel.nestedPlan ? this.applyVirtualRelationPlan(sourceValue, sel.nestedPlan) : sourceValue;
3398
+ } else if (sel.kind === "filtered") {
3333
3399
  let value = sel.single ? Array.isArray(sourceValue) ? sourceValue[0] ?? null : sourceValue ?? null : sourceValue;
3334
3400
  if (sel.discriminatorWhere && value != null) {
3335
3401
  const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3336
3402
  if (!matches) value = null;
3337
3403
  }
3404
+ if (sel.nestedPlan) {
3405
+ value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3406
+ }
3338
3407
  result[sel.field] = value;
3339
3408
  } else if (sel.kind === "through") {
3340
- result[sel.field] = this.extractThroughValue(sourceValue, sel.path ?? []);
3409
+ let value = this.extractThroughValue(sourceValue, sel.path ?? []);
3410
+ if (sel.nestedPlan) {
3411
+ value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3412
+ }
3413
+ result[sel.field] = value;
3341
3414
  }
3342
3415
  if (sel.introducedSourceField) {
3343
3416
  delete result[sel.sourceField];