@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.js CHANGED
@@ -3202,6 +3202,7 @@ var BaseOperationHandler = class {
3202
3202
  if (vr.kind === "filtered") {
3203
3203
  const sourceField = vr.relation;
3204
3204
  const isSelfReferencing = field === sourceField;
3205
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3205
3206
  if (isSelfReferencing) {
3206
3207
  plans.push({
3207
3208
  kind: "filtered",
@@ -3210,11 +3211,12 @@ var BaseOperationHandler = class {
3210
3211
  sourceModel: vr.targetModel,
3211
3212
  single: vr.single,
3212
3213
  discriminatorWhere: vr.where,
3213
- introducedSourceField: false
3214
+ introducedSourceField: false,
3215
+ nestedPlan
3214
3216
  });
3215
3217
  } else {
3216
- const nestedArgs = value === true ? {} : {
3217
- ...value
3218
+ const nestedArgs = rewrittenValue === true ? {} : {
3219
+ ...rewrittenValue
3218
3220
  };
3219
3221
  const where = nestedArgs.where ? {
3220
3222
  ...nestedArgs.where,
@@ -3235,15 +3237,17 @@ var BaseOperationHandler = class {
3235
3237
  sourceField,
3236
3238
  sourceModel: vr.targetModel,
3237
3239
  single: vr.single,
3238
- introducedSourceField: selection[sourceField] === void 0
3240
+ introducedSourceField: selection[sourceField] === void 0,
3241
+ nestedPlan
3239
3242
  });
3240
3243
  }
3241
3244
  } else if (vr.kind === "through") {
3242
3245
  const path = vr.path;
3243
3246
  const [sourceField, ...restPath] = path;
3244
3247
  if (!sourceField) continue;
3245
- let throughValue = value === true ? true : {
3246
- ...value
3248
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(vr.targetModel, value, virtualRelations);
3249
+ let throughValue = rewrittenValue === true ? true : {
3250
+ ...rewrittenValue
3247
3251
  };
3248
3252
  for (let i = restPath.length - 1; i >= 0; i--) {
3249
3253
  throughValue = {
@@ -3260,7 +3264,27 @@ var BaseOperationHandler = class {
3260
3264
  sourceField,
3261
3265
  sourceModel: vr.targetModel,
3262
3266
  path: restPath,
3263
- introducedSourceField: selection[sourceField] === void 0
3267
+ introducedSourceField: selection[sourceField] === void 0,
3268
+ nestedPlan
3269
+ });
3270
+ }
3271
+ }
3272
+ for (const [field, value] of Object.entries(nextSelection)) {
3273
+ const fieldDef = getField(this.schema, model, field);
3274
+ if (!fieldDef?.relation || value === true || !value || typeof value !== "object") {
3275
+ continue;
3276
+ }
3277
+ const { value: rewrittenValue, plan: nestedPlan } = this.rewriteNestedVirtualRelationArgs(fieldDef.type, value, virtualRelations);
3278
+ if (rewrittenValue !== value) {
3279
+ nextSelection[field] = rewrittenValue;
3280
+ }
3281
+ if (nestedPlan) {
3282
+ plans.push({
3283
+ kind: "actual",
3284
+ field,
3285
+ sourceField: field,
3286
+ sourceModel: fieldDef.type,
3287
+ nestedPlan
3264
3288
  });
3265
3289
  }
3266
3290
  }
@@ -3271,6 +3295,46 @@ var BaseOperationHandler = class {
3271
3295
  } : void 0
3272
3296
  };
3273
3297
  }
3298
+ rewriteNestedVirtualRelationArgs(model, value, virtualRelations) {
3299
+ if (value === true || !value || typeof value !== "object" || Array.isArray(value)) {
3300
+ return {
3301
+ value
3302
+ };
3303
+ }
3304
+ let changed = false;
3305
+ const nextValue = {
3306
+ ...value
3307
+ };
3308
+ let combinedPlan;
3309
+ const mergePlan = /* @__PURE__ */ __name((plan) => {
3310
+ if (!plan) return;
3311
+ if (!combinedPlan) {
3312
+ combinedPlan = {
3313
+ selections: [
3314
+ ...plan.selections
3315
+ ]
3316
+ };
3317
+ } else {
3318
+ combinedPlan.selections.push(...plan.selections);
3319
+ }
3320
+ }, "mergePlan");
3321
+ if (nextValue["select"] && typeof nextValue["select"] === "object") {
3322
+ const { selection, plan } = this.rewriteSelectionMap(model, nextValue["select"], virtualRelations);
3323
+ nextValue["select"] = selection;
3324
+ changed = true;
3325
+ mergePlan(plan);
3326
+ }
3327
+ if (nextValue["include"] && typeof nextValue["include"] === "object") {
3328
+ const { selection, plan } = this.rewriteSelectionMap(model, nextValue["include"], virtualRelations);
3329
+ nextValue["include"] = selection;
3330
+ changed = true;
3331
+ mergePlan(plan);
3332
+ }
3333
+ return {
3334
+ value: changed ? nextValue : value,
3335
+ plan: combinedPlan
3336
+ };
3337
+ }
3274
3338
  applyVirtualRelationPlan(data, plan) {
3275
3339
  if (!plan || data == null) return data;
3276
3340
  if (Array.isArray(data)) return data.map((item) => this.applyVirtualRelationPlan(item, plan));
@@ -3280,15 +3344,24 @@ var BaseOperationHandler = class {
3280
3344
  };
3281
3345
  for (const sel of plan.selections) {
3282
3346
  const sourceValue = this.normalizeVirtualRelationValue(result[sel.sourceField]);
3283
- if (sel.kind === "filtered") {
3347
+ if (sel.kind === "actual") {
3348
+ result[sel.field] = sel.nestedPlan ? this.applyVirtualRelationPlan(sourceValue, sel.nestedPlan) : sourceValue;
3349
+ } else if (sel.kind === "filtered") {
3284
3350
  let value = sel.single ? Array.isArray(sourceValue) ? sourceValue[0] ?? null : sourceValue ?? null : sourceValue;
3285
3351
  if (sel.discriminatorWhere && value != null) {
3286
3352
  const matches = Object.entries(sel.discriminatorWhere).every(([key, val]) => result[key] === val);
3287
3353
  if (!matches) value = null;
3288
3354
  }
3355
+ if (sel.nestedPlan) {
3356
+ value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3357
+ }
3289
3358
  result[sel.field] = value;
3290
3359
  } else if (sel.kind === "through") {
3291
- result[sel.field] = this.extractThroughValue(sourceValue, sel.path ?? []);
3360
+ let value = this.extractThroughValue(sourceValue, sel.path ?? []);
3361
+ if (sel.nestedPlan) {
3362
+ value = this.applyVirtualRelationPlan(value, sel.nestedPlan);
3363
+ }
3364
+ result[sel.field] = value;
3292
3365
  }
3293
3366
  if (sel.introducedSourceField) {
3294
3367
  delete result[sel.sourceField];