prisma-guard 1.6.1 → 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;
@@ -2194,21 +2194,67 @@ function createQueryBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
2194
2194
  }
2195
2195
  }
2196
2196
  if (shape.orderBy) {
2197
- if (method === "groupBy" && shape.orderBy === true && shape.by) {
2197
+ if (method === "groupBy" && shape.by) {
2198
2198
  const sortEnum = z7.enum(["asc", "desc"]);
2199
- const groupByOrderFields = {};
2200
- for (const field of shape.by) {
2201
- groupByOrderFields[field] = sortEnum.optional();
2202
- }
2203
- groupByOrderFields["_count"] = sortEnum.optional();
2204
- const fieldKeys = Object.keys(groupByOrderFields);
2205
- const singleSchema = z7.object(groupByOrderFields).strict().refine(
2206
- (v) => fieldKeys.some(
2207
- (k) => v[k] !== void 0
2208
- ),
2209
- { message: "orderBy must specify at least one field" }
2210
- );
2211
- 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
+ }
2212
2258
  } else {
2213
2259
  schemaFields["orderBy"] = argsBuilder.buildOrderBySchema(
2214
2260
  model,