prisma-guard 1.6.0 → 1.7.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.
@@ -54,7 +54,7 @@ interface ShapeConfig {
54
54
  where?: Record<string, unknown>;
55
55
  include?: Record<string, true | NestedIncludeArgs>;
56
56
  select?: Record<string, true | NestedSelectArgs>;
57
- orderBy?: Record<string, OrderByFieldConfig>;
57
+ orderBy?: true | Record<string, OrderByFieldConfig>;
58
58
  cursor?: Record<string, true>;
59
59
  take?: {
60
60
  max: number;
@@ -54,7 +54,7 @@ interface ShapeConfig {
54
54
  where?: Record<string, unknown>;
55
55
  include?: Record<string, true | NestedIncludeArgs>;
56
56
  select?: Record<string, true | NestedSelectArgs>;
57
- orderBy?: Record<string, OrderByFieldConfig>;
57
+ orderBy?: true | Record<string, OrderByFieldConfig>;
58
58
  cursor?: Record<string, true>;
59
59
  take?: {
60
60
  max: number;
@@ -705,28 +705,25 @@ function mergeUniqueWhereForced(where, forced) {
705
705
  }
706
706
  function applyBuiltShape(built, body, isUniqueMethod) {
707
707
  let parseable = body;
708
+ const hasWhereInSchema = "where" in built.zodSchema.shape;
708
709
  if (isPlainObject(body)) {
709
710
  const bodyObj = body;
710
- if (isPlainObject(bodyObj.where)) {
711
- const where = { ...bodyObj.where };
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
- }
719
- }
720
- }
721
- if (Object.keys(where).length === 0 && hasWhereForced(built.forcedWhere)) {
711
+ if ("where" in bodyObj) {
712
+ if (!hasWhereInSchema) {
722
713
  const { where: _, ...rest } = bodyObj;
723
714
  parseable = rest;
724
- } else if (modified) {
725
- parseable = { ...bodyObj, where };
715
+ } else if (built.forcedOnlyWhereKeys.size > 0 && isPlainObject(bodyObj.where)) {
716
+ const where = { ...bodyObj.where };
717
+ for (const key of built.forcedOnlyWhereKeys) {
718
+ delete where[key];
719
+ }
720
+ if (Object.keys(where).length === 0 && hasWhereForced(built.forcedWhere)) {
721
+ const { where: _, ...rest } = bodyObj;
722
+ parseable = rest;
723
+ } else {
724
+ parseable = { ...bodyObj, where };
725
+ }
726
726
  }
727
- } else if ("where" in bodyObj && hasWhereForced(built.forcedWhere) && !built.zodSchema.shape.where) {
728
- const { where: _, ...rest } = bodyObj;
729
- parseable = rest;
730
727
  }
731
728
  }
732
729
  const validated = built.zodSchema.parse(parseable);
@@ -2197,21 +2194,67 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2197
2194
  }
2198
2195
  }
2199
2196
  if (shape.orderBy) {
2200
- if (method === "groupBy" && shape.orderBy === true && shape.by) {
2197
+ if (method === "groupBy" && shape.by) {
2201
2198
  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();
2199
+ const bySet = new Set(shape.by);
2200
+ if (shape.orderBy === true) {
2201
+ const groupByOrderFields = {};
2202
+ for (const field of shape.by) {
2203
+ groupByOrderFields[field] = sortEnum.optional();
2204
+ }
2205
+ groupByOrderFields["_count"] = sortEnum.optional();
2206
+ const fieldKeys = Object.keys(groupByOrderFields);
2207
+ const singleSchema = z7.object(groupByOrderFields).strict().refine(
2208
+ (v) => fieldKeys.some(
2209
+ (k) => v[k] !== void 0
2210
+ ),
2211
+ { message: "orderBy must specify at least one field" }
2212
+ );
2213
+ schemaFields["orderBy"] = z7.union([singleSchema, z7.array(singleSchema).min(1)]).optional();
2214
+ } else {
2215
+ const groupByOrderFields = {};
2216
+ for (const [fieldName, config] of Object.entries(shape.orderBy)) {
2217
+ if (fieldName === "_count") {
2218
+ if (config === true) {
2219
+ groupByOrderFields["_count"] = sortEnum.optional();
2220
+ } else if (typeof config === "object" && config !== null) {
2221
+ const countFields = {};
2222
+ for (const countField of Object.keys(config)) {
2223
+ if (!bySet.has(countField)) {
2224
+ throw new ShapeError(
2225
+ `orderBy _count field "${countField}" must be included in "by" for groupBy`
2226
+ );
2227
+ }
2228
+ countFields[countField] = sortEnum.optional();
2229
+ }
2230
+ const countKeys = Object.keys(countFields);
2231
+ groupByOrderFields["_count"] = z7.object(countFields).strict().refine(
2232
+ (v) => countKeys.some(
2233
+ (k) => v[k] !== void 0
2234
+ ),
2235
+ {
2236
+ message: "orderBy._count must specify at least one field"
2237
+ }
2238
+ ).optional();
2239
+ }
2240
+ continue;
2241
+ }
2242
+ if (!bySet.has(fieldName)) {
2243
+ throw new ShapeError(
2244
+ `orderBy field "${fieldName}" must be included in "by" for groupBy`
2245
+ );
2246
+ }
2247
+ groupByOrderFields[fieldName] = sortEnum.optional();
2248
+ }
2249
+ const fieldKeys = Object.keys(groupByOrderFields);
2250
+ const singleSchema = z7.object(groupByOrderFields).strict().refine(
2251
+ (v) => fieldKeys.some(
2252
+ (k) => v[k] !== void 0
2253
+ ),
2254
+ { message: "orderBy must specify at least one field" }
2255
+ );
2256
+ schemaFields["orderBy"] = z7.union([singleSchema, z7.array(singleSchema).min(1)]).optional();
2257
+ }
2215
2258
  } else {
2216
2259
  schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2217
2260
  model,