@danielhritcu/zenstack-orm 3.5.12 → 3.5.14

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
@@ -3328,7 +3328,7 @@ var BaseOperationHandler = class {
3328
3328
  ...data
3329
3329
  };
3330
3330
  for (const sel of plan.selections) {
3331
- const sourceValue = result[sel.sourceField];
3331
+ const sourceValue = this.normalizeVirtualRelationValue(result[sel.sourceField]);
3332
3332
  if (sel.kind === "filtered") {
3333
3333
  let value = sel.single ? Array.isArray(sourceValue) ? sourceValue[0] ?? null : sourceValue ?? null : sourceValue;
3334
3334
  if (sel.discriminatorWhere && value != null) {
@@ -3345,7 +3345,18 @@ var BaseOperationHandler = class {
3345
3345
  }
3346
3346
  return result;
3347
3347
  }
3348
+ normalizeVirtualRelationValue(value) {
3349
+ if (typeof value !== "string") {
3350
+ return value;
3351
+ }
3352
+ try {
3353
+ return JSON.parse(value);
3354
+ } catch {
3355
+ return value;
3356
+ }
3357
+ }
3348
3358
  extractThroughValue(value, path) {
3359
+ value = this.normalizeVirtualRelationValue(value);
3349
3360
  if (path.length === 0) return value ?? null;
3350
3361
  const [segment, ...rest] = path;
3351
3362
  if (Array.isArray(value)) {
@@ -6249,14 +6260,32 @@ var ZodSchemaFactory = class {
6249
6260
  return import_zod2.z.strictObject(fields);
6250
6261
  }
6251
6262
  addExtResultFields(model, fields) {
6263
+ const keys = [
6264
+ (0, import_common_helpers9.lowerCaseFirst)(model),
6265
+ model
6266
+ ];
6267
+ const schemaExtResult = this.schema.plugins?.extResult;
6268
+ for (const key of keys) {
6269
+ const modelConfig = schemaExtResult?.[key];
6270
+ if (modelConfig) {
6271
+ for (const field of Object.keys(modelConfig)) {
6272
+ fields[field] = import_zod2.z.boolean().optional();
6273
+ }
6274
+ break;
6275
+ }
6276
+ }
6252
6277
  for (const plugin of this.plugins) {
6253
6278
  const resultConfig = plugin.result;
6254
6279
  if (resultConfig) {
6255
- const modelConfig = resultConfig[(0, import_common_helpers9.lowerCaseFirst)(model)];
6256
- if (modelConfig) {
6280
+ for (const key of keys) {
6281
+ const modelConfig = resultConfig[key];
6282
+ if (!modelConfig) {
6283
+ continue;
6284
+ }
6257
6285
  for (const field of Object.keys(modelConfig)) {
6258
6286
  fields[field] = import_zod2.z.boolean().optional();
6259
6287
  }
6288
+ break;
6260
6289
  }
6261
6290
  }
6262
6291
  }
@@ -10075,7 +10104,7 @@ __name(isProcedureIncluded, "isProcedureIncluded");
10075
10104
  function createModelCrudHandler(client, model, inputValidator, resultProcessor) {
10076
10105
  const plugins = client.$options.plugins ?? [];
10077
10106
  const schema = client.$schema;
10078
- const hasAnyExtResult = hasExtResultFieldDefs(plugins);
10107
+ const hasAnyExtResult = hasExtResultFieldDefs(schema, plugins);
10079
10108
  const createPromise = /* @__PURE__ */ __name((operation, nominalOperation, args, handler, postProcess = false, throwIfNoResult = false, metadata = {}) => {
10080
10109
  return createZenStackPromise(async (txClient) => {
10081
10110
  let proceed = /* @__PURE__ */ __name(async (_args) => {
@@ -10339,30 +10368,62 @@ var EXT_RESULT_OPERATIONS = /* @__PURE__ */ new Set([
10339
10368
  "upsert",
10340
10369
  "delete"
10341
10370
  ]);
10342
- function hasExtResultFieldDefs(plugins) {
10343
- return plugins.some((p) => p.result && Object.keys(p.result).length > 0);
10371
+ function getSchemaExtResultConfig(schema) {
10372
+ const config = schema.plugins?.extResult;
10373
+ return config && typeof config === "object" ? config : void 0;
10374
+ }
10375
+ __name(getSchemaExtResultConfig, "getSchemaExtResultConfig");
10376
+ function getExtResultModelConfigs(schema, model, plugins) {
10377
+ const configs = [];
10378
+ const keys = [
10379
+ (0, import_common_helpers15.lowerCaseFirst)(model),
10380
+ model
10381
+ ];
10382
+ const schemaConfig = getSchemaExtResultConfig(schema);
10383
+ for (const key of keys) {
10384
+ const modelConfig = schemaConfig?.[key];
10385
+ if (modelConfig && typeof modelConfig === "object") {
10386
+ configs.push({
10387
+ owner: "schema",
10388
+ config: modelConfig
10389
+ });
10390
+ break;
10391
+ }
10392
+ }
10393
+ for (const plugin of plugins) {
10394
+ const resultConfig = plugin.result;
10395
+ for (const key of keys) {
10396
+ const modelConfig = resultConfig?.[key];
10397
+ if (modelConfig && typeof modelConfig === "object") {
10398
+ configs.push({
10399
+ owner: `plugin "${plugin.id}"`,
10400
+ config: modelConfig
10401
+ });
10402
+ break;
10403
+ }
10404
+ }
10405
+ }
10406
+ return configs;
10407
+ }
10408
+ __name(getExtResultModelConfigs, "getExtResultModelConfigs");
10409
+ function hasExtResultFieldDefs(schema, plugins) {
10410
+ return !!getSchemaExtResultConfig(schema) || plugins.some((p) => p.result && Object.keys(p.result).length > 0);
10344
10411
  }
10345
10412
  __name(hasExtResultFieldDefs, "hasExtResultFieldDefs");
10346
10413
  function collectExtResultFieldDefs(model, schema, plugins) {
10347
10414
  const defs = /* @__PURE__ */ new Map();
10348
- for (const plugin of plugins) {
10349
- const resultConfig = plugin.result;
10350
- if (resultConfig) {
10351
- const modelConfig = resultConfig[(0, import_common_helpers15.lowerCaseFirst)(model)];
10352
- if (modelConfig) {
10353
- for (const [fieldName, fieldDef] of Object.entries(modelConfig)) {
10354
- if (getField(schema, model, fieldName)) {
10355
- throw new Error(`Plugin "${plugin.id}" registers ext result field "${fieldName}" on model "${model}" which conflicts with an existing model field`);
10356
- }
10357
- for (const needField of Object.keys(fieldDef.needs ?? {})) {
10358
- const needDef = getField(schema, model, needField);
10359
- if (!needDef || needDef.relation) {
10360
- throw new Error(`Plugin "${plugin.id}" registers ext result field "${fieldName}" on model "${model}" with invalid need "${needField}"`);
10361
- }
10362
- }
10363
- defs.set(fieldName, fieldDef);
10415
+ for (const { owner, config } of getExtResultModelConfigs(schema, model, plugins)) {
10416
+ for (const [fieldName, fieldDef] of Object.entries(config)) {
10417
+ if (getField(schema, model, fieldName)) {
10418
+ throw new Error(`${owner} registers ext result field "${fieldName}" on model "${model}" which conflicts with an existing model field`);
10419
+ }
10420
+ for (const needField of Object.keys(fieldDef.needs ?? {})) {
10421
+ const needDef = getField(schema, model, needField);
10422
+ if (!needDef || needDef.relation) {
10423
+ throw new Error(`${owner} registers ext result field "${fieldName}" on model "${model}" with invalid need "${needField}"`);
10364
10424
  }
10365
10425
  }
10426
+ defs.set(fieldName, fieldDef);
10366
10427
  }
10367
10428
  }
10368
10429
  return defs;