@zenstackhq/orm 3.5.5 → 3.5.6

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
@@ -833,17 +833,32 @@ var BaseCrudDialect = class {
833
833
  take = -take;
834
834
  }
835
835
  result = this.buildSkipTake(result, skip, take);
836
- result = this.buildOrderBy(result, model, modelAlias, args.orderBy, negateOrderBy, take);
836
+ let distinctFields = [];
837
837
  if ("distinct" in args && args.distinct) {
838
- const distinct = ensureArray(args.distinct);
838
+ distinctFields = ensureArray(args.distinct);
839
839
  if (this.supportsDistinctOn) {
840
- result = result.distinctOn(distinct.map((f) => this.eb.ref(`${modelAlias}.${f}`)));
840
+ result = result.distinctOn(distinctFields.map((f) => this.eb.ref(`${modelAlias}.${f}`)));
841
841
  } else {
842
842
  throw createNotSupportedError(`"distinct" is not supported by "${this.schema.provider.type}" provider`);
843
843
  }
844
844
  }
845
+ let effectiveOrderBy = args.orderBy;
846
+ if (distinctFields.length > 0 && this.supportsDistinctOn) {
847
+ const existingOrderBy = (0, import_common_helpers3.enumerate)(args.orderBy).filter((o) => Object.keys(o).length > 0);
848
+ const alreadySatisfied = distinctFields.every((f, i) => i < existingOrderBy.length && Object.keys(existingOrderBy[i])[0] === f);
849
+ if (existingOrderBy.length > 0 && !alreadySatisfied) {
850
+ const prependedOrderBy = distinctFields.map((f) => ({
851
+ [f]: "asc"
852
+ }));
853
+ effectiveOrderBy = [
854
+ ...prependedOrderBy,
855
+ ...existingOrderBy
856
+ ];
857
+ }
858
+ }
859
+ result = this.buildOrderBy(result, model, modelAlias, effectiveOrderBy, negateOrderBy, take);
845
860
  if (args.cursor) {
846
- result = this.buildCursorFilter(model, result, args.cursor, args.orderBy, negateOrderBy, modelAlias);
861
+ result = this.buildCursorFilter(model, result, args.cursor, effectiveOrderBy, negateOrderBy, modelAlias);
847
862
  }
848
863
  return result;
849
864
  }
@@ -5801,6 +5816,9 @@ var ZodSchemaFactory = class {
5801
5816
  import_zod2.z.literal("asc"),
5802
5817
  import_zod2.z.literal("desc")
5803
5818
  ]);
5819
+ const refineAtMostOneKey = /* @__PURE__ */ __name((s) => s.refine((v) => Object.keys(v).length <= 1, {
5820
+ message: "Each orderBy element must have at most one key"
5821
+ }), "refineAtMostOneKey");
5804
5822
  const nextOpts = this.nextOptions(options);
5805
5823
  for (const [field, fieldDef] of this.getModelFields(model)) {
5806
5824
  if (fieldDef.relation) {
@@ -5808,9 +5826,9 @@ var ZodSchemaFactory = class {
5808
5826
  fields[field] = import_zod2.z.lazy(() => {
5809
5827
  let relationOrderBy = this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation, nextOpts);
5810
5828
  if (fieldDef.array) {
5811
- relationOrderBy = relationOrderBy.extend({
5829
+ relationOrderBy = refineAtMostOneKey(relationOrderBy.safeExtend({
5812
5830
  _count: sort
5813
- });
5831
+ }));
5814
5832
  }
5815
5833
  return relationOrderBy.optional();
5816
5834
  });
@@ -5844,7 +5862,7 @@ var ZodSchemaFactory = class {
5844
5862
  fields[agg] = import_zod2.z.lazy(() => this.makeOrderBySchema(model, true, false, options).optional());
5845
5863
  }
5846
5864
  }
5847
- return import_zod2.z.strictObject(fields);
5865
+ return refineAtMostOneKey(import_zod2.z.strictObject(fields));
5848
5866
  }
5849
5867
  makeDistinctSchema(model) {
5850
5868
  const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation).map(([name]) => name);