@danielhritcu/zenstack-orm 3.5.15 → 3.5.17
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 +156 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +156 -10
- package/dist/index.js.map +1 -1
- package/package.json +145 -145
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 =
|
|
3420
|
+
let value = sourceValue;
|
|
3400
3421
|
if (sel.discriminatorWhere && value != null) {
|
|
3401
|
-
|
|
3402
|
-
|
|
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
|
}
|
|
@@ -5349,6 +5376,27 @@ var FindOperationHandler = class extends BaseOperationHandler {
|
|
|
5349
5376
|
static {
|
|
5350
5377
|
__name(this, "FindOperationHandler");
|
|
5351
5378
|
}
|
|
5379
|
+
buildBatchQuery(operation, args, validateArgs = true) {
|
|
5380
|
+
const normalizedArgs = this.normalizeArgs(args);
|
|
5381
|
+
const findOne = operation === "findFirst" || operation === "findUnique";
|
|
5382
|
+
let parsedArgs = validateArgs ? this.inputValidator.validateFindArgs(this.model, normalizedArgs, operation) : normalizedArgs;
|
|
5383
|
+
if (findOne) {
|
|
5384
|
+
parsedArgs = parsedArgs ?? {};
|
|
5385
|
+
parsedArgs.take = 1;
|
|
5386
|
+
}
|
|
5387
|
+
const defaultWhereArgs = this.applyDefaultWhere(this.model, parsedArgs);
|
|
5388
|
+
const defaultedArgs = this.applySchemaPluginDefaults(this.model, defaultWhereArgs);
|
|
5389
|
+
const searchExpandedArgs = this.applySearchExpansion(this.model, defaultedArgs);
|
|
5390
|
+
const { args: rewrittenArgs, plan } = this.rewriteVirtualRelations(this.model, searchExpandedArgs);
|
|
5391
|
+
return {
|
|
5392
|
+
query: this.buildReadQuery(this.model, rewrittenArgs),
|
|
5393
|
+
args: parsedArgs,
|
|
5394
|
+
plan
|
|
5395
|
+
};
|
|
5396
|
+
}
|
|
5397
|
+
applyBatchVirtualRelationPlan(data, plan) {
|
|
5398
|
+
return plan ? this.applyVirtualRelationPlan(data, plan) : data;
|
|
5399
|
+
}
|
|
5352
5400
|
buildQuery(operation, args, validateArgs = true) {
|
|
5353
5401
|
const normalizedArgs = this.normalizeArgs(args);
|
|
5354
5402
|
const findOne = operation === "findFirst" || operation === "findUnique";
|
|
@@ -10088,20 +10136,26 @@ var ClientImpl = class _ClientImpl {
|
|
|
10088
10136
|
return {};
|
|
10089
10137
|
}
|
|
10090
10138
|
const { jsonArrayFrom, jsonObjectFrom } = await import("kysely/helpers/postgres");
|
|
10139
|
+
const postProcessors = /* @__PURE__ */ new Map();
|
|
10091
10140
|
const selections = await Promise.all(flat.map(async ([name, value]) => {
|
|
10092
|
-
const
|
|
10141
|
+
const built = await value[ZENSTACK_QUERY_SYMBOL](this);
|
|
10142
|
+
const query = built && typeof built === "object" && "query" in built ? built.query : built;
|
|
10143
|
+
if (built && typeof built === "object" && "postProcess" in built && typeof built.postProcess === "function") {
|
|
10144
|
+
postProcessors.set(name, built.postProcess);
|
|
10145
|
+
}
|
|
10093
10146
|
const kind = value[ZENSTACK_QUERY_KIND_SYMBOL];
|
|
10094
10147
|
return kind === "many" ? jsonArrayFrom(query).as(name) : jsonObjectFrom(query).as(name);
|
|
10095
10148
|
}));
|
|
10096
10149
|
const row = await this.$qb.selectNoFrom(selections).executeTakeFirstOrThrow();
|
|
10097
10150
|
const result = {};
|
|
10098
10151
|
for (const [name, value] of Object.entries(row)) {
|
|
10152
|
+
const processedValue = postProcessors.get(name)?.(value) ?? value;
|
|
10099
10153
|
const path = name.split("__");
|
|
10100
10154
|
let current = result;
|
|
10101
10155
|
for (const key of path.slice(0, -1)) {
|
|
10102
10156
|
current = current[key] ??= {};
|
|
10103
10157
|
}
|
|
10104
|
-
current[path[path.length - 1]] =
|
|
10158
|
+
current[path[path.length - 1]] = processedValue;
|
|
10105
10159
|
}
|
|
10106
10160
|
return result;
|
|
10107
10161
|
});
|
|
@@ -10227,35 +10281,127 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
10227
10281
|
findUnique: /* @__PURE__ */ __name((args) => {
|
|
10228
10282
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10229
10283
|
return createPromise("findUnique", "findUnique", args, handler, true, false, {
|
|
10230
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10284
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10285
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findUnique");
|
|
10286
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10287
|
+
const batchHandler = handler;
|
|
10288
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findUnique", processedArgs);
|
|
10289
|
+
return {
|
|
10290
|
+
query,
|
|
10291
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10292
|
+
let result = value;
|
|
10293
|
+
if (result) {
|
|
10294
|
+
result = batchHandler.applyBatchVirtualRelationPlan(result, plan);
|
|
10295
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10296
|
+
}
|
|
10297
|
+
if (result && shouldApplyExtResult) {
|
|
10298
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10299
|
+
}
|
|
10300
|
+
return result ?? null;
|
|
10301
|
+
}, "postProcess")
|
|
10302
|
+
};
|
|
10303
|
+
},
|
|
10231
10304
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
|
|
10232
10305
|
});
|
|
10233
10306
|
}, "findUnique"),
|
|
10234
10307
|
findUniqueOrThrow: /* @__PURE__ */ __name((args) => {
|
|
10235
10308
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10236
10309
|
return createPromise("findUnique", "findUniqueOrThrow", args, handler, true, true, {
|
|
10237
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10310
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10311
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findUnique");
|
|
10312
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10313
|
+
const batchHandler = handler;
|
|
10314
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findUnique", processedArgs);
|
|
10315
|
+
return {
|
|
10316
|
+
query,
|
|
10317
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10318
|
+
if (!value) {
|
|
10319
|
+
throw createNotFoundError(model);
|
|
10320
|
+
}
|
|
10321
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10322
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10323
|
+
if (shouldApplyExtResult) {
|
|
10324
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10325
|
+
}
|
|
10326
|
+
return result;
|
|
10327
|
+
}, "postProcess")
|
|
10328
|
+
};
|
|
10329
|
+
},
|
|
10238
10330
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "one"
|
|
10239
10331
|
});
|
|
10240
10332
|
}, "findUniqueOrThrow"),
|
|
10241
10333
|
findFirst: /* @__PURE__ */ __name((args) => {
|
|
10242
10334
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10243
10335
|
return createPromise("findFirst", "findFirst", args, handler, true, false, {
|
|
10244
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10336
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10337
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findFirst");
|
|
10338
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10339
|
+
const batchHandler = handler;
|
|
10340
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findFirst", processedArgs);
|
|
10341
|
+
return {
|
|
10342
|
+
query,
|
|
10343
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10344
|
+
let result = value;
|
|
10345
|
+
if (result) {
|
|
10346
|
+
result = batchHandler.applyBatchVirtualRelationPlan(result, plan);
|
|
10347
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10348
|
+
}
|
|
10349
|
+
if (result && shouldApplyExtResult) {
|
|
10350
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10351
|
+
}
|
|
10352
|
+
return result ?? null;
|
|
10353
|
+
}, "postProcess")
|
|
10354
|
+
};
|
|
10355
|
+
},
|
|
10245
10356
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
|
|
10246
10357
|
});
|
|
10247
10358
|
}, "findFirst"),
|
|
10248
10359
|
findFirstOrThrow: /* @__PURE__ */ __name((args) => {
|
|
10249
10360
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10250
10361
|
return createPromise("findFirst", "findFirstOrThrow", args, handler, true, true, {
|
|
10251
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10362
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10363
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findFirst");
|
|
10364
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10365
|
+
const batchHandler = handler;
|
|
10366
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findFirst", processedArgs);
|
|
10367
|
+
return {
|
|
10368
|
+
query,
|
|
10369
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10370
|
+
if (!value) {
|
|
10371
|
+
throw createNotFoundError(model);
|
|
10372
|
+
}
|
|
10373
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10374
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10375
|
+
if (shouldApplyExtResult) {
|
|
10376
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10377
|
+
}
|
|
10378
|
+
return result;
|
|
10379
|
+
}, "postProcess")
|
|
10380
|
+
};
|
|
10381
|
+
},
|
|
10252
10382
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "one"
|
|
10253
10383
|
});
|
|
10254
10384
|
}, "findFirstOrThrow"),
|
|
10255
10385
|
findMany: /* @__PURE__ */ __name((args) => {
|
|
10256
10386
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10257
10387
|
return createPromise("findMany", "findMany", args, handler, true, false, {
|
|
10258
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10388
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10389
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findMany");
|
|
10390
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10391
|
+
const batchHandler = handler;
|
|
10392
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findMany", processedArgs);
|
|
10393
|
+
return {
|
|
10394
|
+
query,
|
|
10395
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10396
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10397
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10398
|
+
if (shouldApplyExtResult) {
|
|
10399
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10400
|
+
}
|
|
10401
|
+
return result;
|
|
10402
|
+
}, "postProcess")
|
|
10403
|
+
};
|
|
10404
|
+
},
|
|
10259
10405
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "many"
|
|
10260
10406
|
});
|
|
10261
10407
|
}, "findMany"),
|