prisma-guard 1.26.2 → 1.27.1

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.
@@ -959,6 +959,66 @@ var EMPTY_WHERE_FORCED = {
959
959
  function hasWhereForced(f) {
960
960
  return Object.keys(f.conditions).length > 0 || Object.keys(f.relations).length > 0;
961
961
  }
962
+ function forcedScalarsEqual(a, b) {
963
+ if (a === b)
964
+ return true;
965
+ if (a === null || b === null)
966
+ return false;
967
+ if (typeof a !== typeof b)
968
+ return false;
969
+ if (a instanceof Date && b instanceof Date) {
970
+ return a.getTime() === b.getTime();
971
+ }
972
+ if (a instanceof RegExp && b instanceof RegExp) {
973
+ return a.source === b.source && a.flags === b.flags;
974
+ }
975
+ if (Array.isArray(a)) {
976
+ if (!Array.isArray(b))
977
+ return false;
978
+ if (a.length !== b.length)
979
+ return false;
980
+ return a.every((v, i) => forcedScalarsEqual(v, b[i]));
981
+ }
982
+ if (isPlainObject(a) && isPlainObject(b)) {
983
+ const aKeys = Object.keys(a);
984
+ const bKeys = Object.keys(b);
985
+ if (aKeys.length !== bKeys.length)
986
+ return false;
987
+ return aKeys.every(
988
+ (k) => k in b && forcedScalarsEqual(a[k], b[k])
989
+ );
990
+ }
991
+ return false;
992
+ }
993
+ function tryInlineForcedField(result, field, forcedValue) {
994
+ if (!(field in result))
995
+ return false;
996
+ if (!isPlainObject(forcedValue))
997
+ return false;
998
+ const existing = result[field];
999
+ if (!isPlainObject(existing)) {
1000
+ const merged2 = { equals: existing };
1001
+ for (const [op, value] of Object.entries(forcedValue)) {
1002
+ merged2[op] = deepClone(value);
1003
+ }
1004
+ result[field] = merged2;
1005
+ return true;
1006
+ }
1007
+ const merged = { ...existing };
1008
+ for (const [op, value] of Object.entries(forcedValue)) {
1009
+ if (op in merged) {
1010
+ if (!forcedScalarsEqual(merged[op], value)) {
1011
+ throw new ShapeError(
1012
+ `Conflicting where values for "${field}.${op}": client provided a different value than the forced shape`
1013
+ );
1014
+ }
1015
+ continue;
1016
+ }
1017
+ merged[op] = deepClone(value);
1018
+ }
1019
+ result[field] = merged;
1020
+ return true;
1021
+ }
962
1022
  function mergeWhereForced(where, forced) {
963
1023
  if (!hasWhereForced(forced))
964
1024
  return where ?? {};
@@ -976,11 +1036,19 @@ function mergeWhereForced(where, forced) {
976
1036
  }
977
1037
  }
978
1038
  if (Object.keys(forced.conditions).length > 0) {
979
- const scalarClone = deepClone(forced.conditions);
980
- if (Object.keys(result).length === 0) {
981
- result = scalarClone;
982
- } else {
983
- result = { AND: [result, scalarClone] };
1039
+ const remaining = {};
1040
+ for (const [field, forcedValue] of Object.entries(forced.conditions)) {
1041
+ const inlined = tryInlineForcedField(result, field, forcedValue);
1042
+ if (!inlined) {
1043
+ remaining[field] = deepClone(forcedValue);
1044
+ }
1045
+ }
1046
+ if (Object.keys(remaining).length > 0) {
1047
+ if (Object.keys(result).length === 0) {
1048
+ result = remaining;
1049
+ } else {
1050
+ result = { AND: [result, remaining] };
1051
+ }
984
1052
  }
985
1053
  }
986
1054
  return result;