@danielhritcu/zenstack-orm 3.5.16 → 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.js CHANGED
@@ -5327,6 +5327,27 @@ var FindOperationHandler = class extends BaseOperationHandler {
5327
5327
  static {
5328
5328
  __name(this, "FindOperationHandler");
5329
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
+ }
5330
5351
  buildQuery(operation, args, validateArgs = true) {
5331
5352
  const normalizedArgs = this.normalizeArgs(args);
5332
5353
  const findOne = operation === "findFirst" || operation === "findUnique";
@@ -10066,20 +10087,26 @@ var ClientImpl = class _ClientImpl {
10066
10087
  return {};
10067
10088
  }
10068
10089
  const { jsonArrayFrom, jsonObjectFrom } = await import("kysely/helpers/postgres");
10090
+ const postProcessors = /* @__PURE__ */ new Map();
10069
10091
  const selections = await Promise.all(flat.map(async ([name, value]) => {
10070
- const query = await value[ZENSTACK_QUERY_SYMBOL](this);
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
+ }
10071
10097
  const kind = value[ZENSTACK_QUERY_KIND_SYMBOL];
10072
10098
  return kind === "many" ? jsonArrayFrom(query).as(name) : jsonObjectFrom(query).as(name);
10073
10099
  }));
10074
10100
  const row = await this.$qb.selectNoFrom(selections).executeTakeFirstOrThrow();
10075
10101
  const result = {};
10076
10102
  for (const [name, value] of Object.entries(row)) {
10103
+ const processedValue = postProcessors.get(name)?.(value) ?? value;
10077
10104
  const path = name.split("__");
10078
10105
  let current = result;
10079
10106
  for (const key of path.slice(0, -1)) {
10080
10107
  current = current[key] ??= {};
10081
10108
  }
10082
- current[path[path.length - 1]] = value;
10109
+ current[path[path.length - 1]] = processedValue;
10083
10110
  }
10084
10111
  return result;
10085
10112
  });
@@ -10205,35 +10232,127 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
10205
10232
  findUnique: /* @__PURE__ */ __name((args) => {
10206
10233
  const handler = new FindOperationHandler(client, model, inputValidator);
10207
10234
  return createPromise("findUnique", "findUnique", args, handler, true, false, {
10208
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findUnique", args),
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
+ },
10209
10255
  [ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
10210
10256
  });
10211
10257
  }, "findUnique"),
10212
10258
  findUniqueOrThrow: /* @__PURE__ */ __name((args) => {
10213
10259
  const handler = new FindOperationHandler(client, model, inputValidator);
10214
10260
  return createPromise("findUnique", "findUniqueOrThrow", args, handler, true, true, {
10215
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findUnique", args),
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
+ },
10216
10281
  [ZENSTACK_QUERY_KIND_SYMBOL]: "one"
10217
10282
  });
10218
10283
  }, "findUniqueOrThrow"),
10219
10284
  findFirst: /* @__PURE__ */ __name((args) => {
10220
10285
  const handler = new FindOperationHandler(client, model, inputValidator);
10221
10286
  return createPromise("findFirst", "findFirst", args, handler, true, false, {
10222
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findFirst", args),
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
+ },
10223
10307
  [ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
10224
10308
  });
10225
10309
  }, "findFirst"),
10226
10310
  findFirstOrThrow: /* @__PURE__ */ __name((args) => {
10227
10311
  const handler = new FindOperationHandler(client, model, inputValidator);
10228
10312
  return createPromise("findFirst", "findFirstOrThrow", args, handler, true, true, {
10229
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findFirst", args),
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
+ },
10230
10333
  [ZENSTACK_QUERY_KIND_SYMBOL]: "one"
10231
10334
  });
10232
10335
  }, "findFirstOrThrow"),
10233
10336
  findMany: /* @__PURE__ */ __name((args) => {
10234
10337
  const handler = new FindOperationHandler(client, model, inputValidator);
10235
10338
  return createPromise("findMany", "findMany", args, handler, true, false, {
10236
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findMany", args),
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
+ },
10237
10356
  [ZENSTACK_QUERY_KIND_SYMBOL]: "many"
10238
10357
  });
10239
10358
  }, "findMany"),