@danielhritcu/zenstack-orm 3.5.13 → 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/LICENSE +21 -0
- package/dist/index.cjs +82 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +82 -9
- package/dist/index.js.map +1 -1
- package/package.json +146 -144
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ZenStack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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 =
|
|
3266
|
-
...
|
|
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
|
-
|
|
3295
|
-
|
|
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 === "
|
|
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
|
-
|
|
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];
|