@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.cjs CHANGED
@@ -5376,6 +5376,27 @@ var FindOperationHandler = class extends BaseOperationHandler {
5376
5376
  static {
5377
5377
  __name(this, "FindOperationHandler");
5378
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
+ }
5379
5400
  buildQuery(operation, args, validateArgs = true) {
5380
5401
  const normalizedArgs = this.normalizeArgs(args);
5381
5402
  const findOne = operation === "findFirst" || operation === "findUnique";
@@ -10115,20 +10136,26 @@ var ClientImpl = class _ClientImpl {
10115
10136
  return {};
10116
10137
  }
10117
10138
  const { jsonArrayFrom, jsonObjectFrom } = await import("kysely/helpers/postgres");
10139
+ const postProcessors = /* @__PURE__ */ new Map();
10118
10140
  const selections = await Promise.all(flat.map(async ([name, value]) => {
10119
- const query = await value[ZENSTACK_QUERY_SYMBOL](this);
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
+ }
10120
10146
  const kind = value[ZENSTACK_QUERY_KIND_SYMBOL];
10121
10147
  return kind === "many" ? jsonArrayFrom(query).as(name) : jsonObjectFrom(query).as(name);
10122
10148
  }));
10123
10149
  const row = await this.$qb.selectNoFrom(selections).executeTakeFirstOrThrow();
10124
10150
  const result = {};
10125
10151
  for (const [name, value] of Object.entries(row)) {
10152
+ const processedValue = postProcessors.get(name)?.(value) ?? value;
10126
10153
  const path = name.split("__");
10127
10154
  let current = result;
10128
10155
  for (const key of path.slice(0, -1)) {
10129
10156
  current = current[key] ??= {};
10130
10157
  }
10131
- current[path[path.length - 1]] = value;
10158
+ current[path[path.length - 1]] = processedValue;
10132
10159
  }
10133
10160
  return result;
10134
10161
  });
@@ -10254,35 +10281,127 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
10254
10281
  findUnique: /* @__PURE__ */ __name((args) => {
10255
10282
  const handler = new FindOperationHandler(client, model, inputValidator);
10256
10283
  return createPromise("findUnique", "findUnique", args, handler, true, false, {
10257
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findUnique", args),
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
+ },
10258
10304
  [ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
10259
10305
  });
10260
10306
  }, "findUnique"),
10261
10307
  findUniqueOrThrow: /* @__PURE__ */ __name((args) => {
10262
10308
  const handler = new FindOperationHandler(client, model, inputValidator);
10263
10309
  return createPromise("findUnique", "findUniqueOrThrow", args, handler, true, true, {
10264
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findUnique", args),
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
+ },
10265
10330
  [ZENSTACK_QUERY_KIND_SYMBOL]: "one"
10266
10331
  });
10267
10332
  }, "findUniqueOrThrow"),
10268
10333
  findFirst: /* @__PURE__ */ __name((args) => {
10269
10334
  const handler = new FindOperationHandler(client, model, inputValidator);
10270
10335
  return createPromise("findFirst", "findFirst", args, handler, true, false, {
10271
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findFirst", args),
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
+ },
10272
10356
  [ZENSTACK_QUERY_KIND_SYMBOL]: "maybeOne"
10273
10357
  });
10274
10358
  }, "findFirst"),
10275
10359
  findFirstOrThrow: /* @__PURE__ */ __name((args) => {
10276
10360
  const handler = new FindOperationHandler(client, model, inputValidator);
10277
10361
  return createPromise("findFirst", "findFirstOrThrow", args, handler, true, true, {
10278
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findFirst", args),
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
+ },
10279
10382
  [ZENSTACK_QUERY_KIND_SYMBOL]: "one"
10280
10383
  });
10281
10384
  }, "findFirstOrThrow"),
10282
10385
  findMany: /* @__PURE__ */ __name((args) => {
10283
10386
  const handler = new FindOperationHandler(client, model, inputValidator);
10284
10387
  return createPromise("findMany", "findMany", args, handler, true, false, {
10285
- [ZENSTACK_QUERY_SYMBOL]: () => handler.buildQuery("findMany", args),
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
+ },
10286
10405
  [ZENSTACK_QUERY_KIND_SYMBOL]: "many"
10287
10406
  });
10288
10407
  }, "findMany"),