prisma-guard 1.3.0 → 1.4.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.
- package/dist/generator/index.js +1 -1
- package/dist/generator/index.js.map +1 -1
- package/dist/runtime/index.cjs +66 -9
- package/dist/runtime/index.cjs.map +1 -1
- package/dist/runtime/index.d.cts +4 -3
- package/dist/runtime/index.d.ts +4 -3
- package/dist/runtime/index.js +66 -9
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
package/dist/runtime/index.d.cts
CHANGED
|
@@ -49,11 +49,12 @@ type ModelOpts = {
|
|
|
49
49
|
});
|
|
50
50
|
type QueryMethod = 'findMany' | 'findFirst' | 'findFirstOrThrow' | 'findUnique' | 'findUniqueOrThrow' | 'count' | 'aggregate' | 'groupBy';
|
|
51
51
|
type MutationMethod = 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany';
|
|
52
|
+
type OrderByFieldConfig = true | Record<string, true>;
|
|
52
53
|
interface ShapeConfig {
|
|
53
54
|
where?: Record<string, unknown>;
|
|
54
55
|
include?: Record<string, true | NestedIncludeArgs>;
|
|
55
56
|
select?: Record<string, true | NestedSelectArgs>;
|
|
56
|
-
orderBy?: Record<string,
|
|
57
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
57
58
|
cursor?: Record<string, true>;
|
|
58
59
|
take?: {
|
|
59
60
|
max: number;
|
|
@@ -73,7 +74,7 @@ interface NestedIncludeArgs {
|
|
|
73
74
|
where?: Record<string, unknown>;
|
|
74
75
|
include?: Record<string, true | NestedIncludeArgs>;
|
|
75
76
|
select?: Record<string, true | NestedSelectArgs>;
|
|
76
|
-
orderBy?: Record<string,
|
|
77
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
77
78
|
cursor?: Record<string, true>;
|
|
78
79
|
take?: {
|
|
79
80
|
max: number;
|
|
@@ -84,7 +85,7 @@ interface NestedIncludeArgs {
|
|
|
84
85
|
interface NestedSelectArgs {
|
|
85
86
|
select?: Record<string, true | NestedSelectArgs>;
|
|
86
87
|
where?: Record<string, unknown>;
|
|
87
|
-
orderBy?: Record<string,
|
|
88
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
88
89
|
cursor?: Record<string, true>;
|
|
89
90
|
take?: {
|
|
90
91
|
max: number;
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -49,11 +49,12 @@ type ModelOpts = {
|
|
|
49
49
|
});
|
|
50
50
|
type QueryMethod = 'findMany' | 'findFirst' | 'findFirstOrThrow' | 'findUnique' | 'findUniqueOrThrow' | 'count' | 'aggregate' | 'groupBy';
|
|
51
51
|
type MutationMethod = 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany';
|
|
52
|
+
type OrderByFieldConfig = true | Record<string, true>;
|
|
52
53
|
interface ShapeConfig {
|
|
53
54
|
where?: Record<string, unknown>;
|
|
54
55
|
include?: Record<string, true | NestedIncludeArgs>;
|
|
55
56
|
select?: Record<string, true | NestedSelectArgs>;
|
|
56
|
-
orderBy?: Record<string,
|
|
57
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
57
58
|
cursor?: Record<string, true>;
|
|
58
59
|
take?: {
|
|
59
60
|
max: number;
|
|
@@ -73,7 +74,7 @@ interface NestedIncludeArgs {
|
|
|
73
74
|
where?: Record<string, unknown>;
|
|
74
75
|
include?: Record<string, true | NestedIncludeArgs>;
|
|
75
76
|
select?: Record<string, true | NestedSelectArgs>;
|
|
76
|
-
orderBy?: Record<string,
|
|
77
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
77
78
|
cursor?: Record<string, true>;
|
|
78
79
|
take?: {
|
|
79
80
|
max: number;
|
|
@@ -84,7 +85,7 @@ interface NestedIncludeArgs {
|
|
|
84
85
|
interface NestedSelectArgs {
|
|
85
86
|
select?: Record<string, true | NestedSelectArgs>;
|
|
86
87
|
where?: Record<string, unknown>;
|
|
87
|
-
orderBy?: Record<string,
|
|
88
|
+
orderBy?: Record<string, OrderByFieldConfig>;
|
|
88
89
|
cursor?: Record<string, true>;
|
|
89
90
|
take?: {
|
|
90
91
|
max: number;
|
package/dist/runtime/index.js
CHANGED
|
@@ -1254,23 +1254,80 @@ function requireConfigTrue(config, context) {
|
|
|
1254
1254
|
}
|
|
1255
1255
|
}
|
|
1256
1256
|
function createArgsBuilder(typeMap, enumMap, uniqueMap, scalarBase) {
|
|
1257
|
+
const sortEnum = z5.enum(["asc", "desc"]);
|
|
1258
|
+
const nullsEnum = z5.enum(["first", "last"]);
|
|
1259
|
+
const sortWithNulls = z5.object({ sort: sortEnum, nulls: nullsEnum.optional() }).strict();
|
|
1260
|
+
const scalarOrderSchema = z5.union([sortEnum, sortWithNulls]);
|
|
1261
|
+
function validateScalarOrderByField(fieldName, model, modelFields) {
|
|
1262
|
+
const fieldMeta = modelFields[fieldName];
|
|
1263
|
+
if (!fieldMeta)
|
|
1264
|
+
throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
|
|
1265
|
+
if (fieldMeta.isRelation)
|
|
1266
|
+
throw new ShapeError(`Relation field "${fieldName}" in orderBy requires a nested config object, not true`);
|
|
1267
|
+
if (fieldMeta.type === "Json")
|
|
1268
|
+
throw new ShapeError(`Json field "${fieldName}" cannot be used in orderBy`);
|
|
1269
|
+
if (fieldMeta.isList)
|
|
1270
|
+
throw new ShapeError(`List field "${fieldName}" cannot be used in orderBy`);
|
|
1271
|
+
}
|
|
1257
1272
|
function buildOrderBySchema(model, orderByConfig) {
|
|
1258
1273
|
const modelFields = typeMap[model];
|
|
1259
1274
|
if (!modelFields)
|
|
1260
1275
|
throw new ShapeError(`Unknown model: ${model}`);
|
|
1261
|
-
requireConfigTrue(orderByConfig, `orderBy on model "${model}"`);
|
|
1262
1276
|
const fieldSchemas = {};
|
|
1263
|
-
for (const fieldName of Object.
|
|
1277
|
+
for (const [fieldName, config] of Object.entries(orderByConfig)) {
|
|
1264
1278
|
const fieldMeta = modelFields[fieldName];
|
|
1265
1279
|
if (!fieldMeta)
|
|
1266
1280
|
throw new ShapeError(`Unknown field "${fieldName}" on model "${model}"`);
|
|
1267
|
-
if (
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1281
|
+
if (config === true) {
|
|
1282
|
+
validateScalarOrderByField(fieldName, model, modelFields);
|
|
1283
|
+
fieldSchemas[fieldName] = scalarOrderSchema.optional();
|
|
1284
|
+
continue;
|
|
1285
|
+
}
|
|
1286
|
+
if (typeof config !== "object" || config === null) {
|
|
1287
|
+
throw new ShapeError(`Invalid orderBy config for "${fieldName}" on model "${model}": expected true or a nested config object`);
|
|
1288
|
+
}
|
|
1289
|
+
if (!fieldMeta.isRelation) {
|
|
1290
|
+
throw new ShapeError(`Scalar field "${fieldName}" in orderBy does not accept nested config`);
|
|
1291
|
+
}
|
|
1292
|
+
if (Object.keys(config).length === 0) {
|
|
1293
|
+
throw new ShapeError(`Empty orderBy config for relation "${fieldName}" on model "${model}". Define at least one nested field.`);
|
|
1294
|
+
}
|
|
1295
|
+
if (fieldMeta.isList) {
|
|
1296
|
+
const relKeys = Object.keys(config);
|
|
1297
|
+
if (relKeys.length !== 1 || relKeys[0] !== "_count") {
|
|
1298
|
+
throw new ShapeError(`To-many relation "${fieldName}" in orderBy only supports { _count: true }`);
|
|
1299
|
+
}
|
|
1300
|
+
if (config._count !== true) {
|
|
1301
|
+
throw new ShapeError(`_count in orderBy for "${fieldName}" must be true`);
|
|
1302
|
+
}
|
|
1303
|
+
fieldSchemas[fieldName] = z5.object({ _count: sortEnum }).strict().optional();
|
|
1304
|
+
continue;
|
|
1305
|
+
}
|
|
1306
|
+
const relatedModel = fieldMeta.type;
|
|
1307
|
+
const relatedFields = typeMap[relatedModel];
|
|
1308
|
+
if (!relatedFields)
|
|
1309
|
+
throw new ShapeError(`Related model "${relatedModel}" not found in type map`);
|
|
1310
|
+
const nestedSchemas = {};
|
|
1311
|
+
for (const [nestedField, nestedVal] of Object.entries(config)) {
|
|
1312
|
+
if (nestedVal !== true) {
|
|
1313
|
+
throw new ShapeError(`Nested orderBy field "${nestedField}" on relation "${fieldName}" must be true`);
|
|
1314
|
+
}
|
|
1315
|
+
const nestedMeta = relatedFields[nestedField];
|
|
1316
|
+
if (!nestedMeta)
|
|
1317
|
+
throw new ShapeError(`Unknown field "${nestedField}" on model "${relatedModel}" in orderBy`);
|
|
1318
|
+
if (nestedMeta.isRelation)
|
|
1319
|
+
throw new ShapeError(`Nested relation "${nestedField}" in orderBy on "${fieldName}" is not supported`);
|
|
1320
|
+
if (nestedMeta.type === "Json")
|
|
1321
|
+
throw new ShapeError(`Json field "${nestedField}" cannot be used in orderBy`);
|
|
1322
|
+
if (nestedMeta.isList)
|
|
1323
|
+
throw new ShapeError(`List field "${nestedField}" cannot be used in orderBy`);
|
|
1324
|
+
nestedSchemas[nestedField] = scalarOrderSchema.optional();
|
|
1325
|
+
}
|
|
1326
|
+
const nestedKeys = Object.keys(nestedSchemas);
|
|
1327
|
+
fieldSchemas[fieldName] = z5.object(nestedSchemas).strict().refine(
|
|
1328
|
+
(v) => nestedKeys.some((k) => v[k] !== void 0),
|
|
1329
|
+
{ message: `orderBy for relation "${fieldName}" must specify at least one field` }
|
|
1330
|
+
).optional();
|
|
1274
1331
|
}
|
|
1275
1332
|
const fieldKeys = Object.keys(fieldSchemas);
|
|
1276
1333
|
const singleSchema = z5.object(fieldSchemas).strict().refine(
|