@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.js CHANGED
@@ -787,17 +787,32 @@ var BaseCrudDialect = class {
787
787
  take = -take;
788
788
  }
789
789
  result = this.buildSkipTake(result, skip, take);
790
- result = this.buildOrderBy(result, model, modelAlias, args.orderBy, negateOrderBy, take);
790
+ let distinctFields = [];
791
791
  if ("distinct" in args && args.distinct) {
792
- const distinct = ensureArray(args.distinct);
792
+ distinctFields = ensureArray(args.distinct);
793
793
  if (this.supportsDistinctOn) {
794
- result = result.distinctOn(distinct.map((f) => this.eb.ref(`${modelAlias}.${f}`)));
794
+ result = result.distinctOn(distinctFields.map((f) => this.eb.ref(`${modelAlias}.${f}`)));
795
795
  } else {
796
796
  throw createNotSupportedError(`"distinct" is not supported by "${this.schema.provider.type}" provider`);
797
797
  }
798
798
  }
799
+ let effectiveOrderBy = args.orderBy;
800
+ if (distinctFields.length > 0 && this.supportsDistinctOn) {
801
+ const existingOrderBy = enumerate(args.orderBy).filter((o) => Object.keys(o).length > 0);
802
+ const alreadySatisfied = distinctFields.every((f, i) => i < existingOrderBy.length && Object.keys(existingOrderBy[i])[0] === f);
803
+ if (existingOrderBy.length > 0 && !alreadySatisfied) {
804
+ const prependedOrderBy = distinctFields.map((f) => ({
805
+ [f]: "asc"
806
+ }));
807
+ effectiveOrderBy = [
808
+ ...prependedOrderBy,
809
+ ...existingOrderBy
810
+ ];
811
+ }
812
+ }
813
+ result = this.buildOrderBy(result, model, modelAlias, effectiveOrderBy, negateOrderBy, take);
799
814
  if (args.cursor) {
800
- result = this.buildCursorFilter(model, result, args.cursor, args.orderBy, negateOrderBy, modelAlias);
815
+ result = this.buildCursorFilter(model, result, args.cursor, effectiveOrderBy, negateOrderBy, modelAlias);
801
816
  }
802
817
  return result;
803
818
  }
@@ -5755,6 +5770,9 @@ var ZodSchemaFactory = class {
5755
5770
  z.literal("asc"),
5756
5771
  z.literal("desc")
5757
5772
  ]);
5773
+ const refineAtMostOneKey = /* @__PURE__ */ __name((s) => s.refine((v) => Object.keys(v).length <= 1, {
5774
+ message: "Each orderBy element must have at most one key"
5775
+ }), "refineAtMostOneKey");
5758
5776
  const nextOpts = this.nextOptions(options);
5759
5777
  for (const [field, fieldDef] of this.getModelFields(model)) {
5760
5778
  if (fieldDef.relation) {
@@ -5762,9 +5780,9 @@ var ZodSchemaFactory = class {
5762
5780
  fields[field] = z.lazy(() => {
5763
5781
  let relationOrderBy = this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation, nextOpts);
5764
5782
  if (fieldDef.array) {
5765
- relationOrderBy = relationOrderBy.extend({
5783
+ relationOrderBy = refineAtMostOneKey(relationOrderBy.safeExtend({
5766
5784
  _count: sort
5767
- });
5785
+ }));
5768
5786
  }
5769
5787
  return relationOrderBy.optional();
5770
5788
  });
@@ -5798,7 +5816,7 @@ var ZodSchemaFactory = class {
5798
5816
  fields[agg] = z.lazy(() => this.makeOrderBySchema(model, true, false, options).optional());
5799
5817
  }
5800
5818
  }
5801
- return z.strictObject(fields);
5819
+ return refineAtMostOneKey(z.strictObject(fields));
5802
5820
  }
5803
5821
  makeDistinctSchema(model) {
5804
5822
  const nonRelationFields = this.getModelFields(model).filter(([, def]) => !def.relation).map(([name]) => name);