@zenstackhq/orm 3.0.0-beta.26 → 3.0.0-beta.28
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/index.cjs +624 -209
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +573 -33
- package/dist/index.d.ts +573 -33
- package/dist/index.js +618 -209
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -52,10 +52,12 @@ __export(query_utils_exports, {
|
|
|
52
52
|
isInheritedField: () => isInheritedField,
|
|
53
53
|
isRelationField: () => isRelationField,
|
|
54
54
|
isScalarField: () => isScalarField,
|
|
55
|
+
isTypeDef: () => isTypeDef,
|
|
55
56
|
makeDefaultOrderBy: () => makeDefaultOrderBy,
|
|
56
57
|
requireField: () => requireField,
|
|
57
58
|
requireIdFields: () => requireIdFields,
|
|
58
59
|
requireModel: () => requireModel,
|
|
60
|
+
requireTypeDef: () => requireTypeDef,
|
|
59
61
|
stripAlias: () => stripAlias
|
|
60
62
|
});
|
|
61
63
|
import { invariant } from "@zenstackhq/common-helpers";
|
|
@@ -206,6 +208,14 @@ function requireModel(schema, model) {
|
|
|
206
208
|
return modelDef;
|
|
207
209
|
}
|
|
208
210
|
__name(requireModel, "requireModel");
|
|
211
|
+
function requireTypeDef(schema, type) {
|
|
212
|
+
const typeDef = getTypeDef(schema, type);
|
|
213
|
+
if (!typeDef) {
|
|
214
|
+
throw createInternalError(`Type "${type}" not found in schema`, type);
|
|
215
|
+
}
|
|
216
|
+
return typeDef;
|
|
217
|
+
}
|
|
218
|
+
__name(requireTypeDef, "requireTypeDef");
|
|
209
219
|
function getField(schema, model, field) {
|
|
210
220
|
const modelDef = getModel(schema, model);
|
|
211
221
|
return modelDef?.fields[field];
|
|
@@ -353,6 +363,10 @@ function getEnum(schema, type) {
|
|
|
353
363
|
return schema.enums?.[type];
|
|
354
364
|
}
|
|
355
365
|
__name(getEnum, "getEnum");
|
|
366
|
+
function isTypeDef(schema, type) {
|
|
367
|
+
return !!schema.typeDefs?.[type];
|
|
368
|
+
}
|
|
369
|
+
__name(isTypeDef, "isTypeDef");
|
|
356
370
|
function buildJoinPairs(schema, model, modelAlias, relationField, relationModelAlias) {
|
|
357
371
|
const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(schema, model, relationField);
|
|
358
372
|
return keyPairs.map(({ fk, pk }) => {
|
|
@@ -602,6 +616,33 @@ import { invariant as invariant3 } from "@zenstackhq/common-helpers";
|
|
|
602
616
|
import Decimal from "decimal.js";
|
|
603
617
|
import { sql as sql2 } from "kysely";
|
|
604
618
|
import { match as match3 } from "ts-pattern";
|
|
619
|
+
import z from "zod";
|
|
620
|
+
|
|
621
|
+
// src/common-types.ts
|
|
622
|
+
var DbNullClass = class {
|
|
623
|
+
static {
|
|
624
|
+
__name(this, "DbNullClass");
|
|
625
|
+
}
|
|
626
|
+
// @ts-ignore
|
|
627
|
+
__brand = "DbNull";
|
|
628
|
+
};
|
|
629
|
+
var DbNull = new DbNullClass();
|
|
630
|
+
var JsonNullClass = class {
|
|
631
|
+
static {
|
|
632
|
+
__name(this, "JsonNullClass");
|
|
633
|
+
}
|
|
634
|
+
// @ts-ignore
|
|
635
|
+
__brand = "JsonNull";
|
|
636
|
+
};
|
|
637
|
+
var JsonNull = new JsonNullClass();
|
|
638
|
+
var AnyNullClass = class {
|
|
639
|
+
static {
|
|
640
|
+
__name(this, "AnyNullClass");
|
|
641
|
+
}
|
|
642
|
+
// @ts-ignore
|
|
643
|
+
__brand = "AnyNull";
|
|
644
|
+
};
|
|
645
|
+
var AnyNull = new AnyNullClass();
|
|
605
646
|
|
|
606
647
|
// src/client/crud/dialects/base-dialect.ts
|
|
607
648
|
import { enumerate, invariant as invariant2, isPlainObject } from "@zenstackhq/common-helpers";
|
|
@@ -874,12 +915,174 @@ var BaseCrudDialect = class {
|
|
|
874
915
|
if (isEnum(this.schema, fieldDef.type)) {
|
|
875
916
|
return this.buildEnumFilter(fieldRef, fieldDef, payload);
|
|
876
917
|
}
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
}
|
|
918
|
+
if (isTypeDef(this.schema, fieldDef.type)) {
|
|
919
|
+
return this.buildJsonFilter(fieldRef, payload, fieldDef);
|
|
920
|
+
}
|
|
921
|
+
return match2(fieldDef.type).with("String", () => this.buildStringFilter(fieldRef, payload)).with(P.union("Int", "Float", "Decimal", "BigInt"), (type) => this.buildNumberFilter(fieldRef, type, payload)).with("Boolean", () => this.buildBooleanFilter(fieldRef, payload)).with("DateTime", () => this.buildDateTimeFilter(fieldRef, payload)).with("Bytes", () => this.buildBytesFilter(fieldRef, payload)).with("Json", () => this.buildJsonFilter(fieldRef, payload, fieldDef)).with("Unsupported", () => {
|
|
880
922
|
throw createInvalidInputError(`Unsupported field cannot be used in filters`);
|
|
881
923
|
}).exhaustive();
|
|
882
924
|
}
|
|
925
|
+
buildJsonFilter(receiver, filter, fieldDef) {
|
|
926
|
+
invariant2(filter && typeof filter === "object", "Json filter payload must be an object");
|
|
927
|
+
if ([
|
|
928
|
+
"path",
|
|
929
|
+
"equals",
|
|
930
|
+
"not",
|
|
931
|
+
"string_contains",
|
|
932
|
+
"string_starts_with",
|
|
933
|
+
"string_ends_with",
|
|
934
|
+
"array_contains",
|
|
935
|
+
"array_starts_with",
|
|
936
|
+
"array_ends_with"
|
|
937
|
+
].some((k) => k in filter)) {
|
|
938
|
+
return this.buildPlainJsonFilter(receiver, filter);
|
|
939
|
+
} else if (isTypeDef(this.schema, fieldDef.type)) {
|
|
940
|
+
return this.buildTypedJsonFilter(receiver, filter, fieldDef.type, !!fieldDef.array);
|
|
941
|
+
} else {
|
|
942
|
+
throw createInvalidInputError(`Invalid JSON filter payload`);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
buildPlainJsonFilter(receiver, filter) {
|
|
946
|
+
const clauses = [];
|
|
947
|
+
const path = filter.path;
|
|
948
|
+
const jsonReceiver = this.buildJsonPathSelection(receiver, path);
|
|
949
|
+
const stringReceiver = this.eb.cast(jsonReceiver, "text");
|
|
950
|
+
const mode = filter.mode ?? "default";
|
|
951
|
+
invariant2(mode === "default" || mode === "insensitive", "Invalid JSON filter mode");
|
|
952
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
953
|
+
switch (key) {
|
|
954
|
+
case "equals": {
|
|
955
|
+
clauses.push(this.buildJsonValueFilterClause(jsonReceiver, value));
|
|
956
|
+
break;
|
|
957
|
+
}
|
|
958
|
+
case "not": {
|
|
959
|
+
clauses.push(this.eb.not(this.buildJsonValueFilterClause(jsonReceiver, value)));
|
|
960
|
+
break;
|
|
961
|
+
}
|
|
962
|
+
case "string_contains": {
|
|
963
|
+
invariant2(typeof value === "string", "string_contains value must be a string");
|
|
964
|
+
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
965
|
+
break;
|
|
966
|
+
}
|
|
967
|
+
case "string_starts_with": {
|
|
968
|
+
invariant2(typeof value === "string", "string_starts_with value must be a string");
|
|
969
|
+
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
case "string_ends_with": {
|
|
973
|
+
invariant2(typeof value === "string", "string_ends_with value must be a string");
|
|
974
|
+
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
case "array_contains": {
|
|
978
|
+
clauses.push(this.buildJsonArrayFilter(jsonReceiver, key, value));
|
|
979
|
+
break;
|
|
980
|
+
}
|
|
981
|
+
case "array_starts_with": {
|
|
982
|
+
clauses.push(this.buildJsonArrayFilter(jsonReceiver, key, value));
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
case "array_ends_with": {
|
|
986
|
+
clauses.push(this.buildJsonArrayFilter(jsonReceiver, key, value));
|
|
987
|
+
break;
|
|
988
|
+
}
|
|
989
|
+
case "path":
|
|
990
|
+
case "mode":
|
|
991
|
+
break;
|
|
992
|
+
default:
|
|
993
|
+
throw createInvalidInputError(`Invalid JSON filter key: ${key}`);
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
return this.and(...clauses);
|
|
997
|
+
}
|
|
998
|
+
buildTypedJsonFilter(receiver, filter, typeDefName, array) {
|
|
999
|
+
if (array) {
|
|
1000
|
+
return this.buildTypedJsonArrayFilter(receiver, filter, typeDefName);
|
|
1001
|
+
} else {
|
|
1002
|
+
return this.buildTypeJsonNonArrayFilter(receiver, filter, typeDefName);
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
buildTypedJsonArrayFilter(receiver, filter, typeDefName) {
|
|
1006
|
+
invariant2(filter && typeof filter === "object", "Typed JSON array filter payload must be an object");
|
|
1007
|
+
const makeExistsPred = /* @__PURE__ */ __name((filter2) => this.buildJsonArrayExistsPredicate(receiver, (elem) => this.buildTypedJsonFilter(elem, filter2, typeDefName, false)), "makeExistsPred");
|
|
1008
|
+
const makeExistsNegatedPred = /* @__PURE__ */ __name((filter2) => this.buildJsonArrayExistsPredicate(receiver, (elem) => this.eb.not(this.buildTypedJsonFilter(elem, filter2, typeDefName, false))), "makeExistsNegatedPred");
|
|
1009
|
+
const clauses = [];
|
|
1010
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
1011
|
+
if (!value || typeof value !== "object") {
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
switch (key) {
|
|
1015
|
+
case "some":
|
|
1016
|
+
clauses.push(makeExistsPred(value));
|
|
1017
|
+
break;
|
|
1018
|
+
case "none":
|
|
1019
|
+
clauses.push(this.eb.not(makeExistsPred(value)));
|
|
1020
|
+
break;
|
|
1021
|
+
case "every":
|
|
1022
|
+
clauses.push(this.eb.not(makeExistsNegatedPred(value)));
|
|
1023
|
+
break;
|
|
1024
|
+
default:
|
|
1025
|
+
invariant2(false, `Invalid typed JSON array filter key: ${key}`);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return this.and(...clauses);
|
|
1029
|
+
}
|
|
1030
|
+
buildTypeJsonNonArrayFilter(receiver, filter, typeDefName) {
|
|
1031
|
+
const clauses = [];
|
|
1032
|
+
if (filter === null) {
|
|
1033
|
+
return this.eb(receiver, "=", "null");
|
|
1034
|
+
}
|
|
1035
|
+
invariant2(filter && typeof filter === "object", "Typed JSON filter payload must be an object");
|
|
1036
|
+
if ("is" in filter || "isNot" in filter) {
|
|
1037
|
+
if ("is" in filter && filter.is && typeof filter.is === "object") {
|
|
1038
|
+
clauses.push(this.buildTypedJsonFilter(receiver, filter.is, typeDefName, false));
|
|
1039
|
+
}
|
|
1040
|
+
if ("isNot" in filter && filter.isNot && typeof filter.isNot === "object") {
|
|
1041
|
+
clauses.push(this.eb.not(this.buildTypedJsonFilter(receiver, filter.isNot, typeDefName, false)));
|
|
1042
|
+
}
|
|
1043
|
+
} else {
|
|
1044
|
+
const typeDef = requireTypeDef(this.schema, typeDefName);
|
|
1045
|
+
for (const [key, value] of Object.entries(filter)) {
|
|
1046
|
+
const fieldDef = typeDef.fields[key];
|
|
1047
|
+
invariant2(fieldDef, `Field "${key}" not found in type definition "${typeDefName}"`);
|
|
1048
|
+
const fieldReceiver = this.buildJsonPathSelection(receiver, `$.${key}`);
|
|
1049
|
+
if (isTypeDef(this.schema, fieldDef.type)) {
|
|
1050
|
+
clauses.push(this.buildTypedJsonFilter(fieldReceiver, value, fieldDef.type, !!fieldDef.array));
|
|
1051
|
+
} else {
|
|
1052
|
+
if (fieldDef.array) {
|
|
1053
|
+
clauses.push(this.buildArrayFilter(fieldReceiver, fieldDef, value));
|
|
1054
|
+
} else {
|
|
1055
|
+
let _receiver = fieldReceiver;
|
|
1056
|
+
if (fieldDef.type === "String") {
|
|
1057
|
+
_receiver = this.eb.fn("trim", [
|
|
1058
|
+
this.eb.cast(fieldReceiver, "text"),
|
|
1059
|
+
sql.lit('"')
|
|
1060
|
+
]);
|
|
1061
|
+
}
|
|
1062
|
+
clauses.push(this.buildPrimitiveFilter(_receiver, fieldDef, value));
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
return this.and(...clauses);
|
|
1068
|
+
}
|
|
1069
|
+
buildJsonValueFilterClause(lhs, value) {
|
|
1070
|
+
if (value instanceof DbNullClass) {
|
|
1071
|
+
return this.eb(lhs, "is", null);
|
|
1072
|
+
} else if (value instanceof JsonNullClass) {
|
|
1073
|
+
return this.eb.and([
|
|
1074
|
+
this.eb(lhs, "=", "null"),
|
|
1075
|
+
this.eb(lhs, "is not", null)
|
|
1076
|
+
]);
|
|
1077
|
+
} else if (value instanceof AnyNullClass) {
|
|
1078
|
+
return this.eb.or([
|
|
1079
|
+
this.eb(lhs, "is", null),
|
|
1080
|
+
this.eb(lhs, "=", "null")
|
|
1081
|
+
]);
|
|
1082
|
+
} else {
|
|
1083
|
+
return this.buildLiteralFilter(lhs, "Json", value);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
883
1086
|
buildLiteralFilter(lhs, type, rhs) {
|
|
884
1087
|
return this.eb(lhs, "=", rhs !== null && rhs !== void 0 ? this.transformPrimitive(rhs, type, false) : rhs);
|
|
885
1088
|
}
|
|
@@ -950,7 +1153,9 @@ var BaseCrudDialect = class {
|
|
|
950
1153
|
if (key === "mode" || consumedKeys.includes(key)) {
|
|
951
1154
|
continue;
|
|
952
1155
|
}
|
|
953
|
-
|
|
1156
|
+
invariant2(typeof value === "string", `${key} value must be a string`);
|
|
1157
|
+
const escapedValue = this.escapeLikePattern(value);
|
|
1158
|
+
const condition = match2(key).with("contains", () => this.buildStringLike(fieldRef, `%${escapedValue}%`, mode === "insensitive")).with("startsWith", () => this.buildStringLike(fieldRef, `${escapedValue}%`, mode === "insensitive")).with("endsWith", () => this.buildStringLike(fieldRef, `%${escapedValue}`, mode === "insensitive")).otherwise(() => {
|
|
954
1159
|
throw createInvalidInputError(`Invalid string filter key: ${key}`);
|
|
955
1160
|
});
|
|
956
1161
|
if (condition) {
|
|
@@ -960,6 +1165,19 @@ var BaseCrudDialect = class {
|
|
|
960
1165
|
}
|
|
961
1166
|
return this.and(...conditions);
|
|
962
1167
|
}
|
|
1168
|
+
buildJsonStringFilter(receiver, operation, value, mode) {
|
|
1169
|
+
const escapedValue = this.escapeLikePattern(value);
|
|
1170
|
+
const pattern = match2(operation).with("string_contains", () => `"%${escapedValue}%"`).with("string_starts_with", () => `"${escapedValue}%"`).with("string_ends_with", () => `"%${escapedValue}"`).exhaustive();
|
|
1171
|
+
return this.buildStringLike(receiver, pattern, mode === "insensitive");
|
|
1172
|
+
}
|
|
1173
|
+
escapeLikePattern(pattern) {
|
|
1174
|
+
return pattern.replace(/\\/g, "\\\\").replace(/%/g, "\\%").replace(/_/g, "\\_");
|
|
1175
|
+
}
|
|
1176
|
+
buildStringLike(receiver, pattern, insensitive) {
|
|
1177
|
+
const { supportsILike } = this.getStringCasingBehavior();
|
|
1178
|
+
const op = insensitive && supportsILike ? "ilike" : "like";
|
|
1179
|
+
return sql`${receiver} ${sql.raw(op)} ${sql.val(pattern)} escape '\\'`;
|
|
1180
|
+
}
|
|
963
1181
|
prepStringCasing(eb, value, mode) {
|
|
964
1182
|
if (!mode || mode === "default") {
|
|
965
1183
|
return value === null ? value : sql.val(value);
|
|
@@ -1271,6 +1489,10 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1271
1489
|
static {
|
|
1272
1490
|
__name(this, "PostgresCrudDialect");
|
|
1273
1491
|
}
|
|
1492
|
+
isoDateSchema = z.iso.datetime({
|
|
1493
|
+
local: true,
|
|
1494
|
+
offset: true
|
|
1495
|
+
});
|
|
1274
1496
|
constructor(schema, options) {
|
|
1275
1497
|
super(schema, options);
|
|
1276
1498
|
}
|
|
@@ -1281,6 +1503,13 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1281
1503
|
if (value === void 0) {
|
|
1282
1504
|
return value;
|
|
1283
1505
|
}
|
|
1506
|
+
if (value instanceof JsonNullClass) {
|
|
1507
|
+
return "null";
|
|
1508
|
+
} else if (value instanceof DbNullClass) {
|
|
1509
|
+
return null;
|
|
1510
|
+
} else if (value instanceof AnyNullClass) {
|
|
1511
|
+
invariant3(false, "should not reach here: AnyNull is not a valid input value");
|
|
1512
|
+
}
|
|
1284
1513
|
if (Array.isArray(value)) {
|
|
1285
1514
|
if (type === "Json" && !forArrayField) {
|
|
1286
1515
|
return JSON.stringify(value);
|
|
@@ -1319,7 +1548,12 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1319
1548
|
}
|
|
1320
1549
|
transformOutputDate(value) {
|
|
1321
1550
|
if (typeof value === "string") {
|
|
1322
|
-
|
|
1551
|
+
if (this.isoDateSchema.safeParse(value).success) {
|
|
1552
|
+
const hasOffset = value.endsWith("Z") || /[+-]\d{2}:\d{2}$/.test(value);
|
|
1553
|
+
return new Date(hasOffset ? value : `${value}Z`);
|
|
1554
|
+
} else {
|
|
1555
|
+
return value;
|
|
1556
|
+
}
|
|
1323
1557
|
} else if (value instanceof Date && this.options.fixPostgresTimezone !== false) {
|
|
1324
1558
|
return new Date(value.getTime() - value.getTimezoneOffset() * 60 * 1e3);
|
|
1325
1559
|
} else {
|
|
@@ -1472,6 +1706,35 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1472
1706
|
return `ARRAY[${values.map((v) => typeof v === "string" ? `'${v}'` : v)}]`;
|
|
1473
1707
|
}
|
|
1474
1708
|
}
|
|
1709
|
+
buildJsonPathSelection(receiver, path) {
|
|
1710
|
+
if (path) {
|
|
1711
|
+
return this.eb.fn("jsonb_path_query_first", [
|
|
1712
|
+
receiver,
|
|
1713
|
+
this.eb.val(path)
|
|
1714
|
+
]);
|
|
1715
|
+
} else {
|
|
1716
|
+
return receiver;
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
buildJsonArrayFilter(lhs, operation, value) {
|
|
1720
|
+
return match3(operation).with("array_contains", () => {
|
|
1721
|
+
const v = Array.isArray(value) ? value : [
|
|
1722
|
+
value
|
|
1723
|
+
];
|
|
1724
|
+
return sql2`${lhs} @> ${sql2.val(JSON.stringify(v))}::jsonb`;
|
|
1725
|
+
}).with("array_starts_with", () => this.eb(this.eb.fn("jsonb_extract_path", [
|
|
1726
|
+
lhs,
|
|
1727
|
+
this.eb.val("0")
|
|
1728
|
+
]), "=", this.transformPrimitive(value, "Json", false))).with("array_ends_with", () => this.eb(this.eb.fn("jsonb_extract_path", [
|
|
1729
|
+
lhs,
|
|
1730
|
+
sql2`(jsonb_array_length(${lhs}) - 1)::text`
|
|
1731
|
+
]), "=", this.transformPrimitive(value, "Json", false))).exhaustive();
|
|
1732
|
+
}
|
|
1733
|
+
buildJsonArrayExistsPredicate(receiver, buildFilter) {
|
|
1734
|
+
return this.eb.exists(this.eb.selectFrom(this.eb.fn("jsonb_array_elements", [
|
|
1735
|
+
receiver
|
|
1736
|
+
]).as("$items")).select(this.eb.lit(1).as("$t")).where(buildFilter(this.eb.ref("$items.value"))));
|
|
1737
|
+
}
|
|
1475
1738
|
get supportInsertWithDefault() {
|
|
1476
1739
|
return true;
|
|
1477
1740
|
}
|
|
@@ -1514,14 +1777,20 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1514
1777
|
if (value === void 0) {
|
|
1515
1778
|
return value;
|
|
1516
1779
|
}
|
|
1780
|
+
if (value instanceof JsonNullClass) {
|
|
1781
|
+
return "null";
|
|
1782
|
+
} else if (value instanceof DbNullClass) {
|
|
1783
|
+
return null;
|
|
1784
|
+
} else if (value instanceof AnyNullClass) {
|
|
1785
|
+
invariant4(false, "should not reach here: AnyNull is not a valid input value");
|
|
1786
|
+
}
|
|
1787
|
+
if (type === "Json" || this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
1788
|
+
return JSON.stringify(value);
|
|
1789
|
+
}
|
|
1517
1790
|
if (Array.isArray(value)) {
|
|
1518
1791
|
return value.map((v) => this.transformPrimitive(v, type, false));
|
|
1519
1792
|
} else {
|
|
1520
|
-
|
|
1521
|
-
return JSON.stringify(value);
|
|
1522
|
-
} else {
|
|
1523
|
-
return match4(type).with("Boolean", () => value ? 1 : 0).with("DateTime", () => value instanceof Date ? value.toISOString() : typeof value === "string" ? new Date(value).toISOString() : value).with("Decimal", () => value.toString()).with("Bytes", () => Buffer.from(value)).with("Json", () => JSON.stringify(value)).otherwise(() => value);
|
|
1524
|
-
}
|
|
1793
|
+
return match4(type).with("Boolean", () => value ? 1 : 0).with("DateTime", () => value instanceof Date ? value.toISOString() : typeof value === "string" ? new Date(value).toISOString() : value).with("Decimal", () => value.toString()).with("Bytes", () => Buffer.from(value)).otherwise(() => value);
|
|
1525
1794
|
}
|
|
1526
1795
|
}
|
|
1527
1796
|
transformOutput(value, type) {
|
|
@@ -1691,6 +1960,30 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1691
1960
|
value2
|
|
1692
1961
|
]));
|
|
1693
1962
|
}
|
|
1963
|
+
buildJsonPathSelection(receiver, path) {
|
|
1964
|
+
if (!path) {
|
|
1965
|
+
return receiver;
|
|
1966
|
+
} else {
|
|
1967
|
+
return sql3`${receiver} -> ${this.eb.val(path)}`;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
buildJsonArrayFilter(lhs, operation, value) {
|
|
1971
|
+
return match4(operation).with("array_contains", () => {
|
|
1972
|
+
if (Array.isArray(value)) {
|
|
1973
|
+
throw createNotSupportedError('SQLite "array_contains" only supports checking for a single value, not an array of values');
|
|
1974
|
+
} else {
|
|
1975
|
+
return sql3`EXISTS (SELECT 1 FROM jsonb_each(${lhs}) WHERE value = ${value})`;
|
|
1976
|
+
}
|
|
1977
|
+
}).with("array_starts_with", () => this.eb(this.eb.fn("json_extract", [
|
|
1978
|
+
lhs,
|
|
1979
|
+
this.eb.val("$[0]")
|
|
1980
|
+
]), "=", value)).with("array_ends_with", () => this.eb(sql3`json_extract(${lhs}, '$[' || (json_array_length(${lhs}) - 1) || ']')`, "=", value)).exhaustive();
|
|
1981
|
+
}
|
|
1982
|
+
buildJsonArrayExistsPredicate(receiver, buildFilter) {
|
|
1983
|
+
return this.eb.exists(this.eb.selectFrom(this.eb.fn("jsonb_each", [
|
|
1984
|
+
receiver
|
|
1985
|
+
]).as("$items")).select(this.eb.lit(1).as("$t")).where(buildFilter(this.eb.ref("$items.value"))));
|
|
1986
|
+
}
|
|
1694
1987
|
get supportsUpdateWithLimit() {
|
|
1695
1988
|
return false;
|
|
1696
1989
|
}
|
|
@@ -2309,8 +2602,16 @@ var BaseOperationHandler = class {
|
|
|
2309
2602
|
autoUpdatedFields.push(fieldName);
|
|
2310
2603
|
}
|
|
2311
2604
|
}
|
|
2605
|
+
const thisEntity = await this.getEntityIds(kysely, model, combinedWhere);
|
|
2606
|
+
if (!thisEntity) {
|
|
2607
|
+
if (throwIfNotFound) {
|
|
2608
|
+
throw createNotFoundError(model);
|
|
2609
|
+
} else {
|
|
2610
|
+
return null;
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2312
2613
|
if (Object.keys(finalData).length === 0) {
|
|
2313
|
-
return
|
|
2614
|
+
return thisEntity;
|
|
2314
2615
|
}
|
|
2315
2616
|
let needIdRead = false;
|
|
2316
2617
|
if (modelDef.baseModel && !this.isIdFilter(model, combinedWhere)) {
|
|
@@ -2330,9 +2631,15 @@ var BaseOperationHandler = class {
|
|
|
2330
2631
|
const baseUpdateResult = await this.processBaseModelUpdate(kysely, modelDef.baseModel, combinedWhere, finalData, throwIfNotFound);
|
|
2331
2632
|
finalData = baseUpdateResult.remainingFields;
|
|
2332
2633
|
combinedWhere = baseUpdateResult.baseEntity;
|
|
2634
|
+
if (baseUpdateResult.baseEntity) {
|
|
2635
|
+
for (const [key, value] of Object.entries(baseUpdateResult.baseEntity)) {
|
|
2636
|
+
if (key in thisEntity) {
|
|
2637
|
+
thisEntity[key] = value;
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2333
2641
|
}
|
|
2334
2642
|
const updateFields = {};
|
|
2335
|
-
let thisEntity = void 0;
|
|
2336
2643
|
for (const field in finalData) {
|
|
2337
2644
|
const fieldDef = this.requireField(model, field);
|
|
2338
2645
|
if (isScalarField(this.schema, model, field) || isForeignKeyField(this.schema, model, field)) {
|
|
@@ -2341,17 +2648,7 @@ var BaseOperationHandler = class {
|
|
|
2341
2648
|
if (!allowRelationUpdate) {
|
|
2342
2649
|
throw createNotSupportedError(`Relation update not allowed for field "${field}"`);
|
|
2343
2650
|
}
|
|
2344
|
-
|
|
2345
|
-
thisEntity = await this.getEntityIds(kysely, model, combinedWhere);
|
|
2346
|
-
if (!thisEntity) {
|
|
2347
|
-
if (throwIfNotFound) {
|
|
2348
|
-
throw createNotFoundError(model);
|
|
2349
|
-
} else {
|
|
2350
|
-
return null;
|
|
2351
|
-
}
|
|
2352
|
-
}
|
|
2353
|
-
}
|
|
2354
|
-
const parentUpdates = await this.processRelationUpdates(kysely, model, field, fieldDef, thisEntity, finalData[field], throwIfNotFound);
|
|
2651
|
+
const parentUpdates = await this.processRelationUpdates(kysely, model, field, fieldDef, thisEntity, finalData[field]);
|
|
2355
2652
|
if (Object.keys(parentUpdates).length > 0) {
|
|
2356
2653
|
Object.assign(updateFields, parentUpdates);
|
|
2357
2654
|
}
|
|
@@ -2362,7 +2659,7 @@ var BaseOperationHandler = class {
|
|
|
2362
2659
|
hasFieldUpdate = Object.keys(updateFields).some((f) => !autoUpdatedFields.includes(f));
|
|
2363
2660
|
}
|
|
2364
2661
|
if (!hasFieldUpdate) {
|
|
2365
|
-
return
|
|
2662
|
+
return thisEntity;
|
|
2366
2663
|
} else {
|
|
2367
2664
|
fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
|
|
2368
2665
|
const query = kysely.updateTable(model).where(() => this.dialect.buildFilter(model, model, combinedWhere)).set(updateFields).returning(fieldsToReturn).modifyEnd(this.makeContextComment({
|
|
@@ -2541,7 +2838,7 @@ var BaseOperationHandler = class {
|
|
|
2541
2838
|
const idFields = requireIdFields(this.schema, model);
|
|
2542
2839
|
return idFields.map((f) => kysely.dynamic.ref(`${model}.${f}`));
|
|
2543
2840
|
}
|
|
2544
|
-
async processRelationUpdates(kysely, model, field, fieldDef, parentIds, args
|
|
2841
|
+
async processRelationUpdates(kysely, model, field, fieldDef, parentIds, args) {
|
|
2545
2842
|
const fieldModel = fieldDef.type;
|
|
2546
2843
|
const fromRelationContext = {
|
|
2547
2844
|
model,
|
|
@@ -2592,6 +2889,7 @@ var BaseOperationHandler = class {
|
|
|
2592
2889
|
where = void 0;
|
|
2593
2890
|
data = item;
|
|
2594
2891
|
}
|
|
2892
|
+
const throwIfNotFound = !fieldDef.array || !!where;
|
|
2595
2893
|
await this.update(kysely, fieldModel, where, data, fromRelationContext, true, throwIfNotFound);
|
|
2596
2894
|
}
|
|
2597
2895
|
break;
|
|
@@ -3601,7 +3899,7 @@ import { enumerate as enumerate3, invariant as invariant7 } from "@zenstackhq/co
|
|
|
3601
3899
|
import Decimal4 from "decimal.js";
|
|
3602
3900
|
import stableStringify from "json-stable-stringify";
|
|
3603
3901
|
import { match as match13, P as P3 } from "ts-pattern";
|
|
3604
|
-
import { z as
|
|
3902
|
+
import { z as z3 } from "zod";
|
|
3605
3903
|
|
|
3606
3904
|
// src/utils/zod-utils.ts
|
|
3607
3905
|
import { fromError } from "zod-validation-error/v4";
|
|
@@ -3614,7 +3912,7 @@ __name(formatError, "formatError");
|
|
|
3614
3912
|
import { invariant as invariant6 } from "@zenstackhq/common-helpers";
|
|
3615
3913
|
import Decimal3 from "decimal.js";
|
|
3616
3914
|
import { match as match12, P as P2 } from "ts-pattern";
|
|
3617
|
-
import { z } from "zod";
|
|
3915
|
+
import { z as z2 } from "zod";
|
|
3618
3916
|
import { ZodIssueCode } from "zod/v3";
|
|
3619
3917
|
function getArgValue(expr) {
|
|
3620
3918
|
if (!expr || !schema_exports.ExpressionUtils.isLiteral(expr)) {
|
|
@@ -3723,13 +4021,13 @@ function addBigIntValidation(schema, attributes) {
|
|
|
3723
4021
|
__name(addBigIntValidation, "addBigIntValidation");
|
|
3724
4022
|
function addDecimalValidation(schema, attributes, addExtraValidation) {
|
|
3725
4023
|
let result = schema;
|
|
3726
|
-
if (schema instanceof
|
|
4024
|
+
if (schema instanceof z2.ZodString) {
|
|
3727
4025
|
result = schema.superRefine((v, ctx) => {
|
|
3728
4026
|
try {
|
|
3729
4027
|
new Decimal3(v);
|
|
3730
4028
|
} catch (err) {
|
|
3731
4029
|
ctx.addIssue({
|
|
3732
|
-
code:
|
|
4030
|
+
code: z2.ZodIssueCode.custom,
|
|
3733
4031
|
message: `Invalid decimal: ${err}`
|
|
3734
4032
|
});
|
|
3735
4033
|
}
|
|
@@ -3737,7 +4035,7 @@ function addDecimalValidation(schema, attributes, addExtraValidation) {
|
|
|
3737
4035
|
}
|
|
3738
4036
|
function refine(schema2, op, value) {
|
|
3739
4037
|
return schema2.superRefine((v, ctx) => {
|
|
3740
|
-
const base =
|
|
4038
|
+
const base = z2.number();
|
|
3741
4039
|
const { error } = base[op](value).safeParse(v.toNumber());
|
|
3742
4040
|
error?.issues.forEach((issue) => {
|
|
3743
4041
|
if (op === "gt" || op === "gte") {
|
|
@@ -3942,7 +4240,7 @@ function evalCall(data, expr) {
|
|
|
3942
4240
|
}
|
|
3943
4241
|
invariant6(typeof fieldArg === "string", `"${f}" first argument must be a string`);
|
|
3944
4242
|
const fn = match12(f).with("isEmail", () => "email").with("isUrl", () => "url").with("isDateTime", () => "datetime").exhaustive();
|
|
3945
|
-
return
|
|
4243
|
+
return z2.string()[fn]().safeParse(fieldArg).success;
|
|
3946
4244
|
}).with(P2.union("has", "hasEvery", "hasSome"), (f) => {
|
|
3947
4245
|
invariant6(expr.args?.[1], `${f} requires a search argument`);
|
|
3948
4246
|
if (fieldArg === void 0 || fieldArg === null) {
|
|
@@ -4080,7 +4378,7 @@ var InputValidator = class {
|
|
|
4080
4378
|
if (!options.unique) {
|
|
4081
4379
|
fields["skip"] = this.makeSkipSchema().optional();
|
|
4082
4380
|
if (options.findOne) {
|
|
4083
|
-
fields["take"] =
|
|
4381
|
+
fields["take"] = z3.literal(1).optional();
|
|
4084
4382
|
} else {
|
|
4085
4383
|
fields["take"] = this.makeTakeSchema().optional();
|
|
4086
4384
|
}
|
|
@@ -4088,7 +4386,7 @@ var InputValidator = class {
|
|
|
4088
4386
|
fields["cursor"] = this.makeCursorSchema(model).optional();
|
|
4089
4387
|
fields["distinct"] = this.makeDistinctSchema(model).optional();
|
|
4090
4388
|
}
|
|
4091
|
-
let result =
|
|
4389
|
+
let result = z3.strictObject(fields);
|
|
4092
4390
|
result = this.refineForSelectIncludeMutuallyExclusive(result);
|
|
4093
4391
|
result = this.refineForSelectOmitMutuallyExclusive(result);
|
|
4094
4392
|
if (!options.unique) {
|
|
@@ -4096,25 +4394,25 @@ var InputValidator = class {
|
|
|
4096
4394
|
}
|
|
4097
4395
|
return result;
|
|
4098
4396
|
}
|
|
4099
|
-
|
|
4397
|
+
makeScalarSchema(type, attributes) {
|
|
4100
4398
|
if (this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
4101
4399
|
return this.makeTypeDefSchema(type);
|
|
4102
4400
|
} else if (this.schema.enums && type in this.schema.enums) {
|
|
4103
4401
|
return this.makeEnumSchema(type);
|
|
4104
4402
|
} else {
|
|
4105
|
-
return match13(type).with("String", () => this.extraValidationsEnabled ? addStringValidation(
|
|
4106
|
-
this.extraValidationsEnabled ? addNumberValidation(
|
|
4107
|
-
this.extraValidationsEnabled ? addBigIntValidation(
|
|
4403
|
+
return match13(type).with("String", () => this.extraValidationsEnabled ? addStringValidation(z3.string(), attributes) : z3.string()).with("Int", () => this.extraValidationsEnabled ? addNumberValidation(z3.number().int(), attributes) : z3.number().int()).with("Float", () => this.extraValidationsEnabled ? addNumberValidation(z3.number(), attributes) : z3.number()).with("Boolean", () => z3.boolean()).with("BigInt", () => z3.union([
|
|
4404
|
+
this.extraValidationsEnabled ? addNumberValidation(z3.number().int(), attributes) : z3.number().int(),
|
|
4405
|
+
this.extraValidationsEnabled ? addBigIntValidation(z3.bigint(), attributes) : z3.bigint()
|
|
4108
4406
|
])).with("Decimal", () => {
|
|
4109
|
-
return
|
|
4110
|
-
this.extraValidationsEnabled ? addNumberValidation(
|
|
4111
|
-
addDecimalValidation(
|
|
4112
|
-
addDecimalValidation(
|
|
4407
|
+
return z3.union([
|
|
4408
|
+
this.extraValidationsEnabled ? addNumberValidation(z3.number(), attributes) : z3.number(),
|
|
4409
|
+
addDecimalValidation(z3.instanceof(Decimal4), attributes, this.extraValidationsEnabled),
|
|
4410
|
+
addDecimalValidation(z3.string(), attributes, this.extraValidationsEnabled)
|
|
4113
4411
|
]);
|
|
4114
|
-
}).with("DateTime", () =>
|
|
4115
|
-
|
|
4116
|
-
|
|
4117
|
-
])).with("Bytes", () =>
|
|
4412
|
+
}).with("DateTime", () => z3.union([
|
|
4413
|
+
z3.date(),
|
|
4414
|
+
z3.iso.datetime()
|
|
4415
|
+
])).with("Bytes", () => z3.instanceof(Uint8Array)).with("Json", () => this.makeJsonValueSchema(false, false)).otherwise(() => z3.unknown());
|
|
4118
4416
|
}
|
|
4119
4417
|
}
|
|
4120
4418
|
makeEnumSchema(type) {
|
|
@@ -4128,7 +4426,7 @@ var InputValidator = class {
|
|
|
4128
4426
|
}
|
|
4129
4427
|
const enumDef = getEnum(this.schema, type);
|
|
4130
4428
|
invariant7(enumDef, `Enum "${type}" not found in schema`);
|
|
4131
|
-
schema =
|
|
4429
|
+
schema = z3.enum(Object.keys(enumDef.values));
|
|
4132
4430
|
this.setSchemaCache(key, schema);
|
|
4133
4431
|
return schema;
|
|
4134
4432
|
}
|
|
@@ -4144,21 +4442,24 @@ var InputValidator = class {
|
|
|
4144
4442
|
}
|
|
4145
4443
|
const typeDef = getTypeDef(this.schema, type);
|
|
4146
4444
|
invariant7(typeDef, `Type definition "${type}" not found in schema`);
|
|
4147
|
-
schema =
|
|
4148
|
-
let fieldSchema = this.
|
|
4445
|
+
schema = z3.looseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
4446
|
+
let fieldSchema = this.makeScalarSchema(def.type);
|
|
4149
4447
|
if (def.array) {
|
|
4150
4448
|
fieldSchema = fieldSchema.array();
|
|
4151
4449
|
}
|
|
4152
4450
|
if (def.optional) {
|
|
4153
|
-
fieldSchema = fieldSchema.
|
|
4451
|
+
fieldSchema = fieldSchema.nullish();
|
|
4154
4452
|
}
|
|
4155
4453
|
return [
|
|
4156
4454
|
field,
|
|
4157
4455
|
fieldSchema
|
|
4158
4456
|
];
|
|
4159
4457
|
})));
|
|
4160
|
-
|
|
4161
|
-
|
|
4458
|
+
const finalSchema = z3.custom((v) => {
|
|
4459
|
+
return schema.safeParse(v).success;
|
|
4460
|
+
});
|
|
4461
|
+
this.setSchemaCache(key, finalSchema);
|
|
4462
|
+
return finalSchema;
|
|
4162
4463
|
}
|
|
4163
4464
|
makeWhereSchema(model, unique, withoutRelationFields = false, withAggregations = false) {
|
|
4164
4465
|
const modelDef = requireModel(this.schema, model);
|
|
@@ -4170,21 +4471,21 @@ var InputValidator = class {
|
|
|
4170
4471
|
if (withoutRelationFields) {
|
|
4171
4472
|
continue;
|
|
4172
4473
|
}
|
|
4173
|
-
fieldSchema =
|
|
4474
|
+
fieldSchema = z3.lazy(() => this.makeWhereSchema(fieldDef.type, false).optional());
|
|
4174
4475
|
fieldSchema = this.nullableIf(fieldSchema, !fieldDef.array && !!fieldDef.optional);
|
|
4175
4476
|
if (fieldDef.array) {
|
|
4176
|
-
fieldSchema =
|
|
4477
|
+
fieldSchema = z3.union([
|
|
4177
4478
|
fieldSchema,
|
|
4178
|
-
|
|
4479
|
+
z3.strictObject({
|
|
4179
4480
|
some: fieldSchema.optional(),
|
|
4180
4481
|
every: fieldSchema.optional(),
|
|
4181
4482
|
none: fieldSchema.optional()
|
|
4182
4483
|
})
|
|
4183
4484
|
]);
|
|
4184
4485
|
} else {
|
|
4185
|
-
fieldSchema =
|
|
4486
|
+
fieldSchema = z3.union([
|
|
4186
4487
|
fieldSchema,
|
|
4187
|
-
|
|
4488
|
+
z3.strictObject({
|
|
4188
4489
|
is: fieldSchema.optional(),
|
|
4189
4490
|
isNot: fieldSchema.optional()
|
|
4190
4491
|
})
|
|
@@ -4198,6 +4499,8 @@ var InputValidator = class {
|
|
|
4198
4499
|
}
|
|
4199
4500
|
} else if (fieldDef.array) {
|
|
4200
4501
|
fieldSchema = this.makeArrayFilterSchema(fieldDef.type);
|
|
4502
|
+
} else if (this.isTypeDefType(fieldDef.type)) {
|
|
4503
|
+
fieldSchema = this.makeTypedJsonFilterSchema(fieldDef.type, !!fieldDef.optional, !!fieldDef.array);
|
|
4201
4504
|
} else {
|
|
4202
4505
|
fieldSchema = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, withAggregations);
|
|
4203
4506
|
}
|
|
@@ -4210,7 +4513,7 @@ var InputValidator = class {
|
|
|
4210
4513
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
4211
4514
|
for (const uniqueField of uniqueFields) {
|
|
4212
4515
|
if ("defs" in uniqueField) {
|
|
4213
|
-
fields[uniqueField.name] =
|
|
4516
|
+
fields[uniqueField.name] = z3.object(Object.fromEntries(Object.entries(uniqueField.defs).map(([key, def]) => {
|
|
4214
4517
|
invariant7(!def.relation, "unique field cannot be a relation");
|
|
4215
4518
|
let fieldSchema;
|
|
4216
4519
|
const enumDef = getEnum(this.schema, def.type);
|
|
@@ -4218,7 +4521,7 @@ var InputValidator = class {
|
|
|
4218
4521
|
if (Object.keys(enumDef.values).length > 0) {
|
|
4219
4522
|
fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false);
|
|
4220
4523
|
} else {
|
|
4221
|
-
fieldSchema =
|
|
4524
|
+
fieldSchema = z3.never();
|
|
4222
4525
|
}
|
|
4223
4526
|
} else {
|
|
4224
4527
|
fieldSchema = this.makePrimitiveFilterSchema(def.type, !!def.optional, false);
|
|
@@ -4231,11 +4534,11 @@ var InputValidator = class {
|
|
|
4231
4534
|
}
|
|
4232
4535
|
}
|
|
4233
4536
|
}
|
|
4234
|
-
fields["$expr"] =
|
|
4235
|
-
fields["AND"] = this.orArray(
|
|
4236
|
-
fields["OR"] =
|
|
4237
|
-
fields["NOT"] = this.orArray(
|
|
4238
|
-
const baseWhere =
|
|
4537
|
+
fields["$expr"] = z3.custom((v) => typeof v === "function").optional();
|
|
4538
|
+
fields["AND"] = this.orArray(z3.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)), true).optional();
|
|
4539
|
+
fields["OR"] = z3.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)).array().optional();
|
|
4540
|
+
fields["NOT"] = this.orArray(z3.lazy(() => this.makeWhereSchema(model, false, withoutRelationFields)), true).optional();
|
|
4541
|
+
const baseWhere = z3.strictObject(fields);
|
|
4239
4542
|
let result = baseWhere;
|
|
4240
4543
|
if (unique) {
|
|
4241
4544
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
@@ -4254,9 +4557,55 @@ var InputValidator = class {
|
|
|
4254
4557
|
}
|
|
4255
4558
|
return result;
|
|
4256
4559
|
}
|
|
4560
|
+
makeTypedJsonFilterSchema(type, optional, array) {
|
|
4561
|
+
const typeDef = getTypeDef(this.schema, type);
|
|
4562
|
+
invariant7(typeDef, `Type definition "${type}" not found in schema`);
|
|
4563
|
+
const candidates = [];
|
|
4564
|
+
if (!array) {
|
|
4565
|
+
const fieldSchemas = {};
|
|
4566
|
+
for (const [fieldName, fieldDef] of Object.entries(typeDef.fields)) {
|
|
4567
|
+
if (this.isTypeDefType(fieldDef.type)) {
|
|
4568
|
+
fieldSchemas[fieldName] = this.makeTypedJsonFilterSchema(fieldDef.type, !!fieldDef.optional, !!fieldDef.array).optional();
|
|
4569
|
+
} else {
|
|
4570
|
+
if (fieldDef.array) {
|
|
4571
|
+
fieldSchemas[fieldName] = this.makeArrayFilterSchema(fieldDef.type).optional();
|
|
4572
|
+
} else {
|
|
4573
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
4574
|
+
if (enumDef) {
|
|
4575
|
+
fieldSchemas[fieldName] = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, false).optional();
|
|
4576
|
+
} else {
|
|
4577
|
+
fieldSchemas[fieldName] = this.makePrimitiveFilterSchema(fieldDef.type, !!fieldDef.optional, false).optional();
|
|
4578
|
+
}
|
|
4579
|
+
}
|
|
4580
|
+
}
|
|
4581
|
+
}
|
|
4582
|
+
candidates.push(z3.strictObject(fieldSchemas));
|
|
4583
|
+
}
|
|
4584
|
+
const recursiveSchema = z3.lazy(() => this.makeTypedJsonFilterSchema(type, optional, false)).optional();
|
|
4585
|
+
if (array) {
|
|
4586
|
+
candidates.push(z3.strictObject({
|
|
4587
|
+
some: recursiveSchema,
|
|
4588
|
+
every: recursiveSchema,
|
|
4589
|
+
none: recursiveSchema
|
|
4590
|
+
}));
|
|
4591
|
+
} else {
|
|
4592
|
+
candidates.push(z3.strictObject({
|
|
4593
|
+
is: recursiveSchema,
|
|
4594
|
+
isNot: recursiveSchema
|
|
4595
|
+
}));
|
|
4596
|
+
}
|
|
4597
|
+
candidates.push(this.makeJsonFilterSchema(optional));
|
|
4598
|
+
if (optional) {
|
|
4599
|
+
candidates.push(z3.null());
|
|
4600
|
+
}
|
|
4601
|
+
return z3.union(candidates);
|
|
4602
|
+
}
|
|
4603
|
+
isTypeDefType(type) {
|
|
4604
|
+
return this.schema.typeDefs && type in this.schema.typeDefs;
|
|
4605
|
+
}
|
|
4257
4606
|
makeEnumFilterSchema(enumDef, optional, withAggregations) {
|
|
4258
|
-
const baseSchema =
|
|
4259
|
-
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () =>
|
|
4607
|
+
const baseSchema = z3.enum(Object.keys(enumDef.values));
|
|
4608
|
+
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z3.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations)), [
|
|
4260
4609
|
"equals",
|
|
4261
4610
|
"in",
|
|
4262
4611
|
"notIn",
|
|
@@ -4266,41 +4615,80 @@ var InputValidator = class {
|
|
|
4266
4615
|
"_min",
|
|
4267
4616
|
"_max"
|
|
4268
4617
|
] : void 0);
|
|
4269
|
-
return
|
|
4618
|
+
return z3.union([
|
|
4270
4619
|
this.nullableIf(baseSchema, optional),
|
|
4271
|
-
|
|
4620
|
+
z3.strictObject(components)
|
|
4272
4621
|
]);
|
|
4273
4622
|
}
|
|
4274
4623
|
makeArrayFilterSchema(type) {
|
|
4275
|
-
return
|
|
4276
|
-
equals: this.
|
|
4277
|
-
has: this.
|
|
4278
|
-
hasEvery: this.
|
|
4279
|
-
hasSome: this.
|
|
4280
|
-
isEmpty:
|
|
4624
|
+
return z3.strictObject({
|
|
4625
|
+
equals: this.makeScalarSchema(type).array().optional(),
|
|
4626
|
+
has: this.makeScalarSchema(type).optional(),
|
|
4627
|
+
hasEvery: this.makeScalarSchema(type).array().optional(),
|
|
4628
|
+
hasSome: this.makeScalarSchema(type).array().optional(),
|
|
4629
|
+
isEmpty: z3.boolean().optional()
|
|
4281
4630
|
});
|
|
4282
4631
|
}
|
|
4283
4632
|
makePrimitiveFilterSchema(type, optional, withAggregations) {
|
|
4284
|
-
|
|
4285
|
-
|
|
4286
|
-
|
|
4287
|
-
|
|
4288
|
-
|
|
4289
|
-
|
|
4290
|
-
|
|
4633
|
+
return match13(type).with("String", () => this.makeStringFilterSchema(optional, withAggregations)).with(P3.union("Int", "Float", "Decimal", "BigInt"), (type2) => this.makeNumberFilterSchema(this.makeScalarSchema(type2), optional, withAggregations)).with("Boolean", () => this.makeBooleanFilterSchema(optional, withAggregations)).with("DateTime", () => this.makeDateTimeFilterSchema(optional, withAggregations)).with("Bytes", () => this.makeBytesFilterSchema(optional, withAggregations)).with("Json", () => this.makeJsonFilterSchema(optional)).with("Unsupported", () => z3.never()).exhaustive();
|
|
4634
|
+
}
|
|
4635
|
+
makeJsonValueSchema(nullable, forFilter) {
|
|
4636
|
+
const options = [
|
|
4637
|
+
z3.string(),
|
|
4638
|
+
z3.number(),
|
|
4639
|
+
z3.boolean(),
|
|
4640
|
+
z3.instanceof(JsonNullClass)
|
|
4641
|
+
];
|
|
4642
|
+
if (forFilter) {
|
|
4643
|
+
options.push(z3.instanceof(DbNullClass));
|
|
4644
|
+
} else {
|
|
4645
|
+
if (nullable) {
|
|
4646
|
+
options.push(z3.instanceof(DbNullClass));
|
|
4647
|
+
}
|
|
4648
|
+
}
|
|
4649
|
+
if (forFilter) {
|
|
4650
|
+
options.push(z3.instanceof(AnyNullClass));
|
|
4651
|
+
}
|
|
4652
|
+
const schema = z3.union([
|
|
4653
|
+
...options,
|
|
4654
|
+
z3.lazy(() => z3.union([
|
|
4655
|
+
this.makeJsonValueSchema(false, false),
|
|
4656
|
+
z3.null()
|
|
4657
|
+
]).array()),
|
|
4658
|
+
z3.record(z3.string(), z3.lazy(() => z3.union([
|
|
4659
|
+
this.makeJsonValueSchema(false, false),
|
|
4660
|
+
z3.null()
|
|
4661
|
+
])))
|
|
4662
|
+
]);
|
|
4663
|
+
return this.nullableIf(schema, nullable);
|
|
4664
|
+
}
|
|
4665
|
+
makeJsonFilterSchema(optional) {
|
|
4666
|
+
const valueSchema = this.makeJsonValueSchema(optional, true);
|
|
4667
|
+
return z3.strictObject({
|
|
4668
|
+
path: z3.string().optional(),
|
|
4669
|
+
equals: valueSchema.optional(),
|
|
4670
|
+
not: valueSchema.optional(),
|
|
4671
|
+
string_contains: z3.string().optional(),
|
|
4672
|
+
string_starts_with: z3.string().optional(),
|
|
4673
|
+
string_ends_with: z3.string().optional(),
|
|
4674
|
+
mode: this.makeStringModeSchema().optional(),
|
|
4675
|
+
array_contains: valueSchema.optional(),
|
|
4676
|
+
array_starts_with: valueSchema.optional(),
|
|
4677
|
+
array_ends_with: valueSchema.optional()
|
|
4678
|
+
});
|
|
4291
4679
|
}
|
|
4292
4680
|
makeDateTimeFilterSchema(optional, withAggregations) {
|
|
4293
|
-
return this.makeCommonPrimitiveFilterSchema(
|
|
4294
|
-
|
|
4295
|
-
|
|
4296
|
-
]), optional, () =>
|
|
4681
|
+
return this.makeCommonPrimitiveFilterSchema(z3.union([
|
|
4682
|
+
z3.iso.datetime(),
|
|
4683
|
+
z3.date()
|
|
4684
|
+
]), optional, () => z3.lazy(() => this.makeDateTimeFilterSchema(optional, withAggregations)), withAggregations ? [
|
|
4297
4685
|
"_count",
|
|
4298
4686
|
"_min",
|
|
4299
4687
|
"_max"
|
|
4300
4688
|
] : void 0);
|
|
4301
4689
|
}
|
|
4302
4690
|
makeBooleanFilterSchema(optional, withAggregations) {
|
|
4303
|
-
const components = this.makeCommonPrimitiveFilterComponents(
|
|
4691
|
+
const components = this.makeCommonPrimitiveFilterComponents(z3.boolean(), optional, () => z3.lazy(() => this.makeBooleanFilterSchema(optional, withAggregations)), [
|
|
4304
4692
|
"equals",
|
|
4305
4693
|
"not"
|
|
4306
4694
|
], withAggregations ? [
|
|
@@ -4308,14 +4696,14 @@ var InputValidator = class {
|
|
|
4308
4696
|
"_min",
|
|
4309
4697
|
"_max"
|
|
4310
4698
|
] : void 0);
|
|
4311
|
-
return
|
|
4312
|
-
this.nullableIf(
|
|
4313
|
-
|
|
4699
|
+
return z3.union([
|
|
4700
|
+
this.nullableIf(z3.boolean(), optional),
|
|
4701
|
+
z3.strictObject(components)
|
|
4314
4702
|
]);
|
|
4315
4703
|
}
|
|
4316
4704
|
makeBytesFilterSchema(optional, withAggregations) {
|
|
4317
|
-
const baseSchema =
|
|
4318
|
-
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () =>
|
|
4705
|
+
const baseSchema = z3.instanceof(Uint8Array);
|
|
4706
|
+
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z3.instanceof(Uint8Array), [
|
|
4319
4707
|
"equals",
|
|
4320
4708
|
"in",
|
|
4321
4709
|
"notIn",
|
|
@@ -4325,9 +4713,9 @@ var InputValidator = class {
|
|
|
4325
4713
|
"_min",
|
|
4326
4714
|
"_max"
|
|
4327
4715
|
] : void 0);
|
|
4328
|
-
return
|
|
4716
|
+
return z3.union([
|
|
4329
4717
|
this.nullableIf(baseSchema, optional),
|
|
4330
|
-
|
|
4718
|
+
z3.strictObject(components)
|
|
4331
4719
|
]);
|
|
4332
4720
|
}
|
|
4333
4721
|
makeCommonPrimitiveFilterComponents(baseSchema, optional, makeThis, supportedOperators = void 0, withAggregations = void 0) {
|
|
@@ -4343,7 +4731,7 @@ var InputValidator = class {
|
|
|
4343
4731
|
gte: baseSchema.optional(),
|
|
4344
4732
|
not: makeThis().optional(),
|
|
4345
4733
|
...withAggregations?.includes("_count") ? {
|
|
4346
|
-
_count: this.makeNumberFilterSchema(
|
|
4734
|
+
_count: this.makeNumberFilterSchema(z3.number().int(), false, false).optional()
|
|
4347
4735
|
} : {},
|
|
4348
4736
|
...withAggregations?.includes("_avg") ? {
|
|
4349
4737
|
_avg: commonAggSchema()
|
|
@@ -4368,13 +4756,13 @@ var InputValidator = class {
|
|
|
4368
4756
|
return result;
|
|
4369
4757
|
}
|
|
4370
4758
|
makeCommonPrimitiveFilterSchema(baseSchema, optional, makeThis, withAggregations = void 0) {
|
|
4371
|
-
return
|
|
4759
|
+
return z3.union([
|
|
4372
4760
|
this.nullableIf(baseSchema, optional),
|
|
4373
|
-
|
|
4761
|
+
z3.strictObject(this.makeCommonPrimitiveFilterComponents(baseSchema, optional, makeThis, void 0, withAggregations))
|
|
4374
4762
|
]);
|
|
4375
4763
|
}
|
|
4376
4764
|
makeNumberFilterSchema(baseSchema, optional, withAggregations) {
|
|
4377
|
-
return this.makeCommonPrimitiveFilterSchema(baseSchema, optional, () =>
|
|
4765
|
+
return this.makeCommonPrimitiveFilterSchema(baseSchema, optional, () => z3.lazy(() => this.makeNumberFilterSchema(baseSchema, optional, withAggregations)), withAggregations ? [
|
|
4378
4766
|
"_count",
|
|
4379
4767
|
"_avg",
|
|
4380
4768
|
"_sum",
|
|
@@ -4383,17 +4771,17 @@ var InputValidator = class {
|
|
|
4383
4771
|
] : void 0);
|
|
4384
4772
|
}
|
|
4385
4773
|
makeStringFilterSchema(optional, withAggregations) {
|
|
4386
|
-
return
|
|
4387
|
-
this.nullableIf(
|
|
4388
|
-
|
|
4389
|
-
...this.makeCommonPrimitiveFilterComponents(
|
|
4774
|
+
return z3.union([
|
|
4775
|
+
this.nullableIf(z3.string(), optional),
|
|
4776
|
+
z3.strictObject({
|
|
4777
|
+
...this.makeCommonPrimitiveFilterComponents(z3.string(), optional, () => z3.lazy(() => this.makeStringFilterSchema(optional, withAggregations)), void 0, withAggregations ? [
|
|
4390
4778
|
"_count",
|
|
4391
4779
|
"_min",
|
|
4392
4780
|
"_max"
|
|
4393
4781
|
] : void 0),
|
|
4394
|
-
startsWith:
|
|
4395
|
-
endsWith:
|
|
4396
|
-
contains:
|
|
4782
|
+
startsWith: z3.string().optional(),
|
|
4783
|
+
endsWith: z3.string().optional(),
|
|
4784
|
+
contains: z3.string().optional(),
|
|
4397
4785
|
...this.providerSupportsCaseSensitivity ? {
|
|
4398
4786
|
mode: this.makeStringModeSchema().optional()
|
|
4399
4787
|
} : {}
|
|
@@ -4401,9 +4789,9 @@ var InputValidator = class {
|
|
|
4401
4789
|
]);
|
|
4402
4790
|
}
|
|
4403
4791
|
makeStringModeSchema() {
|
|
4404
|
-
return
|
|
4405
|
-
|
|
4406
|
-
|
|
4792
|
+
return z3.union([
|
|
4793
|
+
z3.literal("default"),
|
|
4794
|
+
z3.literal("insensitive")
|
|
4407
4795
|
]);
|
|
4408
4796
|
}
|
|
4409
4797
|
makeSelectSchema(model) {
|
|
@@ -4414,26 +4802,26 @@ var InputValidator = class {
|
|
|
4414
4802
|
if (fieldDef.relation) {
|
|
4415
4803
|
fields[field] = this.makeRelationSelectIncludeSchema(fieldDef).optional();
|
|
4416
4804
|
} else {
|
|
4417
|
-
fields[field] =
|
|
4805
|
+
fields[field] = z3.boolean().optional();
|
|
4418
4806
|
}
|
|
4419
4807
|
}
|
|
4420
4808
|
const _countSchema = this.makeCountSelectionSchema(modelDef);
|
|
4421
4809
|
if (_countSchema) {
|
|
4422
4810
|
fields["_count"] = _countSchema;
|
|
4423
4811
|
}
|
|
4424
|
-
return
|
|
4812
|
+
return z3.strictObject(fields);
|
|
4425
4813
|
}
|
|
4426
4814
|
makeCountSelectionSchema(modelDef) {
|
|
4427
4815
|
const toManyRelations = Object.values(modelDef.fields).filter((def) => def.relation && def.array);
|
|
4428
4816
|
if (toManyRelations.length > 0) {
|
|
4429
|
-
return
|
|
4430
|
-
|
|
4431
|
-
|
|
4432
|
-
select:
|
|
4817
|
+
return z3.union([
|
|
4818
|
+
z3.literal(true),
|
|
4819
|
+
z3.strictObject({
|
|
4820
|
+
select: z3.strictObject(toManyRelations.reduce((acc, fieldDef) => ({
|
|
4433
4821
|
...acc,
|
|
4434
|
-
[fieldDef.name]:
|
|
4435
|
-
|
|
4436
|
-
|
|
4822
|
+
[fieldDef.name]: z3.union([
|
|
4823
|
+
z3.boolean(),
|
|
4824
|
+
z3.strictObject({
|
|
4437
4825
|
where: this.makeWhereSchema(fieldDef.type, false, false)
|
|
4438
4826
|
})
|
|
4439
4827
|
]).optional()
|
|
@@ -4445,17 +4833,17 @@ var InputValidator = class {
|
|
|
4445
4833
|
}
|
|
4446
4834
|
}
|
|
4447
4835
|
makeRelationSelectIncludeSchema(fieldDef) {
|
|
4448
|
-
let objSchema =
|
|
4836
|
+
let objSchema = z3.strictObject({
|
|
4449
4837
|
...fieldDef.array || fieldDef.optional ? {
|
|
4450
4838
|
// to-many relations and optional to-one relations are filterable
|
|
4451
|
-
where:
|
|
4839
|
+
where: z3.lazy(() => this.makeWhereSchema(fieldDef.type, false)).optional()
|
|
4452
4840
|
} : {},
|
|
4453
|
-
select:
|
|
4454
|
-
include:
|
|
4455
|
-
omit:
|
|
4841
|
+
select: z3.lazy(() => this.makeSelectSchema(fieldDef.type)).optional().nullable(),
|
|
4842
|
+
include: z3.lazy(() => this.makeIncludeSchema(fieldDef.type)).optional().nullable(),
|
|
4843
|
+
omit: z3.lazy(() => this.makeOmitSchema(fieldDef.type)).optional().nullable(),
|
|
4456
4844
|
...fieldDef.array ? {
|
|
4457
4845
|
// to-many relations can be ordered, skipped, taken, and cursor-located
|
|
4458
|
-
orderBy:
|
|
4846
|
+
orderBy: z3.lazy(() => this.orArray(this.makeOrderBySchema(fieldDef.type, true, false), true)).optional(),
|
|
4459
4847
|
skip: this.makeSkipSchema().optional(),
|
|
4460
4848
|
take: this.makeTakeSchema().optional(),
|
|
4461
4849
|
cursor: this.makeCursorSchema(fieldDef.type).optional(),
|
|
@@ -4464,8 +4852,8 @@ var InputValidator = class {
|
|
|
4464
4852
|
});
|
|
4465
4853
|
objSchema = this.refineForSelectIncludeMutuallyExclusive(objSchema);
|
|
4466
4854
|
objSchema = this.refineForSelectOmitMutuallyExclusive(objSchema);
|
|
4467
|
-
return
|
|
4468
|
-
|
|
4855
|
+
return z3.union([
|
|
4856
|
+
z3.boolean(),
|
|
4469
4857
|
objSchema
|
|
4470
4858
|
]);
|
|
4471
4859
|
}
|
|
@@ -4476,13 +4864,13 @@ var InputValidator = class {
|
|
|
4476
4864
|
const fieldDef = requireField(this.schema, model, field);
|
|
4477
4865
|
if (!fieldDef.relation) {
|
|
4478
4866
|
if (this.options.allowQueryTimeOmitOverride !== false) {
|
|
4479
|
-
fields[field] =
|
|
4867
|
+
fields[field] = z3.boolean().optional();
|
|
4480
4868
|
} else {
|
|
4481
|
-
fields[field] =
|
|
4869
|
+
fields[field] = z3.literal(true).optional();
|
|
4482
4870
|
}
|
|
4483
4871
|
}
|
|
4484
4872
|
}
|
|
4485
|
-
return
|
|
4873
|
+
return z3.strictObject(fields);
|
|
4486
4874
|
}
|
|
4487
4875
|
makeIncludeSchema(model) {
|
|
4488
4876
|
const modelDef = requireModel(this.schema, model);
|
|
@@ -4497,20 +4885,20 @@ var InputValidator = class {
|
|
|
4497
4885
|
if (_countSchema) {
|
|
4498
4886
|
fields["_count"] = _countSchema;
|
|
4499
4887
|
}
|
|
4500
|
-
return
|
|
4888
|
+
return z3.strictObject(fields);
|
|
4501
4889
|
}
|
|
4502
4890
|
makeOrderBySchema(model, withRelation, WithAggregation) {
|
|
4503
4891
|
const modelDef = requireModel(this.schema, model);
|
|
4504
4892
|
const fields = {};
|
|
4505
|
-
const sort =
|
|
4506
|
-
|
|
4507
|
-
|
|
4893
|
+
const sort = z3.union([
|
|
4894
|
+
z3.literal("asc"),
|
|
4895
|
+
z3.literal("desc")
|
|
4508
4896
|
]);
|
|
4509
4897
|
for (const field of Object.keys(modelDef.fields)) {
|
|
4510
4898
|
const fieldDef = requireField(this.schema, model, field);
|
|
4511
4899
|
if (fieldDef.relation) {
|
|
4512
4900
|
if (withRelation) {
|
|
4513
|
-
fields[field] =
|
|
4901
|
+
fields[field] = z3.lazy(() => {
|
|
4514
4902
|
let relationOrderBy = this.makeOrderBySchema(fieldDef.type, withRelation, WithAggregation);
|
|
4515
4903
|
if (fieldDef.array) {
|
|
4516
4904
|
relationOrderBy = relationOrderBy.extend({
|
|
@@ -4522,13 +4910,13 @@ var InputValidator = class {
|
|
|
4522
4910
|
}
|
|
4523
4911
|
} else {
|
|
4524
4912
|
if (fieldDef.optional) {
|
|
4525
|
-
fields[field] =
|
|
4913
|
+
fields[field] = z3.union([
|
|
4526
4914
|
sort,
|
|
4527
|
-
|
|
4915
|
+
z3.strictObject({
|
|
4528
4916
|
sort,
|
|
4529
|
-
nulls:
|
|
4530
|
-
|
|
4531
|
-
|
|
4917
|
+
nulls: z3.union([
|
|
4918
|
+
z3.literal("first"),
|
|
4919
|
+
z3.literal("last")
|
|
4532
4920
|
])
|
|
4533
4921
|
})
|
|
4534
4922
|
]).optional();
|
|
@@ -4546,15 +4934,15 @@ var InputValidator = class {
|
|
|
4546
4934
|
"_max"
|
|
4547
4935
|
];
|
|
4548
4936
|
for (const agg of aggregationFields) {
|
|
4549
|
-
fields[agg] =
|
|
4937
|
+
fields[agg] = z3.lazy(() => this.makeOrderBySchema(model, true, false).optional());
|
|
4550
4938
|
}
|
|
4551
4939
|
}
|
|
4552
|
-
return
|
|
4940
|
+
return z3.strictObject(fields);
|
|
4553
4941
|
}
|
|
4554
4942
|
makeDistinctSchema(model) {
|
|
4555
4943
|
const modelDef = requireModel(this.schema, model);
|
|
4556
4944
|
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
|
|
4557
|
-
return this.orArray(
|
|
4945
|
+
return this.orArray(z3.enum(nonRelationFields), true);
|
|
4558
4946
|
}
|
|
4559
4947
|
makeCursorSchema(model) {
|
|
4560
4948
|
return this.makeWhereSchema(model, true, true).optional();
|
|
@@ -4563,7 +4951,7 @@ var InputValidator = class {
|
|
|
4563
4951
|
// #region Create
|
|
4564
4952
|
makeCreateSchema(model) {
|
|
4565
4953
|
const dataSchema = this.makeCreateDataSchema(model, false);
|
|
4566
|
-
let schema =
|
|
4954
|
+
let schema = z3.strictObject({
|
|
4567
4955
|
data: dataSchema,
|
|
4568
4956
|
select: this.makeSelectSchema(model).optional().nullable(),
|
|
4569
4957
|
include: this.makeIncludeSchema(model).optional().nullable(),
|
|
@@ -4613,7 +5001,7 @@ var InputValidator = class {
|
|
|
4613
5001
|
excludeFields.push(...oppositeFieldDef.relation.fields);
|
|
4614
5002
|
}
|
|
4615
5003
|
}
|
|
4616
|
-
let fieldSchema =
|
|
5004
|
+
let fieldSchema = z3.lazy(() => this.makeRelationManipulationSchema(fieldDef, excludeFields, "create"));
|
|
4617
5005
|
if (fieldDef.optional || fieldDef.array) {
|
|
4618
5006
|
fieldSchema = fieldSchema.optional();
|
|
4619
5007
|
} else {
|
|
@@ -4636,12 +5024,12 @@ var InputValidator = class {
|
|
|
4636
5024
|
uncheckedVariantFields[field] = fieldSchema;
|
|
4637
5025
|
}
|
|
4638
5026
|
} else {
|
|
4639
|
-
let fieldSchema = this.
|
|
5027
|
+
let fieldSchema = this.makeScalarSchema(fieldDef.type, fieldDef.attributes);
|
|
4640
5028
|
if (fieldDef.array) {
|
|
4641
5029
|
fieldSchema = addListValidation(fieldSchema.array(), fieldDef.attributes);
|
|
4642
|
-
fieldSchema =
|
|
5030
|
+
fieldSchema = z3.union([
|
|
4643
5031
|
fieldSchema,
|
|
4644
|
-
|
|
5032
|
+
z3.strictObject({
|
|
4645
5033
|
set: fieldSchema
|
|
4646
5034
|
})
|
|
4647
5035
|
]).optional();
|
|
@@ -4650,7 +5038,14 @@ var InputValidator = class {
|
|
|
4650
5038
|
fieldSchema = fieldSchema.optional();
|
|
4651
5039
|
}
|
|
4652
5040
|
if (fieldDef.optional) {
|
|
4653
|
-
|
|
5041
|
+
if (fieldDef.type === "Json") {
|
|
5042
|
+
fieldSchema = z3.union([
|
|
5043
|
+
fieldSchema,
|
|
5044
|
+
z3.instanceof(DbNullClass)
|
|
5045
|
+
]);
|
|
5046
|
+
} else {
|
|
5047
|
+
fieldSchema = fieldSchema.nullable();
|
|
5048
|
+
}
|
|
4654
5049
|
}
|
|
4655
5050
|
uncheckedVariantFields[field] = fieldSchema;
|
|
4656
5051
|
if (!fieldDef.foreignKeyFor) {
|
|
@@ -4658,19 +5053,19 @@ var InputValidator = class {
|
|
|
4658
5053
|
}
|
|
4659
5054
|
}
|
|
4660
5055
|
});
|
|
4661
|
-
const uncheckedCreateSchema = this.extraValidationsEnabled ? addCustomValidation(
|
|
4662
|
-
const checkedCreateSchema = this.extraValidationsEnabled ? addCustomValidation(
|
|
5056
|
+
const uncheckedCreateSchema = this.extraValidationsEnabled ? addCustomValidation(z3.strictObject(uncheckedVariantFields), modelDef.attributes) : z3.strictObject(uncheckedVariantFields);
|
|
5057
|
+
const checkedCreateSchema = this.extraValidationsEnabled ? addCustomValidation(z3.strictObject(checkedVariantFields), modelDef.attributes) : z3.strictObject(checkedVariantFields);
|
|
4663
5058
|
if (!hasRelation) {
|
|
4664
5059
|
return this.orArray(uncheckedCreateSchema, canBeArray);
|
|
4665
5060
|
} else {
|
|
4666
|
-
return
|
|
5061
|
+
return z3.union([
|
|
4667
5062
|
uncheckedCreateSchema,
|
|
4668
5063
|
checkedCreateSchema,
|
|
4669
5064
|
...canBeArray ? [
|
|
4670
|
-
|
|
5065
|
+
z3.array(uncheckedCreateSchema)
|
|
4671
5066
|
] : [],
|
|
4672
5067
|
...canBeArray ? [
|
|
4673
|
-
|
|
5068
|
+
z3.array(checkedCreateSchema)
|
|
4674
5069
|
] : []
|
|
4675
5070
|
]);
|
|
4676
5071
|
}
|
|
@@ -4698,12 +5093,12 @@ var InputValidator = class {
|
|
|
4698
5093
|
fields["disconnect"] = this.makeDisconnectDataSchema(fieldType, array).optional();
|
|
4699
5094
|
fields["delete"] = this.makeDeleteRelationDataSchema(fieldType, array, true).optional();
|
|
4700
5095
|
}
|
|
4701
|
-
fields["update"] = array ? this.orArray(
|
|
4702
|
-
where: this.makeWhereSchema(fieldType, true)
|
|
5096
|
+
fields["update"] = array ? this.orArray(z3.strictObject({
|
|
5097
|
+
where: this.makeWhereSchema(fieldType, true),
|
|
4703
5098
|
data: this.makeUpdateDataSchema(fieldType, withoutFields)
|
|
4704
|
-
}), true).optional() :
|
|
4705
|
-
|
|
4706
|
-
where: this.makeWhereSchema(fieldType,
|
|
5099
|
+
}), true).optional() : z3.union([
|
|
5100
|
+
z3.strictObject({
|
|
5101
|
+
where: this.makeWhereSchema(fieldType, false).optional(),
|
|
4707
5102
|
data: this.makeUpdateDataSchema(fieldType, withoutFields)
|
|
4708
5103
|
}),
|
|
4709
5104
|
this.makeUpdateDataSchema(fieldType, withoutFields)
|
|
@@ -4712,21 +5107,21 @@ var InputValidator = class {
|
|
|
4712
5107
|
if (!fieldDef.array) {
|
|
4713
5108
|
upsertWhere = upsertWhere.optional();
|
|
4714
5109
|
}
|
|
4715
|
-
fields["upsert"] = this.orArray(
|
|
5110
|
+
fields["upsert"] = this.orArray(z3.strictObject({
|
|
4716
5111
|
where: upsertWhere,
|
|
4717
5112
|
create: this.makeCreateDataSchema(fieldType, false, withoutFields),
|
|
4718
5113
|
update: this.makeUpdateDataSchema(fieldType, withoutFields)
|
|
4719
5114
|
}), true).optional();
|
|
4720
5115
|
if (array) {
|
|
4721
5116
|
fields["set"] = this.makeSetDataSchema(fieldType, true).optional();
|
|
4722
|
-
fields["updateMany"] = this.orArray(
|
|
5117
|
+
fields["updateMany"] = this.orArray(z3.strictObject({
|
|
4723
5118
|
where: this.makeWhereSchema(fieldType, false, true),
|
|
4724
5119
|
data: this.makeUpdateDataSchema(fieldType, withoutFields)
|
|
4725
5120
|
}), true).optional();
|
|
4726
5121
|
fields["deleteMany"] = this.makeDeleteRelationDataSchema(fieldType, true, false).optional();
|
|
4727
5122
|
}
|
|
4728
5123
|
}
|
|
4729
|
-
return
|
|
5124
|
+
return z3.strictObject(fields);
|
|
4730
5125
|
}
|
|
4731
5126
|
makeSetDataSchema(model, canBeArray) {
|
|
4732
5127
|
return this.orArray(this.makeWhereSchema(model, true), canBeArray);
|
|
@@ -4738,36 +5133,36 @@ var InputValidator = class {
|
|
|
4738
5133
|
if (canBeArray) {
|
|
4739
5134
|
return this.orArray(this.makeWhereSchema(model, true), canBeArray);
|
|
4740
5135
|
} else {
|
|
4741
|
-
return
|
|
4742
|
-
|
|
5136
|
+
return z3.union([
|
|
5137
|
+
z3.boolean(),
|
|
4743
5138
|
this.makeWhereSchema(model, false)
|
|
4744
5139
|
]);
|
|
4745
5140
|
}
|
|
4746
5141
|
}
|
|
4747
5142
|
makeDeleteRelationDataSchema(model, toManyRelation, uniqueFilter) {
|
|
4748
|
-
return toManyRelation ? this.orArray(this.makeWhereSchema(model, uniqueFilter), true) :
|
|
4749
|
-
|
|
5143
|
+
return toManyRelation ? this.orArray(this.makeWhereSchema(model, uniqueFilter), true) : z3.union([
|
|
5144
|
+
z3.boolean(),
|
|
4750
5145
|
this.makeWhereSchema(model, uniqueFilter)
|
|
4751
5146
|
]);
|
|
4752
5147
|
}
|
|
4753
5148
|
makeConnectOrCreateDataSchema(model, canBeArray, withoutFields) {
|
|
4754
5149
|
const whereSchema = this.makeWhereSchema(model, true);
|
|
4755
5150
|
const createSchema = this.makeCreateDataSchema(model, false, withoutFields);
|
|
4756
|
-
return this.orArray(
|
|
5151
|
+
return this.orArray(z3.strictObject({
|
|
4757
5152
|
where: whereSchema,
|
|
4758
5153
|
create: createSchema
|
|
4759
5154
|
}), canBeArray);
|
|
4760
5155
|
}
|
|
4761
5156
|
makeCreateManyDataSchema(model, withoutFields) {
|
|
4762
|
-
return
|
|
5157
|
+
return z3.strictObject({
|
|
4763
5158
|
data: this.makeCreateDataSchema(model, true, withoutFields, true),
|
|
4764
|
-
skipDuplicates:
|
|
5159
|
+
skipDuplicates: z3.boolean().optional()
|
|
4765
5160
|
});
|
|
4766
5161
|
}
|
|
4767
5162
|
// #endregion
|
|
4768
5163
|
// #region Update
|
|
4769
5164
|
makeUpdateSchema(model) {
|
|
4770
|
-
let schema =
|
|
5165
|
+
let schema = z3.strictObject({
|
|
4771
5166
|
where: this.makeWhereSchema(model, true),
|
|
4772
5167
|
data: this.makeUpdateDataSchema(model),
|
|
4773
5168
|
select: this.makeSelectSchema(model).optional().nullable(),
|
|
@@ -4779,10 +5174,10 @@ var InputValidator = class {
|
|
|
4779
5174
|
return schema;
|
|
4780
5175
|
}
|
|
4781
5176
|
makeUpdateManySchema(model) {
|
|
4782
|
-
return
|
|
5177
|
+
return z3.strictObject({
|
|
4783
5178
|
where: this.makeWhereSchema(model, false).optional(),
|
|
4784
5179
|
data: this.makeUpdateDataSchema(model, [], true),
|
|
4785
|
-
limit:
|
|
5180
|
+
limit: z3.number().int().nonnegative().optional()
|
|
4786
5181
|
});
|
|
4787
5182
|
}
|
|
4788
5183
|
makeUpdateManyAndReturnSchema(model) {
|
|
@@ -4795,7 +5190,7 @@ var InputValidator = class {
|
|
|
4795
5190
|
return schema;
|
|
4796
5191
|
}
|
|
4797
5192
|
makeUpsertSchema(model) {
|
|
4798
|
-
let schema =
|
|
5193
|
+
let schema = z3.strictObject({
|
|
4799
5194
|
where: this.makeWhereSchema(model, true),
|
|
4800
5195
|
create: this.makeCreateDataSchema(model, false),
|
|
4801
5196
|
update: this.makeUpdateDataSchema(model),
|
|
@@ -4830,7 +5225,7 @@ var InputValidator = class {
|
|
|
4830
5225
|
excludeFields.push(...oppositeFieldDef.relation.fields);
|
|
4831
5226
|
}
|
|
4832
5227
|
}
|
|
4833
|
-
let fieldSchema =
|
|
5228
|
+
let fieldSchema = z3.lazy(() => this.makeRelationManipulationSchema(fieldDef, excludeFields, "update")).optional();
|
|
4834
5229
|
if (fieldDef.optional && !fieldDef.array) {
|
|
4835
5230
|
fieldSchema = fieldSchema.nullable();
|
|
4836
5231
|
}
|
|
@@ -4839,26 +5234,26 @@ var InputValidator = class {
|
|
|
4839
5234
|
uncheckedVariantFields[field] = fieldSchema;
|
|
4840
5235
|
}
|
|
4841
5236
|
} else {
|
|
4842
|
-
let fieldSchema = this.
|
|
5237
|
+
let fieldSchema = this.makeScalarSchema(fieldDef.type, fieldDef.attributes);
|
|
4843
5238
|
if (this.isNumericField(fieldDef)) {
|
|
4844
|
-
fieldSchema =
|
|
5239
|
+
fieldSchema = z3.union([
|
|
4845
5240
|
fieldSchema,
|
|
4846
|
-
|
|
4847
|
-
set: this.nullableIf(
|
|
4848
|
-
increment:
|
|
4849
|
-
decrement:
|
|
4850
|
-
multiply:
|
|
4851
|
-
divide:
|
|
5241
|
+
z3.object({
|
|
5242
|
+
set: this.nullableIf(z3.number().optional(), !!fieldDef.optional).optional(),
|
|
5243
|
+
increment: z3.number().optional(),
|
|
5244
|
+
decrement: z3.number().optional(),
|
|
5245
|
+
multiply: z3.number().optional(),
|
|
5246
|
+
divide: z3.number().optional()
|
|
4852
5247
|
}).refine((v) => Object.keys(v).length === 1, 'Only one of "set", "increment", "decrement", "multiply", or "divide" can be provided')
|
|
4853
5248
|
]);
|
|
4854
5249
|
}
|
|
4855
5250
|
if (fieldDef.array) {
|
|
4856
5251
|
const arraySchema = addListValidation(fieldSchema.array(), fieldDef.attributes);
|
|
4857
|
-
fieldSchema =
|
|
5252
|
+
fieldSchema = z3.union([
|
|
4858
5253
|
arraySchema,
|
|
4859
|
-
|
|
5254
|
+
z3.object({
|
|
4860
5255
|
set: arraySchema.optional(),
|
|
4861
|
-
push:
|
|
5256
|
+
push: z3.union([
|
|
4862
5257
|
fieldSchema,
|
|
4863
5258
|
fieldSchema.array()
|
|
4864
5259
|
]).optional()
|
|
@@ -4866,7 +5261,14 @@ var InputValidator = class {
|
|
|
4866
5261
|
]);
|
|
4867
5262
|
}
|
|
4868
5263
|
if (fieldDef.optional) {
|
|
4869
|
-
|
|
5264
|
+
if (fieldDef.type === "Json") {
|
|
5265
|
+
fieldSchema = z3.union([
|
|
5266
|
+
fieldSchema,
|
|
5267
|
+
z3.instanceof(DbNullClass)
|
|
5268
|
+
]);
|
|
5269
|
+
} else {
|
|
5270
|
+
fieldSchema = fieldSchema.nullable();
|
|
5271
|
+
}
|
|
4870
5272
|
}
|
|
4871
5273
|
fieldSchema = fieldSchema.optional();
|
|
4872
5274
|
uncheckedVariantFields[field] = fieldSchema;
|
|
@@ -4875,12 +5277,12 @@ var InputValidator = class {
|
|
|
4875
5277
|
}
|
|
4876
5278
|
}
|
|
4877
5279
|
});
|
|
4878
|
-
const uncheckedUpdateSchema = this.extraValidationsEnabled ? addCustomValidation(
|
|
4879
|
-
const checkedUpdateSchema = this.extraValidationsEnabled ? addCustomValidation(
|
|
5280
|
+
const uncheckedUpdateSchema = this.extraValidationsEnabled ? addCustomValidation(z3.strictObject(uncheckedVariantFields), modelDef.attributes) : z3.strictObject(uncheckedVariantFields);
|
|
5281
|
+
const checkedUpdateSchema = this.extraValidationsEnabled ? addCustomValidation(z3.strictObject(checkedVariantFields), modelDef.attributes) : z3.strictObject(checkedVariantFields);
|
|
4880
5282
|
if (!hasRelation) {
|
|
4881
5283
|
return uncheckedUpdateSchema;
|
|
4882
5284
|
} else {
|
|
4883
|
-
return
|
|
5285
|
+
return z3.union([
|
|
4884
5286
|
uncheckedUpdateSchema,
|
|
4885
5287
|
checkedUpdateSchema
|
|
4886
5288
|
]);
|
|
@@ -4889,7 +5291,7 @@ var InputValidator = class {
|
|
|
4889
5291
|
// #endregion
|
|
4890
5292
|
// #region Delete
|
|
4891
5293
|
makeDeleteSchema(model) {
|
|
4892
|
-
let schema =
|
|
5294
|
+
let schema = z3.strictObject({
|
|
4893
5295
|
where: this.makeWhereSchema(model, true),
|
|
4894
5296
|
select: this.makeSelectSchema(model).optional().nullable(),
|
|
4895
5297
|
include: this.makeIncludeSchema(model).optional().nullable(),
|
|
@@ -4900,15 +5302,15 @@ var InputValidator = class {
|
|
|
4900
5302
|
return schema;
|
|
4901
5303
|
}
|
|
4902
5304
|
makeDeleteManySchema(model) {
|
|
4903
|
-
return
|
|
5305
|
+
return z3.object({
|
|
4904
5306
|
where: this.makeWhereSchema(model, false).optional(),
|
|
4905
|
-
limit:
|
|
5307
|
+
limit: z3.number().int().nonnegative().optional()
|
|
4906
5308
|
}).optional();
|
|
4907
5309
|
}
|
|
4908
5310
|
// #endregion
|
|
4909
5311
|
// #region Count
|
|
4910
5312
|
makeCountSchema(model) {
|
|
4911
|
-
return
|
|
5313
|
+
return z3.object({
|
|
4912
5314
|
where: this.makeWhereSchema(model, false).optional(),
|
|
4913
5315
|
skip: this.makeSkipSchema().optional(),
|
|
4914
5316
|
take: this.makeTakeSchema().optional(),
|
|
@@ -4918,12 +5320,12 @@ var InputValidator = class {
|
|
|
4918
5320
|
}
|
|
4919
5321
|
makeCountAggregateInputSchema(model) {
|
|
4920
5322
|
const modelDef = requireModel(this.schema, model);
|
|
4921
|
-
return
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
_all:
|
|
5323
|
+
return z3.union([
|
|
5324
|
+
z3.literal(true),
|
|
5325
|
+
z3.strictObject({
|
|
5326
|
+
_all: z3.literal(true).optional(),
|
|
4925
5327
|
...Object.keys(modelDef.fields).reduce((acc, field) => {
|
|
4926
|
-
acc[field] =
|
|
5328
|
+
acc[field] = z3.literal(true).optional();
|
|
4927
5329
|
return acc;
|
|
4928
5330
|
}, {})
|
|
4929
5331
|
})
|
|
@@ -4932,7 +5334,7 @@ var InputValidator = class {
|
|
|
4932
5334
|
// #endregion
|
|
4933
5335
|
// #region Aggregate
|
|
4934
5336
|
makeAggregateSchema(model) {
|
|
4935
|
-
return
|
|
5337
|
+
return z3.object({
|
|
4936
5338
|
where: this.makeWhereSchema(model, false).optional(),
|
|
4937
5339
|
skip: this.makeSkipSchema().optional(),
|
|
4938
5340
|
take: this.makeTakeSchema().optional(),
|
|
@@ -4946,20 +5348,20 @@ var InputValidator = class {
|
|
|
4946
5348
|
}
|
|
4947
5349
|
makeSumAvgInputSchema(model) {
|
|
4948
5350
|
const modelDef = requireModel(this.schema, model);
|
|
4949
|
-
return
|
|
5351
|
+
return z3.strictObject(Object.keys(modelDef.fields).reduce((acc, field) => {
|
|
4950
5352
|
const fieldDef = requireField(this.schema, model, field);
|
|
4951
5353
|
if (this.isNumericField(fieldDef)) {
|
|
4952
|
-
acc[field] =
|
|
5354
|
+
acc[field] = z3.literal(true).optional();
|
|
4953
5355
|
}
|
|
4954
5356
|
return acc;
|
|
4955
5357
|
}, {}));
|
|
4956
5358
|
}
|
|
4957
5359
|
makeMinMaxInputSchema(model) {
|
|
4958
5360
|
const modelDef = requireModel(this.schema, model);
|
|
4959
|
-
return
|
|
5361
|
+
return z3.strictObject(Object.keys(modelDef.fields).reduce((acc, field) => {
|
|
4960
5362
|
const fieldDef = requireField(this.schema, model, field);
|
|
4961
5363
|
if (!fieldDef.relation && !fieldDef.array) {
|
|
4962
|
-
acc[field] =
|
|
5364
|
+
acc[field] = z3.literal(true).optional();
|
|
4963
5365
|
}
|
|
4964
5366
|
return acc;
|
|
4965
5367
|
}, {}));
|
|
@@ -4967,8 +5369,8 @@ var InputValidator = class {
|
|
|
4967
5369
|
makeGroupBySchema(model) {
|
|
4968
5370
|
const modelDef = requireModel(this.schema, model);
|
|
4969
5371
|
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
|
|
4970
|
-
const bySchema = nonRelationFields.length > 0 ? this.orArray(
|
|
4971
|
-
let schema =
|
|
5372
|
+
const bySchema = nonRelationFields.length > 0 ? this.orArray(z3.enum(nonRelationFields), true) : z3.never();
|
|
5373
|
+
let schema = z3.strictObject({
|
|
4972
5374
|
where: this.makeWhereSchema(model, false).optional(),
|
|
4973
5375
|
orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
|
|
4974
5376
|
by: bySchema,
|
|
@@ -5035,10 +5437,10 @@ var InputValidator = class {
|
|
|
5035
5437
|
// #endregion
|
|
5036
5438
|
// #region Helpers
|
|
5037
5439
|
makeSkipSchema() {
|
|
5038
|
-
return
|
|
5440
|
+
return z3.number().int().nonnegative();
|
|
5039
5441
|
}
|
|
5040
5442
|
makeTakeSchema() {
|
|
5041
|
-
return
|
|
5443
|
+
return z3.number().int();
|
|
5042
5444
|
}
|
|
5043
5445
|
refineForSelectIncludeMutuallyExclusive(schema) {
|
|
5044
5446
|
return schema.refine((value) => !(value["select"] && value["include"]), '"select" and "include" cannot be used together');
|
|
@@ -5050,9 +5452,9 @@ var InputValidator = class {
|
|
|
5050
5452
|
return nullable ? schema.nullable() : schema;
|
|
5051
5453
|
}
|
|
5052
5454
|
orArray(schema, canBeArray) {
|
|
5053
|
-
return canBeArray ?
|
|
5455
|
+
return canBeArray ? z3.union([
|
|
5054
5456
|
schema,
|
|
5055
|
-
|
|
5457
|
+
z3.array(schema)
|
|
5056
5458
|
]) : schema;
|
|
5057
5459
|
}
|
|
5058
5460
|
isNumericField(fieldDef) {
|
|
@@ -6150,18 +6552,19 @@ var textMatch = /* @__PURE__ */ __name((eb, args, { dialect }, method) => {
|
|
|
6150
6552
|
} else {
|
|
6151
6553
|
op = "like";
|
|
6152
6554
|
}
|
|
6555
|
+
const escapedSearch = sql5`REPLACE(REPLACE(REPLACE(CAST(${searchExpr} as text), '\\', '\\\\'), '%', '\\%'), '_', '\\_')`;
|
|
6153
6556
|
searchExpr = match15(method).with("contains", () => eb.fn("CONCAT", [
|
|
6154
6557
|
sql5.lit("%"),
|
|
6155
|
-
|
|
6558
|
+
escapedSearch,
|
|
6156
6559
|
sql5.lit("%")
|
|
6157
6560
|
])).with("startsWith", () => eb.fn("CONCAT", [
|
|
6158
|
-
|
|
6561
|
+
escapedSearch,
|
|
6159
6562
|
sql5.lit("%")
|
|
6160
6563
|
])).with("endsWith", () => eb.fn("CONCAT", [
|
|
6161
6564
|
sql5.lit("%"),
|
|
6162
|
-
|
|
6565
|
+
escapedSearch
|
|
6163
6566
|
])).exhaustive();
|
|
6164
|
-
return
|
|
6567
|
+
return sql5`${fieldExpr} ${sql5.raw(op)} ${searchExpr} escape '\\'`;
|
|
6165
6568
|
}, "textMatch");
|
|
6166
6569
|
var has = /* @__PURE__ */ __name((eb, args) => {
|
|
6167
6570
|
const [field, search2] = args;
|
|
@@ -7391,9 +7794,15 @@ var MatchingExpressionVisitor = class extends ExpressionVisitor {
|
|
|
7391
7794
|
}
|
|
7392
7795
|
};
|
|
7393
7796
|
export {
|
|
7797
|
+
AnyNull,
|
|
7798
|
+
AnyNullClass,
|
|
7394
7799
|
BaseCrudDialect,
|
|
7395
7800
|
CRUD,
|
|
7396
7801
|
CRUD_EXT,
|
|
7802
|
+
DbNull,
|
|
7803
|
+
DbNullClass,
|
|
7804
|
+
JsonNull,
|
|
7805
|
+
JsonNullClass,
|
|
7397
7806
|
kysely_utils_exports as KyselyUtils,
|
|
7398
7807
|
ORMError,
|
|
7399
7808
|
ORMErrorReason,
|