prisma-guard 1.5.0 → 1.6.0

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.
@@ -705,20 +705,28 @@ function mergeUniqueWhereForced(where, forced) {
705
705
  }
706
706
  function applyBuiltShape(built, body, isUniqueMethod) {
707
707
  let parseable = body;
708
- if (built.forcedOnlyWhereKeys.size > 0 && isPlainObject(body)) {
708
+ if (isPlainObject(body)) {
709
709
  const bodyObj = body;
710
710
  if (isPlainObject(bodyObj.where)) {
711
711
  const where = { ...bodyObj.where };
712
- let stripped = false;
713
- for (const key of built.forcedOnlyWhereKeys) {
714
- if (key in where) {
715
- delete where[key];
716
- stripped = true;
712
+ let modified = false;
713
+ if (built.forcedOnlyWhereKeys.size > 0) {
714
+ for (const key of built.forcedOnlyWhereKeys) {
715
+ if (key in where) {
716
+ delete where[key];
717
+ modified = true;
718
+ }
717
719
  }
718
720
  }
719
- if (stripped) {
721
+ if (Object.keys(where).length === 0 && hasWhereForced(built.forcedWhere)) {
722
+ const { where: _, ...rest } = bodyObj;
723
+ parseable = rest;
724
+ } else if (modified) {
720
725
  parseable = { ...bodyObj, where };
721
726
  }
727
+ } else if ("where" in bodyObj && hasWhereForced(built.forcedWhere) && !built.zodSchema.shape.where) {
728
+ const { where: _, ...rest } = bodyObj;
729
+ parseable = rest;
722
730
  }
723
731
  }
724
732
  const validated = built.zodSchema.parse(parseable);
@@ -2106,9 +2114,11 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2106
2114
  }
2107
2115
  if (method === "count" && shape.include)
2108
2116
  throw new ShapeError('count does not support "include"');
2109
- if (method === "groupBy" && shape.orderBy) {
2117
+ if (method === "groupBy" && shape.orderBy && shape.orderBy !== true) {
2110
2118
  const bySet = new Set(shape.by);
2111
2119
  for (const fieldName of Object.keys(shape.orderBy)) {
2120
+ if (fieldName === "_count")
2121
+ continue;
2112
2122
  if (!bySet.has(fieldName)) {
2113
2123
  throw new ShapeError(
2114
2124
  `orderBy field "${fieldName}" must be included in "by" for groupBy`
@@ -2186,11 +2196,29 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2186
2196
  forcedSelectCountWhere = result.forcedCountWhere;
2187
2197
  }
2188
2198
  }
2189
- if (shape.orderBy)
2190
- schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2191
- model,
2192
- shape.orderBy
2193
- );
2199
+ if (shape.orderBy) {
2200
+ if (method === "groupBy" && shape.orderBy === true && shape.by) {
2201
+ const sortEnum = z7.enum(["asc", "desc"]);
2202
+ const groupByOrderFields = {};
2203
+ for (const field of shape.by) {
2204
+ groupByOrderFields[field] = sortEnum.optional();
2205
+ }
2206
+ groupByOrderFields["_count"] = sortEnum.optional();
2207
+ const fieldKeys = Object.keys(groupByOrderFields);
2208
+ const singleSchema = z7.object(groupByOrderFields).strict().refine(
2209
+ (v) => fieldKeys.some(
2210
+ (k) => v[k] !== void 0
2211
+ ),
2212
+ { message: "orderBy must specify at least one field" }
2213
+ );
2214
+ schemaFields["orderBy"] = z7.union([singleSchema, z7.array(singleSchema).min(1)]).optional();
2215
+ } else {
2216
+ schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2217
+ model,
2218
+ shape.orderBy
2219
+ );
2220
+ }
2221
+ }
2194
2222
  if (shape.cursor)
2195
2223
  schemaFields["cursor"] = argsBuilder.buildCursorSchema(
2196
2224
  model,