@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.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 =
|
|
3371
|
+
let value = sourceValue;
|
|
3351
3372
|
if (sel.discriminatorWhere && value != null) {
|
|
3352
|
-
|
|
3353
|
-
|
|
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
|
}
|
|
@@ -5300,6 +5327,27 @@ var FindOperationHandler = class extends BaseOperationHandler {
|
|
|
5300
5327
|
static {
|
|
5301
5328
|
__name(this, "FindOperationHandler");
|
|
5302
5329
|
}
|
|
5330
|
+
buildBatchQuery(operation, args, validateArgs = true) {
|
|
5331
|
+
const normalizedArgs = this.normalizeArgs(args);
|
|
5332
|
+
const findOne = operation === "findFirst" || operation === "findUnique";
|
|
5333
|
+
let parsedArgs = validateArgs ? this.inputValidator.validateFindArgs(this.model, normalizedArgs, operation) : normalizedArgs;
|
|
5334
|
+
if (findOne) {
|
|
5335
|
+
parsedArgs = parsedArgs ?? {};
|
|
5336
|
+
parsedArgs.take = 1;
|
|
5337
|
+
}
|
|
5338
|
+
const defaultWhereArgs = this.applyDefaultWhere(this.model, parsedArgs);
|
|
5339
|
+
const defaultedArgs = this.applySchemaPluginDefaults(this.model, defaultWhereArgs);
|
|
5340
|
+
const searchExpandedArgs = this.applySearchExpansion(this.model, defaultedArgs);
|
|
5341
|
+
const { args: rewrittenArgs, plan } = this.rewriteVirtualRelations(this.model, searchExpandedArgs);
|
|
5342
|
+
return {
|
|
5343
|
+
query: this.buildReadQuery(this.model, rewrittenArgs),
|
|
5344
|
+
args: parsedArgs,
|
|
5345
|
+
plan
|
|
5346
|
+
};
|
|
5347
|
+
}
|
|
5348
|
+
applyBatchVirtualRelationPlan(data, plan) {
|
|
5349
|
+
return plan ? this.applyVirtualRelationPlan(data, plan) : data;
|
|
5350
|
+
}
|
|
5303
5351
|
buildQuery(operation, args, validateArgs = true) {
|
|
5304
5352
|
const normalizedArgs = this.normalizeArgs(args);
|
|
5305
5353
|
const findOne = operation === "findFirst" || operation === "findUnique";
|
|
@@ -10039,20 +10087,26 @@ var ClientImpl = class _ClientImpl {
|
|
|
10039
10087
|
return {};
|
|
10040
10088
|
}
|
|
10041
10089
|
const { jsonArrayFrom, jsonObjectFrom } = await import("kysely/helpers/postgres");
|
|
10090
|
+
const postProcessors = /* @__PURE__ */ new Map();
|
|
10042
10091
|
const selections = await Promise.all(flat.map(async ([name, value]) => {
|
|
10043
|
-
const
|
|
10092
|
+
const built = await value[ZENSTACK_QUERY_SYMBOL](this);
|
|
10093
|
+
const query = built && typeof built === "object" && "query" in built ? built.query : built;
|
|
10094
|
+
if (built && typeof built === "object" && "postProcess" in built && typeof built.postProcess === "function") {
|
|
10095
|
+
postProcessors.set(name, built.postProcess);
|
|
10096
|
+
}
|
|
10044
10097
|
const kind = value[ZENSTACK_QUERY_KIND_SYMBOL];
|
|
10045
10098
|
return kind === "many" ? jsonArrayFrom(query).as(name) : jsonObjectFrom(query).as(name);
|
|
10046
10099
|
}));
|
|
10047
10100
|
const row = await this.$qb.selectNoFrom(selections).executeTakeFirstOrThrow();
|
|
10048
10101
|
const result = {};
|
|
10049
10102
|
for (const [name, value] of Object.entries(row)) {
|
|
10103
|
+
const processedValue = postProcessors.get(name)?.(value) ?? value;
|
|
10050
10104
|
const path = name.split("__");
|
|
10051
10105
|
let current = result;
|
|
10052
10106
|
for (const key of path.slice(0, -1)) {
|
|
10053
10107
|
current = current[key] ??= {};
|
|
10054
10108
|
}
|
|
10055
|
-
current[path[path.length - 1]] =
|
|
10109
|
+
current[path[path.length - 1]] = processedValue;
|
|
10056
10110
|
}
|
|
10057
10111
|
return result;
|
|
10058
10112
|
});
|
|
@@ -10178,35 +10232,127 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
10178
10232
|
findUnique: /* @__PURE__ */ __name((args) => {
|
|
10179
10233
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10180
10234
|
return createPromise("findUnique", "findUnique", args, handler, true, false, {
|
|
10181
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10235
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10236
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findUnique");
|
|
10237
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10238
|
+
const batchHandler = handler;
|
|
10239
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findUnique", processedArgs);
|
|
10240
|
+
return {
|
|
10241
|
+
query,
|
|
10242
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10243
|
+
let result = value;
|
|
10244
|
+
if (result) {
|
|
10245
|
+
result = batchHandler.applyBatchVirtualRelationPlan(result, plan);
|
|
10246
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10247
|
+
}
|
|
10248
|
+
if (result && shouldApplyExtResult) {
|
|
10249
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10250
|
+
}
|
|
10251
|
+
return result ?? null;
|
|
10252
|
+
}, "postProcess")
|
|
10253
|
+
};
|
|
10254
|
+
},
|
|
10182
10255
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
|
|
10183
10256
|
});
|
|
10184
10257
|
}, "findUnique"),
|
|
10185
10258
|
findUniqueOrThrow: /* @__PURE__ */ __name((args) => {
|
|
10186
10259
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10187
10260
|
return createPromise("findUnique", "findUniqueOrThrow", args, handler, true, true, {
|
|
10188
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10261
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10262
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findUnique");
|
|
10263
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10264
|
+
const batchHandler = handler;
|
|
10265
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findUnique", processedArgs);
|
|
10266
|
+
return {
|
|
10267
|
+
query,
|
|
10268
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10269
|
+
if (!value) {
|
|
10270
|
+
throw createNotFoundError(model);
|
|
10271
|
+
}
|
|
10272
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10273
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10274
|
+
if (shouldApplyExtResult) {
|
|
10275
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10276
|
+
}
|
|
10277
|
+
return result;
|
|
10278
|
+
}, "postProcess")
|
|
10279
|
+
};
|
|
10280
|
+
},
|
|
10189
10281
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "one"
|
|
10190
10282
|
});
|
|
10191
10283
|
}, "findUniqueOrThrow"),
|
|
10192
10284
|
findFirst: /* @__PURE__ */ __name((args) => {
|
|
10193
10285
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10194
10286
|
return createPromise("findFirst", "findFirst", args, handler, true, false, {
|
|
10195
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10287
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10288
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findFirst");
|
|
10289
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10290
|
+
const batchHandler = handler;
|
|
10291
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findFirst", processedArgs);
|
|
10292
|
+
return {
|
|
10293
|
+
query,
|
|
10294
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10295
|
+
let result = value;
|
|
10296
|
+
if (result) {
|
|
10297
|
+
result = batchHandler.applyBatchVirtualRelationPlan(result, plan);
|
|
10298
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10299
|
+
}
|
|
10300
|
+
if (result && shouldApplyExtResult) {
|
|
10301
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10302
|
+
}
|
|
10303
|
+
return result ?? null;
|
|
10304
|
+
}, "postProcess")
|
|
10305
|
+
};
|
|
10306
|
+
},
|
|
10196
10307
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
|
|
10197
10308
|
});
|
|
10198
10309
|
}, "findFirst"),
|
|
10199
10310
|
findFirstOrThrow: /* @__PURE__ */ __name((args) => {
|
|
10200
10311
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10201
10312
|
return createPromise("findFirst", "findFirstOrThrow", args, handler, true, true, {
|
|
10202
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10313
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10314
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findFirst");
|
|
10315
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10316
|
+
const batchHandler = handler;
|
|
10317
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findFirst", processedArgs);
|
|
10318
|
+
return {
|
|
10319
|
+
query,
|
|
10320
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10321
|
+
if (!value) {
|
|
10322
|
+
throw createNotFoundError(model);
|
|
10323
|
+
}
|
|
10324
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10325
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10326
|
+
if (shouldApplyExtResult) {
|
|
10327
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10328
|
+
}
|
|
10329
|
+
return result;
|
|
10330
|
+
}, "postProcess")
|
|
10331
|
+
};
|
|
10332
|
+
},
|
|
10203
10333
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "one"
|
|
10204
10334
|
});
|
|
10205
10335
|
}, "findFirstOrThrow"),
|
|
10206
10336
|
findMany: /* @__PURE__ */ __name((args) => {
|
|
10207
10337
|
const handler = new FindOperationHandler(client, model, inputValidator);
|
|
10208
10338
|
return createPromise("findMany", "findMany", args, handler, true, false, {
|
|
10209
|
-
[ZENSTACK_QUERY_SYMBOL]: () =>
|
|
10339
|
+
[ZENSTACK_QUERY_SYMBOL]: () => {
|
|
10340
|
+
const shouldApplyExtResult = hasAnyExtResult && EXT_RESULT_OPERATIONS.has("findMany");
|
|
10341
|
+
const processedArgs = shouldApplyExtResult ? prepareArgsForExtResult(args, model, schema, plugins) : args;
|
|
10342
|
+
const batchHandler = handler;
|
|
10343
|
+
const { query, args: batchArgs, plan } = batchHandler.buildBatchQuery("findMany", processedArgs);
|
|
10344
|
+
return {
|
|
10345
|
+
query,
|
|
10346
|
+
postProcess: /* @__PURE__ */ __name((value) => {
|
|
10347
|
+
let result = batchHandler.applyBatchVirtualRelationPlan(value, plan);
|
|
10348
|
+
result = resultProcessor.processResult(result, model, batchArgs);
|
|
10349
|
+
if (shouldApplyExtResult) {
|
|
10350
|
+
result = applyExtResult(result, model, args, schema, plugins);
|
|
10351
|
+
}
|
|
10352
|
+
return result;
|
|
10353
|
+
}, "postProcess")
|
|
10354
|
+
};
|
|
10355
|
+
},
|
|
10210
10356
|
[ZENSTACK_QUERY_KIND_SYMBOL]: "many"
|
|
10211
10357
|
});
|
|
10212
10358
|
}, "findMany"),
|