prisma-guard 1.16.0 → 1.18.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/runtime/index.cjs +88 -15
- package/dist/runtime/index.cjs.map +1 -1
- package/dist/runtime/index.js +88 -15
- package/dist/runtime/index.js.map +1 -1
- package/package.json +1 -1
package/dist/runtime/index.js
CHANGED
|
@@ -210,7 +210,18 @@ var SCALAR_OPERATORS = {
|
|
|
210
210
|
BigInt: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
|
|
211
211
|
Boolean: /* @__PURE__ */ new Set(["equals", "not"]),
|
|
212
212
|
DateTime: /* @__PURE__ */ new Set(["equals", "not", "gt", "gte", "lt", "lte", "in", "notIn"]),
|
|
213
|
-
Bytes: /* @__PURE__ */ new Set([])
|
|
213
|
+
Bytes: /* @__PURE__ */ new Set([]),
|
|
214
|
+
Json: /* @__PURE__ */ new Set([
|
|
215
|
+
"equals",
|
|
216
|
+
"not",
|
|
217
|
+
"path",
|
|
218
|
+
"string_contains",
|
|
219
|
+
"string_starts_with",
|
|
220
|
+
"string_ends_with",
|
|
221
|
+
"array_contains",
|
|
222
|
+
"array_starts_with",
|
|
223
|
+
"array_ends_with"
|
|
224
|
+
])
|
|
214
225
|
};
|
|
215
226
|
var SCALAR_LIST_OPERATORS = /* @__PURE__ */ new Set([
|
|
216
227
|
"has",
|
|
@@ -229,6 +240,16 @@ var COMPARABLE_TYPES = /* @__PURE__ */ new Set([
|
|
|
229
240
|
"String",
|
|
230
241
|
"DateTime"
|
|
231
242
|
]);
|
|
243
|
+
var JSON_STRING_OPERATORS = /* @__PURE__ */ new Set([
|
|
244
|
+
"string_contains",
|
|
245
|
+
"string_starts_with",
|
|
246
|
+
"string_ends_with"
|
|
247
|
+
]);
|
|
248
|
+
var JSON_ARRAY_OPERATORS = /* @__PURE__ */ new Set([
|
|
249
|
+
"array_contains",
|
|
250
|
+
"array_starts_with",
|
|
251
|
+
"array_ends_with"
|
|
252
|
+
]);
|
|
232
253
|
function getSupportedOperators(fieldMeta) {
|
|
233
254
|
if (fieldMeta.isList)
|
|
234
255
|
return [...SCALAR_LIST_OPERATORS];
|
|
@@ -281,6 +302,24 @@ function createScalarListOperatorSchema(fieldMeta, operator, enumMap, scalarBase
|
|
|
281
302
|
}
|
|
282
303
|
return z2.preprocess(coerceToArray, z2.array(itemBase));
|
|
283
304
|
}
|
|
305
|
+
function createJsonOperatorSchema(fieldMeta, operator) {
|
|
306
|
+
const jsonValue = z2.unknown();
|
|
307
|
+
if (operator === "equals" || operator === "not") {
|
|
308
|
+
return !fieldMeta.isRequired ? z2.union([jsonValue, z2.null()]) : jsonValue;
|
|
309
|
+
}
|
|
310
|
+
if (operator === "path") {
|
|
311
|
+
return z2.preprocess(coerceToArray, z2.array(z2.string()).min(1));
|
|
312
|
+
}
|
|
313
|
+
if (JSON_STRING_OPERATORS.has(operator)) {
|
|
314
|
+
return z2.string();
|
|
315
|
+
}
|
|
316
|
+
if (JSON_ARRAY_OPERATORS.has(operator)) {
|
|
317
|
+
return jsonValue;
|
|
318
|
+
}
|
|
319
|
+
throw new ShapeError(
|
|
320
|
+
`Operator "${operator}" not supported for Json fields`
|
|
321
|
+
);
|
|
322
|
+
}
|
|
284
323
|
function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
|
|
285
324
|
if (fieldMeta.isList) {
|
|
286
325
|
return createScalarListOperatorSchema(
|
|
@@ -307,6 +346,15 @@ function createOperatorSchema(fieldMeta, operator, enumMap, scalarBase) {
|
|
|
307
346
|
const itemSchema = !fieldMeta.isRequired ? z2.union([enumSchema, z2.null()]) : enumSchema;
|
|
308
347
|
return z2.preprocess(coerceToArray, z2.array(itemSchema));
|
|
309
348
|
}
|
|
349
|
+
if (fieldMeta.type === "Json") {
|
|
350
|
+
const supportedOps2 = SCALAR_OPERATORS["Json"];
|
|
351
|
+
if (!supportedOps2 || !supportedOps2.has(operator)) {
|
|
352
|
+
throw new ShapeError(
|
|
353
|
+
`Operator "${operator}" not supported for type "Json"`
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
return createJsonOperatorSchema(fieldMeta, operator);
|
|
357
|
+
}
|
|
310
358
|
const supportedOps = SCALAR_OPERATORS[fieldMeta.type];
|
|
311
359
|
if (!supportedOps) {
|
|
312
360
|
throw new ShapeError(`Unknown scalar type for operator: ${fieldMeta.type}`);
|
|
@@ -1064,13 +1112,18 @@ function validateUniqueEquality(model, where, method, uniqueMap, typeMap) {
|
|
|
1064
1112
|
}
|
|
1065
1113
|
|
|
1066
1114
|
// src/runtime/query-builder-where.ts
|
|
1067
|
-
var UNSUPPORTED_WHERE_TYPES = /* @__PURE__ */ new Set(["
|
|
1115
|
+
var UNSUPPORTED_WHERE_TYPES = /* @__PURE__ */ new Set(["Bytes"]);
|
|
1068
1116
|
var STRING_MODE_OPS = /* @__PURE__ */ new Set([
|
|
1069
1117
|
"contains",
|
|
1070
1118
|
"startsWith",
|
|
1071
1119
|
"endsWith",
|
|
1072
1120
|
"equals"
|
|
1073
1121
|
]);
|
|
1122
|
+
var JSON_STRING_MODE_OPS = /* @__PURE__ */ new Set([
|
|
1123
|
+
"string_contains",
|
|
1124
|
+
"string_starts_with",
|
|
1125
|
+
"string_ends_with"
|
|
1126
|
+
]);
|
|
1074
1127
|
var MAX_WHERE_DEPTH = 10;
|
|
1075
1128
|
function safeStringify(v) {
|
|
1076
1129
|
if (typeof v === "bigint")
|
|
@@ -1166,6 +1219,13 @@ function mergeRelationForcedMaps(target, source) {
|
|
|
1166
1219
|
}
|
|
1167
1220
|
}
|
|
1168
1221
|
}
|
|
1222
|
+
function isModeCompatibleOp(fieldType, op) {
|
|
1223
|
+
if (fieldType === "String")
|
|
1224
|
+
return STRING_MODE_OPS.has(op);
|
|
1225
|
+
if (fieldType === "Json")
|
|
1226
|
+
return JSON_STRING_MODE_OPS.has(op);
|
|
1227
|
+
return false;
|
|
1228
|
+
}
|
|
1169
1229
|
function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
1170
1230
|
function buildWhereSchema(model, whereConfig, depth) {
|
|
1171
1231
|
const currentDepth = depth ?? 0;
|
|
@@ -1368,7 +1428,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1368
1428
|
const opSchemas = {};
|
|
1369
1429
|
const fieldForced = {};
|
|
1370
1430
|
let hasClientOps = false;
|
|
1371
|
-
let
|
|
1431
|
+
let hasModeCompatibleOp = false;
|
|
1372
1432
|
const clientOpKeys = [];
|
|
1373
1433
|
let modeConfigValue = void 0;
|
|
1374
1434
|
let hasModeConfig = false;
|
|
@@ -1387,8 +1447,8 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1387
1447
|
).optional();
|
|
1388
1448
|
hasClientOps = true;
|
|
1389
1449
|
clientOpKeys.push(op);
|
|
1390
|
-
if (fieldMeta.
|
|
1391
|
-
|
|
1450
|
+
if (!fieldMeta.isList && isModeCompatibleOp(fieldMeta.type, op)) {
|
|
1451
|
+
hasModeCompatibleOp = true;
|
|
1392
1452
|
}
|
|
1393
1453
|
} else {
|
|
1394
1454
|
const actualOpValue = isForcedValue(opValue) ? opValue.value : opValue;
|
|
@@ -1407,15 +1467,15 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1407
1467
|
);
|
|
1408
1468
|
}
|
|
1409
1469
|
fieldForced[op] = parsed;
|
|
1410
|
-
if (fieldMeta.
|
|
1411
|
-
|
|
1470
|
+
if (!fieldMeta.isList && isModeCompatibleOp(fieldMeta.type, op)) {
|
|
1471
|
+
hasModeCompatibleOp = true;
|
|
1412
1472
|
}
|
|
1413
1473
|
}
|
|
1414
1474
|
}
|
|
1415
1475
|
if (!hasClientOps && Object.keys(fieldForced).length === 0) {
|
|
1416
1476
|
if (hasModeConfig) {
|
|
1417
1477
|
throw new ShapeError(
|
|
1418
|
-
`Where field "${fieldName}" on model "${model}" has "mode" but no operators. Add at least one operator
|
|
1478
|
+
`Where field "${fieldName}" on model "${model}" has "mode" but no operators. Add at least one compatible operator.`
|
|
1419
1479
|
);
|
|
1420
1480
|
}
|
|
1421
1481
|
throw new ShapeError(
|
|
@@ -1423,9 +1483,9 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1423
1483
|
);
|
|
1424
1484
|
}
|
|
1425
1485
|
if (hasModeConfig) {
|
|
1426
|
-
if (!
|
|
1486
|
+
if (!hasModeCompatibleOp) {
|
|
1427
1487
|
throw new ShapeError(
|
|
1428
|
-
`"mode" on where field "${fieldName}" on model "${model}" requires a compatible
|
|
1488
|
+
`"mode" on where field "${fieldName}" on model "${model}" requires a compatible operator (String: contains, startsWith, endsWith, equals; Json: string_contains, string_starts_with, string_ends_with)`
|
|
1429
1489
|
);
|
|
1430
1490
|
}
|
|
1431
1491
|
if (modeConfigValue === true) {
|
|
@@ -1443,7 +1503,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1443
1503
|
}
|
|
1444
1504
|
fieldForced["mode"] = parsed;
|
|
1445
1505
|
}
|
|
1446
|
-
} else if (
|
|
1506
|
+
} else if (hasModeCompatibleOp) {
|
|
1447
1507
|
opSchemas["mode"] = z4.enum(["default", "insensitive"]).optional();
|
|
1448
1508
|
}
|
|
1449
1509
|
if (hasClientOps) {
|
|
@@ -1456,7 +1516,7 @@ function createWhereBuilder(typeMap, enumMap, scalarBase) {
|
|
|
1456
1516
|
message: `At least one operator required for where field "${fieldName}"`
|
|
1457
1517
|
}
|
|
1458
1518
|
);
|
|
1459
|
-
if ("equals" in opSchemas) {
|
|
1519
|
+
if ("equals" in opSchemas && fieldMeta.type !== "Json") {
|
|
1460
1520
|
const equalsBase = createOperatorSchema(
|
|
1461
1521
|
fieldMeta,
|
|
1462
1522
|
"equals",
|
|
@@ -3703,6 +3763,17 @@ var PROJECTION_MUTATION_METHODS = /* @__PURE__ */ new Set([
|
|
|
3703
3763
|
"updateManyAndReturn"
|
|
3704
3764
|
]);
|
|
3705
3765
|
var BATCH_CREATE_METHODS = /* @__PURE__ */ new Set(["createMany", "createManyAndReturn"]);
|
|
3766
|
+
function normalizeUniqueWhere(where) {
|
|
3767
|
+
const result = {};
|
|
3768
|
+
for (const [key, value] of Object.entries(where)) {
|
|
3769
|
+
if (key !== "AND" && key !== "OR" && key !== "NOT" && isPlainObject(value) && "equals" in value && Object.keys(value).length === 1) {
|
|
3770
|
+
result[key] = value.equals;
|
|
3771
|
+
} else {
|
|
3772
|
+
result[key] = value;
|
|
3773
|
+
}
|
|
3774
|
+
}
|
|
3775
|
+
return result;
|
|
3776
|
+
}
|
|
3706
3777
|
function buildDefaultSelectInput(config) {
|
|
3707
3778
|
const result = {};
|
|
3708
3779
|
for (const [key, value] of Object.entries(config)) {
|
|
@@ -4312,7 +4383,7 @@ function createModelGuardExtension(config) {
|
|
|
4312
4383
|
dataSchema,
|
|
4313
4384
|
method
|
|
4314
4385
|
);
|
|
4315
|
-
|
|
4386
|
+
let where = isUniqueWhere ? requireWhere(
|
|
4316
4387
|
resolved.shape,
|
|
4317
4388
|
resolved.body.where,
|
|
4318
4389
|
method,
|
|
@@ -4333,6 +4404,7 @@ function createModelGuardExtension(config) {
|
|
|
4333
4404
|
}
|
|
4334
4405
|
if (isUniqueWhere) {
|
|
4335
4406
|
validateResolvedUniqueWhere(modelName, where, method, uniqueMap);
|
|
4407
|
+
where = normalizeUniqueWhere(where);
|
|
4336
4408
|
}
|
|
4337
4409
|
const args = { data, where };
|
|
4338
4410
|
if (supportsProjection) {
|
|
@@ -4371,7 +4443,7 @@ function createModelGuardExtension(config) {
|
|
|
4371
4443
|
);
|
|
4372
4444
|
}
|
|
4373
4445
|
maybeValidateUniqueWhere(modelName, resolved.shape, method);
|
|
4374
|
-
|
|
4446
|
+
let where = isUniqueWhere ? requireWhere(
|
|
4375
4447
|
resolved.shape,
|
|
4376
4448
|
resolved.body.where,
|
|
4377
4449
|
method,
|
|
@@ -4392,6 +4464,7 @@ function createModelGuardExtension(config) {
|
|
|
4392
4464
|
}
|
|
4393
4465
|
if (isUniqueWhere) {
|
|
4394
4466
|
validateResolvedUniqueWhere(modelName, where, method, uniqueMap);
|
|
4467
|
+
where = normalizeUniqueWhere(where);
|
|
4395
4468
|
}
|
|
4396
4469
|
const args = { where };
|
|
4397
4470
|
if (supportsProjection) {
|
|
@@ -4476,7 +4549,7 @@ function createModelGuardExtension(config) {
|
|
|
4476
4549
|
);
|
|
4477
4550
|
validateResolvedUniqueWhere(modelName, where, "upsert", uniqueMap);
|
|
4478
4551
|
const args = {
|
|
4479
|
-
where,
|
|
4552
|
+
where: normalizeUniqueWhere(where),
|
|
4480
4553
|
create: createData,
|
|
4481
4554
|
update: updateData
|
|
4482
4555
|
};
|