@zenstackhq/orm 3.5.3 → 3.5.5
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 +228 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -6
- package/dist/index.d.ts +21 -6
- package/dist/index.js +107 -20
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -65,7 +65,7 @@ __export(src_exports, {
|
|
|
65
65
|
module.exports = __toCommonJS(src_exports);
|
|
66
66
|
|
|
67
67
|
// src/client/client-impl.ts
|
|
68
|
-
var
|
|
68
|
+
var import_common_helpers15 = require("@zenstackhq/common-helpers");
|
|
69
69
|
var import_kysely12 = require("kysely");
|
|
70
70
|
var import_zod3 = __toESM(require("zod"), 1);
|
|
71
71
|
|
|
@@ -119,7 +119,7 @@ __export(query_utils_exports, {
|
|
|
119
119
|
stripAlias: () => stripAlias,
|
|
120
120
|
tmpAlias: () => tmpAlias
|
|
121
121
|
});
|
|
122
|
-
var
|
|
122
|
+
var import_common_helpers2 = require("@zenstackhq/common-helpers");
|
|
123
123
|
var import_kysely = require("kysely");
|
|
124
124
|
var import_ts_pattern = require("ts-pattern");
|
|
125
125
|
|
|
@@ -128,6 +128,7 @@ var schema_exports = {};
|
|
|
128
128
|
__reExport(schema_exports, require("@zenstackhq/schema"));
|
|
129
129
|
|
|
130
130
|
// src/utils/object-utils.ts
|
|
131
|
+
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
131
132
|
function extractFields(obj, fields) {
|
|
132
133
|
return Object.fromEntries(Object.entries(obj).filter(([key]) => fields.includes(key)));
|
|
133
134
|
}
|
|
@@ -139,6 +140,10 @@ function fieldsToSelectObject(fields) {
|
|
|
139
140
|
]));
|
|
140
141
|
}
|
|
141
142
|
__name(fieldsToSelectObject, "fieldsToSelectObject");
|
|
143
|
+
function isEmptyObject(x) {
|
|
144
|
+
return (0, import_common_helpers.isPlainObject)(x) && Object.keys(x).length === 0;
|
|
145
|
+
}
|
|
146
|
+
__name(isEmptyObject, "isEmptyObject");
|
|
142
147
|
|
|
143
148
|
// src/client/executor/error-processor.ts
|
|
144
149
|
function getDbErrorCode(error) {
|
|
@@ -507,9 +512,9 @@ function getManyToManyRelation(schema, model, field) {
|
|
|
507
512
|
];
|
|
508
513
|
}
|
|
509
514
|
const modelIdFields = requireIdFields(schema, realModel);
|
|
510
|
-
(0,
|
|
515
|
+
(0, import_common_helpers2.invariant)(modelIdFields.length === 1, "Only single-field ID is supported for many-to-many relation");
|
|
511
516
|
const otherIdFields = requireIdFields(schema, fieldDef.type);
|
|
512
|
-
(0,
|
|
517
|
+
(0, import_common_helpers2.invariant)(otherIdFields.length === 1, "Only single-field ID is supported for many-to-many relation");
|
|
513
518
|
return {
|
|
514
519
|
parentFkName: orderedFK[0],
|
|
515
520
|
parentPKName: modelIdFields[0],
|
|
@@ -651,7 +656,7 @@ __name(tmpAlias, "tmpAlias");
|
|
|
651
656
|
|
|
652
657
|
// src/client/crud/operations/base.ts
|
|
653
658
|
var import_cuid2 = require("@paralleldrive/cuid2");
|
|
654
|
-
var
|
|
659
|
+
var import_common_helpers8 = require("@zenstackhq/common-helpers");
|
|
655
660
|
var import_cuid = __toESM(require("cuid"), 1);
|
|
656
661
|
var import_kysely6 = require("kysely");
|
|
657
662
|
var import_nanoid = require("nanoid");
|
|
@@ -741,7 +746,7 @@ var CRUD_EXT = [
|
|
|
741
746
|
var import_ts_pattern3 = require("ts-pattern");
|
|
742
747
|
|
|
743
748
|
// src/client/crud/dialects/mysql.ts
|
|
744
|
-
var
|
|
749
|
+
var import_common_helpers5 = require("@zenstackhq/common-helpers");
|
|
745
750
|
var import_decimal = __toESM(require("decimal.js"), 1);
|
|
746
751
|
var import_kysely3 = require("kysely");
|
|
747
752
|
|
|
@@ -772,10 +777,10 @@ var AnyNullClass = class {
|
|
|
772
777
|
var AnyNull = new AnyNullClass();
|
|
773
778
|
|
|
774
779
|
// src/client/crud/dialects/lateral-join-dialect-base.ts
|
|
775
|
-
var
|
|
780
|
+
var import_common_helpers4 = require("@zenstackhq/common-helpers");
|
|
776
781
|
|
|
777
782
|
// src/client/crud/dialects/base-dialect.ts
|
|
778
|
-
var
|
|
783
|
+
var import_common_helpers3 = require("@zenstackhq/common-helpers");
|
|
779
784
|
var import_kysely2 = require("kysely");
|
|
780
785
|
var import_ts_pattern2 = require("ts-pattern");
|
|
781
786
|
var BaseCrudDialect = class {
|
|
@@ -903,7 +908,31 @@ var BaseCrudDialect = class {
|
|
|
903
908
|
return LOGICAL_COMBINATORS.includes(key);
|
|
904
909
|
}
|
|
905
910
|
buildCompositeFilter(model, modelAlias, key, payload) {
|
|
906
|
-
|
|
911
|
+
const normalizedPayload = (0, import_common_helpers3.enumerate)(payload).filter((el) => {
|
|
912
|
+
if (typeof el === "object" && el !== null && !Array.isArray(el)) {
|
|
913
|
+
const entries = Object.entries(el);
|
|
914
|
+
return entries.some(([, v]) => v !== void 0);
|
|
915
|
+
} else {
|
|
916
|
+
return true;
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
const normalizedFilters = normalizedPayload.map((el) => this.buildFilter(model, modelAlias, el));
|
|
920
|
+
return (0, import_ts_pattern2.match)(key).with("AND", () => {
|
|
921
|
+
if (normalizedFilters.length === 0) {
|
|
922
|
+
return this.true();
|
|
923
|
+
}
|
|
924
|
+
return this.and(...normalizedFilters);
|
|
925
|
+
}).with("OR", () => {
|
|
926
|
+
if (normalizedFilters.length === 0) {
|
|
927
|
+
return this.false();
|
|
928
|
+
}
|
|
929
|
+
return this.or(...normalizedFilters);
|
|
930
|
+
}).with("NOT", () => {
|
|
931
|
+
if (normalizedFilters.length === 0) {
|
|
932
|
+
return this.true();
|
|
933
|
+
}
|
|
934
|
+
return this.not(...normalizedFilters);
|
|
935
|
+
}).exhaustive();
|
|
907
936
|
}
|
|
908
937
|
buildRelationFilter(model, modelAlias, field, fieldDef, payload) {
|
|
909
938
|
if (!fieldDef.array) {
|
|
@@ -970,9 +999,9 @@ var BaseCrudDialect = class {
|
|
|
970
999
|
const m2m = getManyToManyRelation(this.schema, model, field);
|
|
971
1000
|
if (m2m) {
|
|
972
1001
|
const modelIdFields = requireIdFields(this.schema, model);
|
|
973
|
-
(0,
|
|
1002
|
+
(0, import_common_helpers3.invariant)(modelIdFields.length === 1, "many-to-many relation must have exactly one id field");
|
|
974
1003
|
const relationIdFields = requireIdFields(this.schema, relationModel);
|
|
975
|
-
(0,
|
|
1004
|
+
(0, import_common_helpers3.invariant)(relationIdFields.length === 1, "many-to-many relation must have exactly one id field");
|
|
976
1005
|
return eb(this.eb.ref(`${relationFilterSelectAlias}.${relationIdFields[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(this.eb.ref(`${m2m.joinTable}.${m2m.parentFkName}`), "=", this.eb.ref(`${modelAlias}.${modelIdFields[0]}`)));
|
|
977
1006
|
} else {
|
|
978
1007
|
const relationKeyPairs = getRelationForeignKeyFieldPairs(this.schema, model, field);
|
|
@@ -1021,14 +1050,14 @@ var BaseCrudDialect = class {
|
|
|
1021
1050
|
if (_value === void 0) {
|
|
1022
1051
|
continue;
|
|
1023
1052
|
}
|
|
1024
|
-
(0,
|
|
1053
|
+
(0, import_common_helpers3.invariant)(fieldDef.array, "Field must be an array type to build array filter");
|
|
1025
1054
|
const value = this.transformInput(_value, fieldType, true);
|
|
1026
1055
|
let receiver = fieldRef;
|
|
1027
1056
|
if (isEnum(this.schema, fieldType)) {
|
|
1028
1057
|
receiver = this.eb.cast(fieldRef, import_kysely2.sql.raw("text[]"));
|
|
1029
1058
|
}
|
|
1030
1059
|
const buildArray = /* @__PURE__ */ __name((value2) => {
|
|
1031
|
-
(0,
|
|
1060
|
+
(0, import_common_helpers3.invariant)(Array.isArray(value2), "Array filter value must be an array");
|
|
1032
1061
|
return this.buildArrayValue(value2.map((v) => this.eb.val(v)), fieldType);
|
|
1033
1062
|
}, "buildArray");
|
|
1034
1063
|
switch (key) {
|
|
@@ -1067,6 +1096,9 @@ var BaseCrudDialect = class {
|
|
|
1067
1096
|
return this.buildEnumFilter(fieldRef, fieldDef, payload);
|
|
1068
1097
|
}
|
|
1069
1098
|
if (isTypeDef(this.schema, fieldDef.type)) {
|
|
1099
|
+
if (payload instanceof DbNullClass || payload instanceof JsonNullClass || payload instanceof AnyNullClass) {
|
|
1100
|
+
return this.buildJsonValueFilterClause(fieldRef, payload);
|
|
1101
|
+
}
|
|
1070
1102
|
return this.buildJsonFilter(fieldRef, payload, fieldDef);
|
|
1071
1103
|
}
|
|
1072
1104
|
return (0, import_ts_pattern2.match)(fieldDef.type).with("String", () => this.buildStringFilter(fieldRef, payload)).with(import_ts_pattern2.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", () => {
|
|
@@ -1074,7 +1106,7 @@ var BaseCrudDialect = class {
|
|
|
1074
1106
|
}).exhaustive();
|
|
1075
1107
|
}
|
|
1076
1108
|
buildJsonFilter(receiver, filter, fieldDef) {
|
|
1077
|
-
(0,
|
|
1109
|
+
(0, import_common_helpers3.invariant)(filter && typeof filter === "object", "Json filter payload must be an object");
|
|
1078
1110
|
if ([
|
|
1079
1111
|
"path",
|
|
1080
1112
|
"equals",
|
|
@@ -1099,7 +1131,7 @@ var BaseCrudDialect = class {
|
|
|
1099
1131
|
const jsonReceiver = this.buildJsonPathSelection(receiver, path);
|
|
1100
1132
|
const stringReceiver = this.castText(jsonReceiver);
|
|
1101
1133
|
const mode = filter.mode ?? "default";
|
|
1102
|
-
(0,
|
|
1134
|
+
(0, import_common_helpers3.invariant)(mode === "default" || mode === "insensitive", "Invalid JSON filter mode");
|
|
1103
1135
|
for (const [key, value] of Object.entries(filter)) {
|
|
1104
1136
|
switch (key) {
|
|
1105
1137
|
case "equals": {
|
|
@@ -1111,17 +1143,17 @@ var BaseCrudDialect = class {
|
|
|
1111
1143
|
break;
|
|
1112
1144
|
}
|
|
1113
1145
|
case "string_contains": {
|
|
1114
|
-
(0,
|
|
1146
|
+
(0, import_common_helpers3.invariant)(typeof value === "string", "string_contains value must be a string");
|
|
1115
1147
|
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
1116
1148
|
break;
|
|
1117
1149
|
}
|
|
1118
1150
|
case "string_starts_with": {
|
|
1119
|
-
(0,
|
|
1151
|
+
(0, import_common_helpers3.invariant)(typeof value === "string", "string_starts_with value must be a string");
|
|
1120
1152
|
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
1121
1153
|
break;
|
|
1122
1154
|
}
|
|
1123
1155
|
case "string_ends_with": {
|
|
1124
|
-
(0,
|
|
1156
|
+
(0, import_common_helpers3.invariant)(typeof value === "string", "string_ends_with value must be a string");
|
|
1125
1157
|
clauses.push(this.buildJsonStringFilter(stringReceiver, key, value, mode));
|
|
1126
1158
|
break;
|
|
1127
1159
|
}
|
|
@@ -1154,7 +1186,7 @@ var BaseCrudDialect = class {
|
|
|
1154
1186
|
}
|
|
1155
1187
|
}
|
|
1156
1188
|
buildTypedJsonArrayFilter(receiver, filter, typeDefName) {
|
|
1157
|
-
(0,
|
|
1189
|
+
(0, import_common_helpers3.invariant)(filter && typeof filter === "object", "Typed JSON array filter payload must be an object");
|
|
1158
1190
|
const makeExistsPred = /* @__PURE__ */ __name((filter2) => this.buildJsonArrayExistsPredicate(receiver, (elem) => this.buildTypedJsonFilter(elem, filter2, typeDefName, false)), "makeExistsPred");
|
|
1159
1191
|
const makeExistsNegatedPred = /* @__PURE__ */ __name((filter2) => this.buildJsonArrayExistsPredicate(receiver, (elem) => this.eb.not(this.buildTypedJsonFilter(elem, filter2, typeDefName, false))), "makeExistsNegatedPred");
|
|
1160
1192
|
const clauses = [];
|
|
@@ -1173,7 +1205,7 @@ var BaseCrudDialect = class {
|
|
|
1173
1205
|
clauses.push(this.eb.not(makeExistsNegatedPred(value)));
|
|
1174
1206
|
break;
|
|
1175
1207
|
default:
|
|
1176
|
-
(0,
|
|
1208
|
+
(0, import_common_helpers3.invariant)(false, `Invalid typed JSON array filter key: ${key}`);
|
|
1177
1209
|
}
|
|
1178
1210
|
}
|
|
1179
1211
|
return this.and(...clauses);
|
|
@@ -1183,7 +1215,7 @@ var BaseCrudDialect = class {
|
|
|
1183
1215
|
if (filter === null) {
|
|
1184
1216
|
return this.eb(receiver, "=", this.transformInput(null, "Json", false));
|
|
1185
1217
|
}
|
|
1186
|
-
(0,
|
|
1218
|
+
(0, import_common_helpers3.invariant)(filter && typeof filter === "object", "Typed JSON filter payload must be an object");
|
|
1187
1219
|
if ("is" in filter || "isNot" in filter) {
|
|
1188
1220
|
if ("is" in filter && filter.is && typeof filter.is === "object") {
|
|
1189
1221
|
clauses.push(this.buildTypedJsonFilter(receiver, filter.is, typeDefName, false));
|
|
@@ -1195,7 +1227,7 @@ var BaseCrudDialect = class {
|
|
|
1195
1227
|
const typeDef = requireTypeDef(this.schema, typeDefName);
|
|
1196
1228
|
for (const [key, value] of Object.entries(filter)) {
|
|
1197
1229
|
const fieldDef = typeDef.fields[key];
|
|
1198
|
-
(0,
|
|
1230
|
+
(0, import_common_helpers3.invariant)(fieldDef, `Field "${key}" not found in type definition "${typeDefName}"`);
|
|
1199
1231
|
const fieldReceiver = this.buildJsonPathSelection(receiver, `$.${key}`);
|
|
1200
1232
|
if (isTypeDef(this.schema, fieldDef.type)) {
|
|
1201
1233
|
clauses.push(this.buildTypedJsonFilter(fieldReceiver, value, fieldDef.type, !!fieldDef.array));
|
|
@@ -1244,7 +1276,7 @@ var BaseCrudDialect = class {
|
|
|
1244
1276
|
return this.eb(lhs, "=", this.transformInput(rhs, type, false));
|
|
1245
1277
|
}
|
|
1246
1278
|
buildStandardFilter(type, payload, lhs, getRhs, recurse, throwIfInvalid = false, onlyForKeys = void 0, excludeKeys = []) {
|
|
1247
|
-
if (payload === null || !(0,
|
|
1279
|
+
if (payload === null || !(0, import_common_helpers3.isPlainObject)(payload)) {
|
|
1248
1280
|
return {
|
|
1249
1281
|
conditions: [
|
|
1250
1282
|
this.buildValueFilter(lhs, type, payload)
|
|
@@ -1261,24 +1293,27 @@ var BaseCrudDialect = class {
|
|
|
1261
1293
|
if (excludeKeys.includes(op)) {
|
|
1262
1294
|
continue;
|
|
1263
1295
|
}
|
|
1296
|
+
if (value === void 0) {
|
|
1297
|
+
continue;
|
|
1298
|
+
}
|
|
1264
1299
|
const rhs = Array.isArray(value) ? value.map(getRhs) : getRhs(value);
|
|
1265
1300
|
const condition = (0, import_ts_pattern2.match)(op).with("equals", () => rhs === null ? this.eb(lhs, "is", null) : this.eb(lhs, "=", rhs)).with("in", () => {
|
|
1266
|
-
(0,
|
|
1301
|
+
(0, import_common_helpers3.invariant)(Array.isArray(rhs), "right hand side must be an array");
|
|
1267
1302
|
if (rhs.length === 0) {
|
|
1268
1303
|
return this.false();
|
|
1269
1304
|
} else {
|
|
1270
1305
|
return this.eb(lhs, "in", rhs);
|
|
1271
1306
|
}
|
|
1272
1307
|
}).with("notIn", () => {
|
|
1273
|
-
(0,
|
|
1308
|
+
(0, import_common_helpers3.invariant)(Array.isArray(rhs), "right hand side must be an array");
|
|
1274
1309
|
if (rhs.length === 0) {
|
|
1275
1310
|
return this.true();
|
|
1276
1311
|
} else {
|
|
1277
1312
|
return this.eb.not(this.eb(lhs, "in", rhs));
|
|
1278
1313
|
}
|
|
1279
1314
|
}).with("lt", () => this.eb(lhs, "<", rhs)).with("lte", () => this.eb(lhs, "<=", rhs)).with("gt", () => this.eb(lhs, ">", rhs)).with("gte", () => this.eb(lhs, ">=", rhs)).with("between", () => {
|
|
1280
|
-
(0,
|
|
1281
|
-
(0,
|
|
1315
|
+
(0, import_common_helpers3.invariant)(Array.isArray(rhs), "right hand side must be an array");
|
|
1316
|
+
(0, import_common_helpers3.invariant)(rhs.length === 2, "right hand side must have a length of 2");
|
|
1282
1317
|
const [start, end] = rhs;
|
|
1283
1318
|
return this.eb.and([
|
|
1284
1319
|
this.eb(lhs, ">=", start),
|
|
@@ -1318,7 +1353,10 @@ var BaseCrudDialect = class {
|
|
|
1318
1353
|
if (key === "mode" || consumedKeys.includes(key)) {
|
|
1319
1354
|
continue;
|
|
1320
1355
|
}
|
|
1321
|
-
|
|
1356
|
+
if (value === void 0) {
|
|
1357
|
+
continue;
|
|
1358
|
+
}
|
|
1359
|
+
(0, import_common_helpers3.invariant)(typeof value === "string", `${key} value must be a string`);
|
|
1322
1360
|
const escapedValue = this.escapeLikePattern(value);
|
|
1323
1361
|
const condition = (0, import_ts_pattern2.match)(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(() => {
|
|
1324
1362
|
throw createInvalidInputError(`Invalid string filter key: ${key}`);
|
|
@@ -1399,7 +1437,7 @@ var BaseCrudDialect = class {
|
|
|
1399
1437
|
const fieldDef = requireField(this.schema, model2, field);
|
|
1400
1438
|
return fieldDef.originModel ? this.fieldRef(fieldDef.originModel, field, fieldDef.originModel) : this.fieldRef(model2, field, modelAlias2);
|
|
1401
1439
|
}, "buildFieldRef");
|
|
1402
|
-
(0,
|
|
1440
|
+
(0, import_common_helpers3.enumerate)(orderBy).forEach((orderBy2, index) => {
|
|
1403
1441
|
for (const [field, value] of Object.entries(orderBy2)) {
|
|
1404
1442
|
if (!value) {
|
|
1405
1443
|
continue;
|
|
@@ -1411,9 +1449,9 @@ var BaseCrudDialect = class {
|
|
|
1411
1449
|
"_min",
|
|
1412
1450
|
"_max"
|
|
1413
1451
|
].includes(field)) {
|
|
1414
|
-
(0,
|
|
1452
|
+
(0, import_common_helpers3.invariant)(typeof value === "object", `invalid orderBy value for field "${field}"`);
|
|
1415
1453
|
for (const [k, v] of Object.entries(value)) {
|
|
1416
|
-
(0,
|
|
1454
|
+
(0, import_common_helpers3.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1417
1455
|
result = result.orderBy((eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field), this.negateSort(v, negated));
|
|
1418
1456
|
}
|
|
1419
1457
|
continue;
|
|
@@ -1433,7 +1471,7 @@ var BaseCrudDialect = class {
|
|
|
1433
1471
|
throw createInvalidInputError(`invalid orderBy value for field "${field}"`);
|
|
1434
1472
|
}
|
|
1435
1473
|
if ("_count" in value) {
|
|
1436
|
-
(0,
|
|
1474
|
+
(0, import_common_helpers3.invariant)(value._count === "asc" || value._count === "desc", 'invalid orderBy value for field "_count"');
|
|
1437
1475
|
const sort = this.negateSort(value._count, negated);
|
|
1438
1476
|
result = result.orderBy((eb) => {
|
|
1439
1477
|
const subQueryAlias = tmpAlias(`${modelAlias}$ob$${field}$ct`);
|
|
@@ -1490,7 +1528,7 @@ var BaseCrudDialect = class {
|
|
|
1490
1528
|
if (omit && typeof omit === "object" && typeof omit[field] === "boolean") {
|
|
1491
1529
|
return omit[field];
|
|
1492
1530
|
}
|
|
1493
|
-
const uncapModel = (0,
|
|
1531
|
+
const uncapModel = (0, import_common_helpers3.lowerCaseFirst)(model);
|
|
1494
1532
|
const omitConfig = this.options.omit?.[uncapModel] ?? this.options.omit?.[model];
|
|
1495
1533
|
if (omitConfig && typeof omitConfig === "object" && typeof omitConfig[field] === "boolean") {
|
|
1496
1534
|
return omitConfig[field];
|
|
@@ -1538,12 +1576,13 @@ var BaseCrudDialect = class {
|
|
|
1538
1576
|
const fieldDef = requireField(this.schema, model, field);
|
|
1539
1577
|
const fieldModel = fieldDef.type;
|
|
1540
1578
|
let fieldCountQuery;
|
|
1579
|
+
const subQueryAlias = tmpAlias(`${parentAlias}$_${field}$count`);
|
|
1541
1580
|
const m2m = getManyToManyRelation(this.schema, model, field);
|
|
1542
1581
|
if (m2m) {
|
|
1543
|
-
fieldCountQuery = this.buildModelSelect(fieldModel,
|
|
1582
|
+
fieldCountQuery = this.buildModelSelect(fieldModel, subQueryAlias, value, false).innerJoin(m2m.joinTable, (join) => join.onRef(`${m2m.joinTable}.${m2m.otherFkName}`, "=", `${subQueryAlias}.${m2m.otherPKName}`).onRef(`${m2m.joinTable}.${m2m.parentFkName}`, "=", `${parentAlias}.${m2m.parentPKName}`)).select(eb.fn.countAll().as(`_count$${field}`));
|
|
1544
1583
|
} else {
|
|
1545
|
-
fieldCountQuery = this.buildModelSelect(fieldModel,
|
|
1546
|
-
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field,
|
|
1584
|
+
fieldCountQuery = this.buildModelSelect(fieldModel, subQueryAlias, value, false).select(eb.fn.countAll().as(`_count$${field}`));
|
|
1585
|
+
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, field, subQueryAlias);
|
|
1547
1586
|
for (const [left, right] of joinPairs) {
|
|
1548
1587
|
fieldCountQuery = fieldCountQuery.whereRef(left, "=", right);
|
|
1549
1588
|
}
|
|
@@ -1612,7 +1651,7 @@ var BaseCrudDialect = class {
|
|
|
1612
1651
|
if ("computedFields" in this.options) {
|
|
1613
1652
|
const computedFields = this.options.computedFields;
|
|
1614
1653
|
const computedModel = fieldDef.originModel ?? model;
|
|
1615
|
-
computer = computedFields?.[(0,
|
|
1654
|
+
computer = computedFields?.[(0, import_common_helpers3.lowerCaseFirst)(computedModel)]?.[field] ?? computedFields?.[computedModel]?.[field];
|
|
1616
1655
|
}
|
|
1617
1656
|
if (!computer) {
|
|
1618
1657
|
throw createConfigError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
@@ -1691,8 +1730,8 @@ var LateralJoinDialectBase = class extends BaseCrudDialect {
|
|
|
1691
1730
|
if (m2m) {
|
|
1692
1731
|
const parentIds = requireIdFields(this.schema, model);
|
|
1693
1732
|
const relationIds = requireIdFields(this.schema, relationModel);
|
|
1694
|
-
(0,
|
|
1695
|
-
(0,
|
|
1733
|
+
(0, import_common_helpers4.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1734
|
+
(0, import_common_helpers4.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
1696
1735
|
query = query.where((eb) => eb(eb.ref(`${relationModelAlias}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
|
|
1697
1736
|
} else {
|
|
1698
1737
|
const joinPairs = buildJoinPairs(this.schema, model, parentAlias, relationField, relationModelAlias);
|
|
@@ -1846,7 +1885,7 @@ var MySqlCrudDialect = class extends LateralJoinDialectBase {
|
|
|
1846
1885
|
} else if (value instanceof DbNullClass) {
|
|
1847
1886
|
return null;
|
|
1848
1887
|
} else if (value instanceof AnyNullClass) {
|
|
1849
|
-
(0,
|
|
1888
|
+
(0, import_common_helpers5.invariant)(false, "should not reach here: AnyNull is not a valid input value");
|
|
1850
1889
|
}
|
|
1851
1890
|
if (isTypeDef(this.schema, type)) {
|
|
1852
1891
|
if (typeof value !== "string") {
|
|
@@ -1909,14 +1948,14 @@ var MySqlCrudDialect = class extends LateralJoinDialectBase {
|
|
|
1909
1948
|
if (typeof value === "bigint") {
|
|
1910
1949
|
return value;
|
|
1911
1950
|
}
|
|
1912
|
-
(0,
|
|
1951
|
+
(0, import_common_helpers5.invariant)(typeof value === "string" || typeof value === "number", `Expected string or number, got ${typeof value}`);
|
|
1913
1952
|
return BigInt(value);
|
|
1914
1953
|
}
|
|
1915
1954
|
transformDecimal(value) {
|
|
1916
1955
|
if (value instanceof import_decimal.default) {
|
|
1917
1956
|
return value;
|
|
1918
1957
|
}
|
|
1919
|
-
(0,
|
|
1958
|
+
(0, import_common_helpers5.invariant)(typeof value === "string" || typeof value === "number" || value instanceof import_decimal.default, `Expected string, number or Decimal, got ${typeof value}`);
|
|
1920
1959
|
return new import_decimal.default(value);
|
|
1921
1960
|
}
|
|
1922
1961
|
transformOutputDate(value) {
|
|
@@ -2063,7 +2102,7 @@ var MySqlCrudDialect = class extends LateralJoinDialectBase {
|
|
|
2063
2102
|
};
|
|
2064
2103
|
|
|
2065
2104
|
// src/client/crud/dialects/postgresql.ts
|
|
2066
|
-
var
|
|
2105
|
+
var import_common_helpers6 = require("@zenstackhq/common-helpers");
|
|
2067
2106
|
var import_decimal2 = __toESM(require("decimal.js"), 1);
|
|
2068
2107
|
var import_kysely4 = require("kysely");
|
|
2069
2108
|
var import_postgres_array = require("postgres-array");
|
|
@@ -2183,7 +2222,7 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
|
|
|
2183
2222
|
} else if (value instanceof DbNullClass) {
|
|
2184
2223
|
return null;
|
|
2185
2224
|
} else if (value instanceof AnyNullClass) {
|
|
2186
|
-
(0,
|
|
2225
|
+
(0, import_common_helpers6.invariant)(false, "should not reach here: AnyNull is not a valid input value");
|
|
2187
2226
|
}
|
|
2188
2227
|
if (isTypeDef(this.schema, type)) {
|
|
2189
2228
|
if (typeof value !== "string") {
|
|
@@ -2239,14 +2278,14 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
|
|
|
2239
2278
|
if (typeof value === "bigint") {
|
|
2240
2279
|
return value;
|
|
2241
2280
|
}
|
|
2242
|
-
(0,
|
|
2281
|
+
(0, import_common_helpers6.invariant)(typeof value === "string" || typeof value === "number", `Expected string or number, got ${typeof value}`);
|
|
2243
2282
|
return BigInt(value);
|
|
2244
2283
|
}
|
|
2245
2284
|
transformDecimal(value) {
|
|
2246
2285
|
if (value instanceof import_decimal2.default) {
|
|
2247
2286
|
return value;
|
|
2248
2287
|
}
|
|
2249
|
-
(0,
|
|
2288
|
+
(0, import_common_helpers6.invariant)(typeof value === "string" || typeof value === "number" || value instanceof import_decimal2.default, `Expected string, number or Decimal, got ${typeof value}`);
|
|
2250
2289
|
return new import_decimal2.default(value);
|
|
2251
2290
|
}
|
|
2252
2291
|
transformOutputDate(value) {
|
|
@@ -2296,10 +2335,20 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
|
|
|
2296
2335
|
return query;
|
|
2297
2336
|
}
|
|
2298
2337
|
buildJsonObject(value) {
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2338
|
+
const entries = Object.entries(value);
|
|
2339
|
+
const MAX_PAIRS = 50;
|
|
2340
|
+
const buildChunk = /* @__PURE__ */ __name((chunk) => this.eb.fn("jsonb_build_object", chunk.flatMap(([k, v]) => [
|
|
2341
|
+
import_kysely4.sql.lit(k),
|
|
2342
|
+
v
|
|
2343
|
+
])), "buildChunk");
|
|
2344
|
+
if (entries.length <= MAX_PAIRS) {
|
|
2345
|
+
return buildChunk(entries);
|
|
2346
|
+
}
|
|
2347
|
+
const chunks = [];
|
|
2348
|
+
for (let i = 0; i < entries.length; i += MAX_PAIRS) {
|
|
2349
|
+
chunks.push(buildChunk(entries.slice(i, i + MAX_PAIRS)));
|
|
2350
|
+
}
|
|
2351
|
+
return chunks.reduce((acc, chunk) => import_kysely4.sql`${acc} || ${chunk}`);
|
|
2303
2352
|
}
|
|
2304
2353
|
castInt(expression) {
|
|
2305
2354
|
return this.eb.cast(expression, "integer");
|
|
@@ -2459,7 +2508,7 @@ var PostgresCrudDialect = class _PostgresCrudDialect extends LateralJoinDialectB
|
|
|
2459
2508
|
};
|
|
2460
2509
|
|
|
2461
2510
|
// src/client/crud/dialects/sqlite.ts
|
|
2462
|
-
var
|
|
2511
|
+
var import_common_helpers7 = require("@zenstackhq/common-helpers");
|
|
2463
2512
|
var import_decimal3 = __toESM(require("decimal.js"), 1);
|
|
2464
2513
|
var import_kysely5 = require("kysely");
|
|
2465
2514
|
var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
@@ -2502,7 +2551,7 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2502
2551
|
} else if (value instanceof DbNullClass) {
|
|
2503
2552
|
return null;
|
|
2504
2553
|
} else if (value instanceof AnyNullClass) {
|
|
2505
|
-
(0,
|
|
2554
|
+
(0, import_common_helpers7.invariant)(false, "should not reach here: AnyNull is not a valid input value");
|
|
2506
2555
|
}
|
|
2507
2556
|
if (type === "Json" || this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
2508
2557
|
return JSON.stringify(value);
|
|
@@ -2552,14 +2601,14 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2552
2601
|
if (value instanceof import_decimal3.default) {
|
|
2553
2602
|
return value;
|
|
2554
2603
|
}
|
|
2555
|
-
(0,
|
|
2604
|
+
(0, import_common_helpers7.invariant)(typeof value === "string" || typeof value === "number" || value instanceof import_decimal3.default, `Expected string, number or Decimal, got ${typeof value}`);
|
|
2556
2605
|
return new import_decimal3.default(value);
|
|
2557
2606
|
}
|
|
2558
2607
|
transformOutputBigInt(value) {
|
|
2559
2608
|
if (typeof value === "bigint") {
|
|
2560
2609
|
return value;
|
|
2561
2610
|
}
|
|
2562
|
-
(0,
|
|
2611
|
+
(0, import_common_helpers7.invariant)(typeof value === "string" || typeof value === "number", `Expected string or number, got ${typeof value}`);
|
|
2563
2612
|
return BigInt(value);
|
|
2564
2613
|
}
|
|
2565
2614
|
transformOutputBoolean(value) {
|
|
@@ -2675,8 +2724,8 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
2675
2724
|
if (m2m) {
|
|
2676
2725
|
const parentIds = requireIdFields(this.schema, model);
|
|
2677
2726
|
const relationIds = requireIdFields(this.schema, relationModel);
|
|
2678
|
-
(0,
|
|
2679
|
-
(0,
|
|
2727
|
+
(0, import_common_helpers7.invariant)(parentIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
2728
|
+
(0, import_common_helpers7.invariant)(relationIds.length === 1, "many-to-many relation must have exactly one id field");
|
|
2680
2729
|
selectModelQuery = selectModelQuery.where((eb) => eb(eb.ref(`${relationModelAlias}.${relationIds[0]}`), "in", eb.selectFrom(m2m.joinTable).select(`${m2m.joinTable}.${m2m.otherFkName}`).whereRef(`${parentAlias}.${parentIds[0]}`, "=", `${m2m.joinTable}.${m2m.parentFkName}`)));
|
|
2681
2730
|
} else {
|
|
2682
2731
|
const { keyPairs, ownedByModel } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
|
|
@@ -3109,7 +3158,7 @@ var BaseOperationHandler = class {
|
|
|
3109
3158
|
}
|
|
3110
3159
|
});
|
|
3111
3160
|
const discriminatorField = getDiscriminatorField(this.schema, model);
|
|
3112
|
-
(0,
|
|
3161
|
+
(0, import_common_helpers8.invariant)(discriminatorField, `Base model "${model}" must have a discriminator field`);
|
|
3113
3162
|
thisCreateFields[discriminatorField] = forModel;
|
|
3114
3163
|
const baseEntity = await this.create(kysely, model, thisCreateFields, void 0, true);
|
|
3115
3164
|
const idValues = extractIdFields(baseEntity, this.schema, model);
|
|
@@ -3121,8 +3170,8 @@ var BaseOperationHandler = class {
|
|
|
3121
3170
|
}
|
|
3122
3171
|
async buildFkAssignments(kysely, model, relationField, entity) {
|
|
3123
3172
|
const parentFkFields = {};
|
|
3124
|
-
(0,
|
|
3125
|
-
(0,
|
|
3173
|
+
(0, import_common_helpers8.invariant)(relationField, "parentField must be defined if parentModel is defined");
|
|
3174
|
+
(0, import_common_helpers8.invariant)(entity, "parentEntity must be defined if parentModel is defined");
|
|
3126
3175
|
const { keyPairs } = getRelationForeignKeyFieldPairs(this.schema, model, relationField);
|
|
3127
3176
|
for (const pair of keyPairs) {
|
|
3128
3177
|
if (!(pair.pk in entity)) {
|
|
@@ -3151,8 +3200,8 @@ var BaseOperationHandler = class {
|
|
|
3151
3200
|
const leftFirst = leftModel !== rightModel ? leftModel.localeCompare(rightModel) <= 0 : leftField.localeCompare(rightField) <= 0;
|
|
3152
3201
|
const leftIdField = requireIdFields(this.schema, leftModel);
|
|
3153
3202
|
const rightIdField = requireIdFields(this.schema, rightModel);
|
|
3154
|
-
(0,
|
|
3155
|
-
(0,
|
|
3203
|
+
(0, import_common_helpers8.invariant)(leftIdField.length === 1, "many-to-many relation must have exactly one id field");
|
|
3204
|
+
(0, import_common_helpers8.invariant)(rightIdField.length === 1, "many-to-many relation must have exactly one id field");
|
|
3156
3205
|
const leftIdValue = leftEntity[leftIdField[0]];
|
|
3157
3206
|
const rightIdValues = rightEntities.map((e) => e[rightIdField[0]]);
|
|
3158
3207
|
if (action === "connect") {
|
|
@@ -3177,10 +3226,10 @@ var BaseOperationHandler = class {
|
|
|
3177
3226
|
}
|
|
3178
3227
|
}
|
|
3179
3228
|
resetManyToManyRelation(kysely, model, field, parentIds) {
|
|
3180
|
-
(0,
|
|
3229
|
+
(0, import_common_helpers8.invariant)(Object.keys(parentIds).length === 1, "parentIds must have exactly one field");
|
|
3181
3230
|
const parentId = Object.values(parentIds)[0];
|
|
3182
3231
|
const m2m = getManyToManyRelation(this.schema, model, field);
|
|
3183
|
-
(0,
|
|
3232
|
+
(0, import_common_helpers8.invariant)(m2m, "not a many-to-many relation");
|
|
3184
3233
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
3185
3234
|
return kysely.deleteFrom(m2m.joinTable).where(eb(`${m2m.joinTable}.${m2m.parentFkName}`, "=", parentId)).execute();
|
|
3186
3235
|
}
|
|
@@ -3202,7 +3251,7 @@ var BaseOperationHandler = class {
|
|
|
3202
3251
|
}
|
|
3203
3252
|
case "connect": {
|
|
3204
3253
|
const referencedPkFields = relationField.relation.references;
|
|
3205
|
-
(0,
|
|
3254
|
+
(0, import_common_helpers8.invariant)(referencedPkFields, "relation must have fields info");
|
|
3206
3255
|
const extractedFks = extractFields(subPayload, referencedPkFields);
|
|
3207
3256
|
if (Object.keys(extractedFks).length === referencedPkFields.length) {
|
|
3208
3257
|
result = extractedFks;
|
|
@@ -3249,13 +3298,13 @@ var BaseOperationHandler = class {
|
|
|
3249
3298
|
}
|
|
3250
3299
|
switch (action) {
|
|
3251
3300
|
case "create": {
|
|
3252
|
-
for (const item of (0,
|
|
3301
|
+
for (const item of (0, import_common_helpers8.enumerate)(subPayload)) {
|
|
3253
3302
|
await this.create(kysely, relationModel, item, fromRelationContext);
|
|
3254
3303
|
}
|
|
3255
3304
|
break;
|
|
3256
3305
|
}
|
|
3257
3306
|
case "createMany": {
|
|
3258
|
-
(0,
|
|
3307
|
+
(0, import_common_helpers8.invariant)(relationFieldDef.array, "relation must be an array for createMany");
|
|
3259
3308
|
await this.createMany(kysely, relationModel, subPayload, false, fromRelationContext);
|
|
3260
3309
|
break;
|
|
3261
3310
|
}
|
|
@@ -3264,7 +3313,7 @@ var BaseOperationHandler = class {
|
|
|
3264
3313
|
break;
|
|
3265
3314
|
}
|
|
3266
3315
|
case "connectOrCreate": {
|
|
3267
|
-
for (const item of (0,
|
|
3316
|
+
for (const item of (0, import_common_helpers8.enumerate)(subPayload)) {
|
|
3268
3317
|
const found = await this.exists(kysely, relationModel, item.where);
|
|
3269
3318
|
if (!found) {
|
|
3270
3319
|
await this.create(kysely, relationModel, item.create, fromRelationContext);
|
|
@@ -3294,11 +3343,11 @@ var BaseOperationHandler = class {
|
|
|
3294
3343
|
}
|
|
3295
3344
|
relationKeyPairs.push(...keyPairs);
|
|
3296
3345
|
}
|
|
3297
|
-
let createData = (0,
|
|
3346
|
+
let createData = (0, import_common_helpers8.enumerate)(input.data).map((item) => {
|
|
3298
3347
|
const newItem = {};
|
|
3299
3348
|
for (const [name, value] of Object.entries(item)) {
|
|
3300
3349
|
const fieldDef = this.requireField(model, name);
|
|
3301
|
-
(0,
|
|
3350
|
+
(0, import_common_helpers8.invariant)(!fieldDef.relation, "createMany does not support relations");
|
|
3302
3351
|
newItem[name] = this.dialect.transformInput(value, fieldDef.type, !!fieldDef.array);
|
|
3303
3352
|
}
|
|
3304
3353
|
if (fromRelation) {
|
|
@@ -3361,7 +3410,7 @@ var BaseOperationHandler = class {
|
|
|
3361
3410
|
const thisCreateRows = [];
|
|
3362
3411
|
const remainingFieldRows = [];
|
|
3363
3412
|
const discriminatorField = getDiscriminatorField(this.schema, model);
|
|
3364
|
-
(0,
|
|
3413
|
+
(0, import_common_helpers8.invariant)(discriminatorField, `Base model "${model}" must have a discriminator field`);
|
|
3365
3414
|
for (const createFields of createRows) {
|
|
3366
3415
|
const thisCreateFields = {};
|
|
3367
3416
|
const remainingFields = {};
|
|
@@ -3400,7 +3449,7 @@ var BaseOperationHandler = class {
|
|
|
3400
3449
|
}
|
|
3401
3450
|
fillGeneratedAndDefaultValues(modelDef, data) {
|
|
3402
3451
|
const fields = modelDef.fields;
|
|
3403
|
-
const values = (0,
|
|
3452
|
+
const values = (0, import_common_helpers8.clone)(data);
|
|
3404
3453
|
for (const [field, fieldDef] of Object.entries(fields)) {
|
|
3405
3454
|
if (fieldDef.originModel) {
|
|
3406
3455
|
continue;
|
|
@@ -3472,7 +3521,7 @@ var BaseOperationHandler = class {
|
|
|
3472
3521
|
throw createInvalidInputError("data must be an object");
|
|
3473
3522
|
}
|
|
3474
3523
|
const parentWhere = await this.buildUpdateParentRelationFilter(kysely, fromRelation);
|
|
3475
|
-
let combinedWhere = where ??
|
|
3524
|
+
let combinedWhere = where ?? true;
|
|
3476
3525
|
if (Object.keys(parentWhere).length > 0) {
|
|
3477
3526
|
combinedWhere = Object.keys(combinedWhere).length > 0 ? {
|
|
3478
3527
|
AND: [
|
|
@@ -3491,7 +3540,7 @@ var BaseOperationHandler = class {
|
|
|
3491
3540
|
const hasNonIgnoredFields = Object.keys(data).some((field) => (isScalarField(this.schema, modelDef.name, field) || isForeignKeyField(this.schema, modelDef.name, field)) && !ignoredFields.has(field));
|
|
3492
3541
|
if (hasNonIgnoredFields) {
|
|
3493
3542
|
if (finalData === data) {
|
|
3494
|
-
finalData = (0,
|
|
3543
|
+
finalData = (0, import_common_helpers8.clone)(data);
|
|
3495
3544
|
}
|
|
3496
3545
|
finalData[fieldName] = this.dialect.transformInput(/* @__PURE__ */ new Date(), "DateTime", false);
|
|
3497
3546
|
autoUpdatedFields.push(fieldName);
|
|
@@ -3594,7 +3643,7 @@ var BaseOperationHandler = class {
|
|
|
3594
3643
|
const updatingIdFields = idFields.some((idField) => idField in updateFields);
|
|
3595
3644
|
if (updatingIdFields) {
|
|
3596
3645
|
const origIdValues = await loadThisEntity();
|
|
3597
|
-
(0,
|
|
3646
|
+
(0, import_common_helpers8.invariant)(origIdValues, "Original entity should have been loaded for update without RETURNING");
|
|
3598
3647
|
readFilter = {
|
|
3599
3648
|
...origIdValues
|
|
3600
3649
|
};
|
|
@@ -3653,7 +3702,7 @@ var BaseOperationHandler = class {
|
|
|
3653
3702
|
}
|
|
3654
3703
|
} else {
|
|
3655
3704
|
const fromRelationFieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
3656
|
-
(0,
|
|
3705
|
+
(0, import_common_helpers8.invariant)(fromRelationFieldDef.relation?.opposite);
|
|
3657
3706
|
parentWhere[fromRelationFieldDef.relation.opposite] = {
|
|
3658
3707
|
some: fromRelation.ids
|
|
3659
3708
|
};
|
|
@@ -3704,7 +3753,7 @@ var BaseOperationHandler = class {
|
|
|
3704
3753
|
};
|
|
3705
3754
|
}
|
|
3706
3755
|
transformIncrementalUpdate(model, field, fieldDef, payload) {
|
|
3707
|
-
(0,
|
|
3756
|
+
(0, import_common_helpers8.invariant)(Object.keys(payload).length === 1, 'Only one of "set", "increment", "decrement", "multiply", or "divide" can be provided');
|
|
3708
3757
|
const key = Object.keys(payload)[0];
|
|
3709
3758
|
const value = this.dialect.transformInput(payload[key], fieldDef.type, false);
|
|
3710
3759
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
@@ -3714,7 +3763,7 @@ var BaseOperationHandler = class {
|
|
|
3714
3763
|
});
|
|
3715
3764
|
}
|
|
3716
3765
|
transformScalarListUpdate(model, field, fieldDef, payload) {
|
|
3717
|
-
(0,
|
|
3766
|
+
(0, import_common_helpers8.invariant)(Object.keys(payload).length === 1, 'Only one of "set", "push" can be provided');
|
|
3718
3767
|
const key = Object.keys(payload)[0];
|
|
3719
3768
|
const value = this.dialect.transformInput(payload[key], fieldDef.type, true);
|
|
3720
3769
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
@@ -3745,7 +3794,7 @@ var BaseOperationHandler = class {
|
|
|
3745
3794
|
throw createNotSupportedError("Updating with a limit is not supported for polymorphic models");
|
|
3746
3795
|
}
|
|
3747
3796
|
const parentWhere = await this.buildUpdateParentRelationFilter(kysely, fromRelation);
|
|
3748
|
-
let combinedWhere = where ??
|
|
3797
|
+
let combinedWhere = where ?? true;
|
|
3749
3798
|
if (Object.keys(parentWhere).length > 0) {
|
|
3750
3799
|
combinedWhere = Object.keys(combinedWhere).length > 0 ? {
|
|
3751
3800
|
AND: [
|
|
@@ -3874,14 +3923,14 @@ var BaseOperationHandler = class {
|
|
|
3874
3923
|
for (const [key, value] of Object.entries(args)) {
|
|
3875
3924
|
switch (key) {
|
|
3876
3925
|
case "create": {
|
|
3877
|
-
(0,
|
|
3878
|
-
for (const item of (0,
|
|
3926
|
+
(0, import_common_helpers8.invariant)(!Array.isArray(value) || fieldDef.array, "relation must be an array if create is an array");
|
|
3927
|
+
for (const item of (0, import_common_helpers8.enumerate)(value)) {
|
|
3879
3928
|
await this.create(kysely, fieldModel, item, fromRelationContext);
|
|
3880
3929
|
}
|
|
3881
3930
|
break;
|
|
3882
3931
|
}
|
|
3883
3932
|
case "createMany": {
|
|
3884
|
-
(0,
|
|
3933
|
+
(0, import_common_helpers8.invariant)(fieldDef.array, "relation must be an array for createMany");
|
|
3885
3934
|
await this.createMany(kysely, fieldModel, value, false, fromRelationContext);
|
|
3886
3935
|
break;
|
|
3887
3936
|
}
|
|
@@ -3898,12 +3947,12 @@ var BaseOperationHandler = class {
|
|
|
3898
3947
|
break;
|
|
3899
3948
|
}
|
|
3900
3949
|
case "set": {
|
|
3901
|
-
(0,
|
|
3950
|
+
(0, import_common_helpers8.invariant)(fieldDef.array, "relation must be an array");
|
|
3902
3951
|
await this.setRelation(kysely, fieldModel, value, fromRelationContext);
|
|
3903
3952
|
break;
|
|
3904
3953
|
}
|
|
3905
3954
|
case "update": {
|
|
3906
|
-
for (const _item of (0,
|
|
3955
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
3907
3956
|
const item = _item;
|
|
3908
3957
|
let where;
|
|
3909
3958
|
let data;
|
|
@@ -3920,7 +3969,7 @@ var BaseOperationHandler = class {
|
|
|
3920
3969
|
break;
|
|
3921
3970
|
}
|
|
3922
3971
|
case "upsert": {
|
|
3923
|
-
for (const _item of (0,
|
|
3972
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
3924
3973
|
const item = _item;
|
|
3925
3974
|
const updated = await this.update(kysely, fieldModel, item.where, item.update, fromRelationContext, true, false);
|
|
3926
3975
|
if (!updated) {
|
|
@@ -3930,18 +3979,18 @@ var BaseOperationHandler = class {
|
|
|
3930
3979
|
break;
|
|
3931
3980
|
}
|
|
3932
3981
|
case "updateMany": {
|
|
3933
|
-
for (const _item of (0,
|
|
3982
|
+
for (const _item of (0, import_common_helpers8.enumerate)(value)) {
|
|
3934
3983
|
const item = _item;
|
|
3935
3984
|
await this.updateMany(kysely, fieldModel, item.where, item.data, item.limit, false, fieldModel, fromRelationContext);
|
|
3936
3985
|
}
|
|
3937
3986
|
break;
|
|
3938
3987
|
}
|
|
3939
3988
|
case "delete": {
|
|
3940
|
-
await this.deleteRelation(kysely, fieldModel, value, fromRelationContext, true);
|
|
3989
|
+
await this.deleteRelation(kysely, fieldModel, value, fromRelationContext, true, true);
|
|
3941
3990
|
break;
|
|
3942
3991
|
}
|
|
3943
3992
|
case "deleteMany": {
|
|
3944
|
-
await this.deleteRelation(kysely, fieldModel, value, fromRelationContext, false);
|
|
3993
|
+
await this.deleteRelation(kysely, fieldModel, value, fromRelationContext, false, false);
|
|
3945
3994
|
break;
|
|
3946
3995
|
}
|
|
3947
3996
|
default: {
|
|
@@ -3967,7 +4016,7 @@ var BaseOperationHandler = class {
|
|
|
3967
4016
|
} else {
|
|
3968
4017
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
3969
4018
|
if (ownedByModel) {
|
|
3970
|
-
(0,
|
|
4019
|
+
(0, import_common_helpers8.invariant)(_data.length === 1, "only one entity can be connected");
|
|
3971
4020
|
const target = await this.readUnique(kysely, model, {
|
|
3972
4021
|
where: _data[0]
|
|
3973
4022
|
});
|
|
@@ -4004,7 +4053,7 @@ var BaseOperationHandler = class {
|
|
|
4004
4053
|
}
|
|
4005
4054
|
}
|
|
4006
4055
|
async connectOrCreateRelation(kysely, model, data, fromRelation) {
|
|
4007
|
-
const _data = (0,
|
|
4056
|
+
const _data = (0, import_common_helpers8.enumerate)(data);
|
|
4008
4057
|
if (_data.length === 0) {
|
|
4009
4058
|
return;
|
|
4010
4059
|
}
|
|
@@ -4029,6 +4078,10 @@ var BaseOperationHandler = class {
|
|
|
4029
4078
|
true
|
|
4030
4079
|
];
|
|
4031
4080
|
}
|
|
4081
|
+
} else if (isEmptyObject(data)) {
|
|
4082
|
+
disconnectConditions = [
|
|
4083
|
+
true
|
|
4084
|
+
];
|
|
4032
4085
|
} else {
|
|
4033
4086
|
disconnectConditions = this.normalizeRelationManipulationInput(model, data);
|
|
4034
4087
|
if (disconnectConditions.length === 0) {
|
|
@@ -4048,7 +4101,7 @@ var BaseOperationHandler = class {
|
|
|
4048
4101
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
4049
4102
|
const eb = (0, import_kysely6.expressionBuilder)();
|
|
4050
4103
|
if (ownedByModel) {
|
|
4051
|
-
(0,
|
|
4104
|
+
(0, import_common_helpers8.invariant)(disconnectConditions.length === 1, "only one entity can be disconnected");
|
|
4052
4105
|
const condition = disconnectConditions[0];
|
|
4053
4106
|
if (condition === true) {
|
|
4054
4107
|
for (const { fk } of keyPairs) {
|
|
@@ -4151,9 +4204,9 @@ var BaseOperationHandler = class {
|
|
|
4151
4204
|
}
|
|
4152
4205
|
}
|
|
4153
4206
|
}
|
|
4154
|
-
async deleteRelation(kysely, model, data, fromRelation, throwForNotFound) {
|
|
4207
|
+
async deleteRelation(kysely, model, data, fromRelation, uniqueDelete, throwForNotFound) {
|
|
4155
4208
|
let deleteConditions = [];
|
|
4156
|
-
let expectedDeleteCount;
|
|
4209
|
+
let expectedDeleteCount = -1;
|
|
4157
4210
|
if (typeof data === "boolean") {
|
|
4158
4211
|
if (data === false) {
|
|
4159
4212
|
return;
|
|
@@ -4161,6 +4214,15 @@ var BaseOperationHandler = class {
|
|
|
4161
4214
|
deleteConditions = [
|
|
4162
4215
|
true
|
|
4163
4216
|
];
|
|
4217
|
+
if (uniqueDelete) {
|
|
4218
|
+
expectedDeleteCount = 1;
|
|
4219
|
+
}
|
|
4220
|
+
}
|
|
4221
|
+
} else if (isEmptyObject(data)) {
|
|
4222
|
+
deleteConditions = [
|
|
4223
|
+
true
|
|
4224
|
+
];
|
|
4225
|
+
if (uniqueDelete) {
|
|
4164
4226
|
expectedDeleteCount = 1;
|
|
4165
4227
|
}
|
|
4166
4228
|
} else {
|
|
@@ -4168,7 +4230,9 @@ var BaseOperationHandler = class {
|
|
|
4168
4230
|
if (deleteConditions.length === 0) {
|
|
4169
4231
|
return;
|
|
4170
4232
|
}
|
|
4171
|
-
|
|
4233
|
+
if (uniqueDelete) {
|
|
4234
|
+
expectedDeleteCount = deleteConditions.length;
|
|
4235
|
+
}
|
|
4172
4236
|
}
|
|
4173
4237
|
let deleteResult;
|
|
4174
4238
|
let deleteFromModel;
|
|
@@ -4176,7 +4240,7 @@ var BaseOperationHandler = class {
|
|
|
4176
4240
|
if (m2m) {
|
|
4177
4241
|
deleteFromModel = model;
|
|
4178
4242
|
const fieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
4179
|
-
(0,
|
|
4243
|
+
(0, import_common_helpers8.invariant)(fieldDef.relation?.opposite);
|
|
4180
4244
|
deleteResult = await this.delete(kysely, model, {
|
|
4181
4245
|
AND: [
|
|
4182
4246
|
{
|
|
@@ -4200,7 +4264,7 @@ var BaseOperationHandler = class {
|
|
|
4200
4264
|
throw createNotFoundError(fromRelation.model);
|
|
4201
4265
|
}
|
|
4202
4266
|
const fieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
4203
|
-
(0,
|
|
4267
|
+
(0, import_common_helpers8.invariant)(fieldDef.relation?.opposite);
|
|
4204
4268
|
deleteResult = await this.delete(kysely, model, {
|
|
4205
4269
|
AND: [
|
|
4206
4270
|
// filter for parent
|
|
@@ -4228,12 +4292,12 @@ var BaseOperationHandler = class {
|
|
|
4228
4292
|
});
|
|
4229
4293
|
}
|
|
4230
4294
|
}
|
|
4231
|
-
if (throwForNotFound && expectedDeleteCount > (deleteResult.numAffectedRows ?? 0)) {
|
|
4295
|
+
if (throwForNotFound && expectedDeleteCount >= 0 && expectedDeleteCount > (deleteResult.numAffectedRows ?? 0)) {
|
|
4232
4296
|
throw createNotFoundError(deleteFromModel);
|
|
4233
4297
|
}
|
|
4234
4298
|
}
|
|
4235
4299
|
normalizeRelationManipulationInput(model, data) {
|
|
4236
|
-
return (0,
|
|
4300
|
+
return (0, import_common_helpers8.enumerate)(data).map((item) => flattenCompoundUniqueFilters(this.schema, model, item));
|
|
4237
4301
|
}
|
|
4238
4302
|
// #endregion
|
|
4239
4303
|
async delete(kysely, model, where, limit, filterModel, fieldsToReturn) {
|
|
@@ -4354,7 +4418,7 @@ var BaseOperationHandler = class {
|
|
|
4354
4418
|
if (!args) {
|
|
4355
4419
|
return;
|
|
4356
4420
|
}
|
|
4357
|
-
const newArgs = (0,
|
|
4421
|
+
const newArgs = (0, import_common_helpers8.clone)(args);
|
|
4358
4422
|
this.doNormalizeArgs(newArgs);
|
|
4359
4423
|
return newArgs;
|
|
4360
4424
|
}
|
|
@@ -4363,7 +4427,7 @@ var BaseOperationHandler = class {
|
|
|
4363
4427
|
for (const [key, value] of Object.entries(args)) {
|
|
4364
4428
|
if (value === void 0) {
|
|
4365
4429
|
delete args[key];
|
|
4366
|
-
} else if (value && (0,
|
|
4430
|
+
} else if (value && (0, import_common_helpers8.isPlainObject)(value)) {
|
|
4367
4431
|
this.doNormalizeArgs(value);
|
|
4368
4432
|
}
|
|
4369
4433
|
}
|
|
@@ -4997,11 +5061,11 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
4997
5061
|
};
|
|
4998
5062
|
|
|
4999
5063
|
// src/client/crud/validator/validator.ts
|
|
5000
|
-
var
|
|
5064
|
+
var import_common_helpers10 = require("@zenstackhq/common-helpers");
|
|
5001
5065
|
var import_ts_pattern11 = require("ts-pattern");
|
|
5002
5066
|
|
|
5003
5067
|
// src/client/zod/factory.ts
|
|
5004
|
-
var
|
|
5068
|
+
var import_common_helpers9 = require("@zenstackhq/common-helpers");
|
|
5005
5069
|
var import_zod = require("@zenstackhq/zod");
|
|
5006
5070
|
var import_decimal4 = __toESM(require("decimal.js"), 1);
|
|
5007
5071
|
var import_ts_pattern10 = require("ts-pattern");
|
|
@@ -5196,12 +5260,12 @@ var ZodSchemaFactory = class {
|
|
|
5196
5260
|
}
|
|
5197
5261
|
makeEnumSchema(_enum) {
|
|
5198
5262
|
const enumDef = getEnum(this.schema, _enum);
|
|
5199
|
-
(0,
|
|
5263
|
+
(0, import_common_helpers9.invariant)(enumDef, `Enum "${_enum}" not found in schema`);
|
|
5200
5264
|
return import_zod2.z.enum(Object.keys(enumDef.values));
|
|
5201
5265
|
}
|
|
5202
5266
|
makeTypeDefSchema(type) {
|
|
5203
5267
|
const typeDef = getTypeDef(this.schema, type);
|
|
5204
|
-
(0,
|
|
5268
|
+
(0, import_common_helpers9.invariant)(typeDef, `Type definition "${type}" not found in schema`);
|
|
5205
5269
|
const schema = import_zod2.z.looseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
5206
5270
|
let fieldSchema = this.makeScalarSchema(def.type);
|
|
5207
5271
|
if (def.array) {
|
|
@@ -5285,7 +5349,7 @@ var ZodSchemaFactory = class {
|
|
|
5285
5349
|
for (const uniqueField of uniqueFields) {
|
|
5286
5350
|
if ("defs" in uniqueField) {
|
|
5287
5351
|
fields[uniqueField.name] = import_zod2.z.object(Object.fromEntries(Object.entries(uniqueField.defs).map(([key, def]) => {
|
|
5288
|
-
(0,
|
|
5352
|
+
(0, import_common_helpers9.invariant)(!def.relation, "unique field cannot be a relation");
|
|
5289
5353
|
let fieldSchema;
|
|
5290
5354
|
const enumDef = getEnum(this.schema, def.type);
|
|
5291
5355
|
if (enumDef) {
|
|
@@ -5336,7 +5400,7 @@ var ZodSchemaFactory = class {
|
|
|
5336
5400
|
const optional = !!fieldInfo.optional;
|
|
5337
5401
|
const array = !!fieldInfo.array;
|
|
5338
5402
|
const typeDef = getTypeDef(this.schema, type);
|
|
5339
|
-
(0,
|
|
5403
|
+
(0, import_common_helpers9.invariant)(typeDef, `Type definition "${type}" not found in schema`);
|
|
5340
5404
|
const candidates = [];
|
|
5341
5405
|
if (!array) {
|
|
5342
5406
|
const fieldSchemas = {};
|
|
@@ -5377,9 +5441,28 @@ var ZodSchemaFactory = class {
|
|
|
5377
5441
|
candidates.push(this.makeJsonFilterSchema(contextModel, field, optional));
|
|
5378
5442
|
if (optional) {
|
|
5379
5443
|
candidates.push(import_zod2.z.null());
|
|
5444
|
+
candidates.push(import_zod2.z.instanceof(DbNullClass));
|
|
5445
|
+
candidates.push(import_zod2.z.instanceof(JsonNullClass));
|
|
5446
|
+
candidates.push(import_zod2.z.instanceof(AnyNullClass));
|
|
5380
5447
|
}
|
|
5381
5448
|
return import_zod2.z.union(candidates);
|
|
5382
5449
|
}
|
|
5450
|
+
// For optional typed JSON fields, allow DbNull, JsonNull, and null.
|
|
5451
|
+
// z.union doesn't work here because `z.any()` (returned by `makeScalarSchema`)
|
|
5452
|
+
// always wins, so we create a wrapper superRefine instead.
|
|
5453
|
+
// The caller must pass the already-built fieldSchema so that array/list
|
|
5454
|
+
// mutation shapes (set, push, etc.) are preserved.
|
|
5455
|
+
makeNullableTypedJsonMutationSchema(fieldSchema) {
|
|
5456
|
+
return import_zod2.z.any().superRefine((value, ctx) => {
|
|
5457
|
+
if (value instanceof DbNullClass || value instanceof JsonNullClass || value === null || value === void 0) {
|
|
5458
|
+
return;
|
|
5459
|
+
}
|
|
5460
|
+
const parseResult = fieldSchema.safeParse(value);
|
|
5461
|
+
if (!parseResult.success) {
|
|
5462
|
+
parseResult.error.issues.forEach((issue) => ctx.addIssue(issue));
|
|
5463
|
+
}
|
|
5464
|
+
}).optional();
|
|
5465
|
+
}
|
|
5383
5466
|
isTypeDefType(type) {
|
|
5384
5467
|
return this.schema.typeDefs && type in this.schema.typeDefs;
|
|
5385
5468
|
}
|
|
@@ -5388,7 +5471,7 @@ var ZodSchemaFactory = class {
|
|
|
5388
5471
|
const optional = !!fieldInfo.optional;
|
|
5389
5472
|
const array = !!fieldInfo.array;
|
|
5390
5473
|
const enumDef = getEnum(this.schema, enumName);
|
|
5391
|
-
(0,
|
|
5474
|
+
(0, import_common_helpers9.invariant)(enumDef, `Enum "${enumName}" not found in schema`);
|
|
5392
5475
|
const baseSchema = import_zod2.z.enum(Object.keys(enumDef.values));
|
|
5393
5476
|
if (array) {
|
|
5394
5477
|
return this.internalMakeArrayFilterSchema(model, fieldInfo.name, baseSchema);
|
|
@@ -5681,7 +5764,7 @@ var ZodSchemaFactory = class {
|
|
|
5681
5764
|
for (const plugin of this.plugins) {
|
|
5682
5765
|
const resultConfig = plugin.result;
|
|
5683
5766
|
if (resultConfig) {
|
|
5684
|
-
const modelConfig = resultConfig[(0,
|
|
5767
|
+
const modelConfig = resultConfig[(0, import_common_helpers9.lowerCaseFirst)(model)];
|
|
5685
5768
|
if (modelConfig) {
|
|
5686
5769
|
for (const field of Object.keys(modelConfig)) {
|
|
5687
5770
|
fields[field] = import_zod2.z.boolean().optional();
|
|
@@ -5871,6 +5954,8 @@ var ZodSchemaFactory = class {
|
|
|
5871
5954
|
fieldSchema,
|
|
5872
5955
|
import_zod2.z.instanceof(DbNullClass)
|
|
5873
5956
|
]);
|
|
5957
|
+
} else if (this.isTypeDefType(fieldDef.type)) {
|
|
5958
|
+
fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
|
|
5874
5959
|
} else {
|
|
5875
5960
|
fieldSchema = fieldSchema.nullable();
|
|
5876
5961
|
}
|
|
@@ -6107,6 +6192,8 @@ var ZodSchemaFactory = class {
|
|
|
6107
6192
|
fieldSchema,
|
|
6108
6193
|
import_zod2.z.instanceof(DbNullClass)
|
|
6109
6194
|
]);
|
|
6195
|
+
} else if (this.isTypeDefType(fieldDef.type)) {
|
|
6196
|
+
fieldSchema = this.makeNullableTypedJsonMutationSchema(fieldSchema);
|
|
6110
6197
|
} else {
|
|
6111
6198
|
fieldSchema = fieldSchema.nullable();
|
|
6112
6199
|
}
|
|
@@ -6224,7 +6311,7 @@ var ZodSchemaFactory = class {
|
|
|
6224
6311
|
});
|
|
6225
6312
|
let schema = this.mergePluginArgsSchema(baseSchema, "groupBy");
|
|
6226
6313
|
schema = schema.refine((value) => {
|
|
6227
|
-
const bys = (0,
|
|
6314
|
+
const bys = (0, import_common_helpers9.enumerate)(value.by);
|
|
6228
6315
|
if (value.having && typeof value.having === "object") {
|
|
6229
6316
|
for (const [key, val] of Object.entries(value.having)) {
|
|
6230
6317
|
if (AggregateOperators.includes(key)) {
|
|
@@ -6244,8 +6331,8 @@ var ZodSchemaFactory = class {
|
|
|
6244
6331
|
return true;
|
|
6245
6332
|
}, 'fields in "having" must be in "by"');
|
|
6246
6333
|
schema = schema.refine((value) => {
|
|
6247
|
-
const bys = (0,
|
|
6248
|
-
for (const orderBy of (0,
|
|
6334
|
+
const bys = (0, import_common_helpers9.enumerate)(value.by);
|
|
6335
|
+
for (const orderBy of (0, import_common_helpers9.enumerate)(value.orderBy)) {
|
|
6249
6336
|
if (orderBy && Object.keys(orderBy).filter((f) => !AggregateOperators.includes(f)).some((key) => !bys.includes(key))) {
|
|
6250
6337
|
return false;
|
|
6251
6338
|
}
|
|
@@ -6260,7 +6347,7 @@ var ZodSchemaFactory = class {
|
|
|
6260
6347
|
continue;
|
|
6261
6348
|
}
|
|
6262
6349
|
if (LOGICAL_COMBINATORS.includes(key)) {
|
|
6263
|
-
if ((0,
|
|
6350
|
+
if ((0, import_common_helpers9.enumerate)(value).every((v) => this.onlyAggregationFields(v))) {
|
|
6264
6351
|
continue;
|
|
6265
6352
|
}
|
|
6266
6353
|
}
|
|
@@ -6320,8 +6407,8 @@ var ZodSchemaFactory = class {
|
|
|
6320
6407
|
const createSchema = "$create" in plugin.queryArgs && plugin.queryArgs["$create"] ? plugin.queryArgs["$create"] : void 0;
|
|
6321
6408
|
const updateSchema = "$update" in plugin.queryArgs && plugin.queryArgs["$update"] ? plugin.queryArgs["$update"] : void 0;
|
|
6322
6409
|
if (createSchema && updateSchema) {
|
|
6323
|
-
(0,
|
|
6324
|
-
(0,
|
|
6410
|
+
(0, import_common_helpers9.invariant)(createSchema instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6411
|
+
(0, import_common_helpers9.invariant)(updateSchema instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6325
6412
|
result = createSchema.extend(updateSchema.shape);
|
|
6326
6413
|
} else if (createSchema) {
|
|
6327
6414
|
result = createSchema;
|
|
@@ -6342,7 +6429,7 @@ var ZodSchemaFactory = class {
|
|
|
6342
6429
|
} else if ("$all" in plugin.queryArgs && plugin.queryArgs["$all"]) {
|
|
6343
6430
|
result = plugin.queryArgs["$all"];
|
|
6344
6431
|
}
|
|
6345
|
-
(0,
|
|
6432
|
+
(0, import_common_helpers9.invariant)(result === void 0 || result instanceof import_zod2.ZodObject, "Plugin extended query args schema must be a Zod object");
|
|
6346
6433
|
return result;
|
|
6347
6434
|
}
|
|
6348
6435
|
// #endregion
|
|
@@ -6396,7 +6483,7 @@ var ZodSchemaFactory = class {
|
|
|
6396
6483
|
return void 0;
|
|
6397
6484
|
}
|
|
6398
6485
|
const modelsRecord = slicing.models;
|
|
6399
|
-
const modelConfig = modelsRecord[(0,
|
|
6486
|
+
const modelConfig = modelsRecord[(0, import_common_helpers9.lowerCaseFirst)(model)];
|
|
6400
6487
|
if (modelConfig?.fields) {
|
|
6401
6488
|
const fieldConfig = modelConfig.fields[field];
|
|
6402
6489
|
if (fieldConfig) {
|
|
@@ -7003,7 +7090,7 @@ var InputValidator = class {
|
|
|
7003
7090
|
return input;
|
|
7004
7091
|
}
|
|
7005
7092
|
const procDef = (this.client.$schema.procedures ?? {})[proc];
|
|
7006
|
-
(0,
|
|
7093
|
+
(0, import_common_helpers10.invariant)(procDef, `Procedure "${proc}" not found in schema`);
|
|
7007
7094
|
const params = Object.values(procDef.params ?? {});
|
|
7008
7095
|
if (typeof input === "undefined") {
|
|
7009
7096
|
if (params.length === 0) {
|
|
@@ -7264,12 +7351,12 @@ function performanceNow() {
|
|
|
7264
7351
|
__name(performanceNow, "performanceNow");
|
|
7265
7352
|
|
|
7266
7353
|
// src/client/executor/zenstack-query-executor.ts
|
|
7267
|
-
var
|
|
7354
|
+
var import_common_helpers12 = require("@zenstackhq/common-helpers");
|
|
7268
7355
|
var import_kysely9 = require("kysely");
|
|
7269
7356
|
var import_ts_pattern12 = require("ts-pattern");
|
|
7270
7357
|
|
|
7271
7358
|
// src/client/executor/name-mapper.ts
|
|
7272
|
-
var
|
|
7359
|
+
var import_common_helpers11 = require("@zenstackhq/common-helpers");
|
|
7273
7360
|
var import_kysely7 = require("kysely");
|
|
7274
7361
|
var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
7275
7362
|
static {
|
|
@@ -7342,7 +7429,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
7342
7429
|
return super.transformInsertQuery(node, queryId);
|
|
7343
7430
|
}
|
|
7344
7431
|
const model = extractModelName(node.into);
|
|
7345
|
-
(0,
|
|
7432
|
+
(0, import_common_helpers11.invariant)(model, 'InsertQueryNode must have a model name in the "into" clause');
|
|
7346
7433
|
return this.withScope({
|
|
7347
7434
|
model
|
|
7348
7435
|
}, () => {
|
|
@@ -7425,7 +7512,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
7425
7512
|
return super.transformUpdateQuery(node);
|
|
7426
7513
|
}
|
|
7427
7514
|
const model = extractModelName(innerTable);
|
|
7428
|
-
(0,
|
|
7515
|
+
(0, import_common_helpers11.invariant)(model, 'UpdateQueryNode must have a model name in the "table" clause');
|
|
7429
7516
|
return this.withScope({
|
|
7430
7517
|
model,
|
|
7431
7518
|
alias
|
|
@@ -7729,7 +7816,7 @@ var QueryNameMapper = class extends import_kysely7.OperationNodeTransformer {
|
|
|
7729
7816
|
}
|
|
7730
7817
|
requireCurrentScope() {
|
|
7731
7818
|
const scope = this.scopes[this.scopes.length - 1];
|
|
7732
|
-
(0,
|
|
7819
|
+
(0, import_common_helpers11.invariant)(scope, "No scope available");
|
|
7733
7820
|
return scope;
|
|
7734
7821
|
}
|
|
7735
7822
|
// #endregion
|
|
@@ -8196,7 +8283,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8196
8283
|
for (const valuesItem of values.values) {
|
|
8197
8284
|
const rowIds = {};
|
|
8198
8285
|
if (import_kysely9.PrimitiveValueListNode.is(valuesItem)) {
|
|
8199
|
-
(0,
|
|
8286
|
+
(0, import_common_helpers12.invariant)(valuesItem.values.length === columns.length, "Values count must match columns count");
|
|
8200
8287
|
for (const idField of idFields) {
|
|
8201
8288
|
const colIndex = columns.findIndex((col) => col.column.name === idField);
|
|
8202
8289
|
if (colIndex === -1) {
|
|
@@ -8205,7 +8292,7 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8205
8292
|
rowIds[idField] = valuesItem.values[colIndex];
|
|
8206
8293
|
}
|
|
8207
8294
|
} else {
|
|
8208
|
-
(0,
|
|
8295
|
+
(0, import_common_helpers12.invariant)(valuesItem.values.length === columns.length, "Values count must match columns count");
|
|
8209
8296
|
for (const idField of idFields) {
|
|
8210
8297
|
const colIndex = columns.findIndex((col) => col.column.name === idField);
|
|
8211
8298
|
if (colIndex === -1) {
|
|
@@ -8262,17 +8349,17 @@ In such cases, ZenStack cannot reliably determine the IDs of the mutated entitie
|
|
|
8262
8349
|
}
|
|
8263
8350
|
getMutationModel(queryNode) {
|
|
8264
8351
|
return (0, import_ts_pattern12.match)(queryNode).when(import_kysely9.InsertQueryNode.is, (node) => {
|
|
8265
|
-
(0,
|
|
8352
|
+
(0, import_common_helpers12.invariant)(node.into, "InsertQueryNode must have an into clause");
|
|
8266
8353
|
return node.into.table.identifier.name;
|
|
8267
8354
|
}).when(import_kysely9.UpdateQueryNode.is, (node) => {
|
|
8268
|
-
(0,
|
|
8355
|
+
(0, import_common_helpers12.invariant)(node.table, "UpdateQueryNode must have a table");
|
|
8269
8356
|
const { node: tableNode } = stripAlias(node.table);
|
|
8270
|
-
(0,
|
|
8357
|
+
(0, import_common_helpers12.invariant)(import_kysely9.TableNode.is(tableNode), "UpdateQueryNode must use a TableNode");
|
|
8271
8358
|
return tableNode.table.identifier.name;
|
|
8272
8359
|
}).when(import_kysely9.DeleteQueryNode.is, (node) => {
|
|
8273
|
-
(0,
|
|
8360
|
+
(0, import_common_helpers12.invariant)(node.from.froms.length === 1, "Delete query must have exactly one from table");
|
|
8274
8361
|
const { node: tableNode } = stripAlias(node.from.froms[0]);
|
|
8275
|
-
(0,
|
|
8362
|
+
(0, import_common_helpers12.invariant)(import_kysely9.TableNode.is(tableNode), "DeleteQueryNode must use a TableNode");
|
|
8276
8363
|
return tableNode.table.identifier.name;
|
|
8277
8364
|
}).otherwise((node) => {
|
|
8278
8365
|
throw createInternalError(`Invalid query node: ${node}`);
|
|
@@ -8417,7 +8504,7 @@ __export(functions_exports, {
|
|
|
8417
8504
|
search: () => search,
|
|
8418
8505
|
startsWith: () => startsWith
|
|
8419
8506
|
});
|
|
8420
|
-
var
|
|
8507
|
+
var import_common_helpers13 = require("@zenstackhq/common-helpers");
|
|
8421
8508
|
var import_kysely10 = require("kysely");
|
|
8422
8509
|
var import_ts_pattern13 = require("ts-pattern");
|
|
8423
8510
|
var contains = /* @__PURE__ */ __name((eb, args, context) => textMatch(eb, args, context, "contains"), "contains");
|
|
@@ -8527,8 +8614,8 @@ var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
|
|
|
8527
8614
|
}, "currentOperation");
|
|
8528
8615
|
function processCasing(casing, result, model) {
|
|
8529
8616
|
const opNode = casing.toOperationNode();
|
|
8530
|
-
(0,
|
|
8531
|
-
result = (0, import_ts_pattern13.match)(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => (0,
|
|
8617
|
+
(0, import_common_helpers13.invariant)(import_kysely10.ValueNode.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
|
|
8618
|
+
result = (0, import_ts_pattern13.match)(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => (0, import_common_helpers13.upperCaseFirst)(result)).with("uncapitalize", () => (0, import_common_helpers13.lowerCaseFirst)(result)).otherwise(() => {
|
|
8532
8619
|
throw new Error(`Invalid casing value: ${opNode.value}. Must be "original", "upper", "lower", "capitalize", or "uncapitalize".`);
|
|
8533
8620
|
});
|
|
8534
8621
|
return result;
|
|
@@ -8539,13 +8626,13 @@ function readBoolean(expr, defaultValue) {
|
|
|
8539
8626
|
return defaultValue;
|
|
8540
8627
|
}
|
|
8541
8628
|
const opNode = expr.toOperationNode();
|
|
8542
|
-
(0,
|
|
8629
|
+
(0, import_common_helpers13.invariant)(import_kysely10.ValueNode.is(opNode), "expression must be a literal value");
|
|
8543
8630
|
return !!opNode.value;
|
|
8544
8631
|
}
|
|
8545
8632
|
__name(readBoolean, "readBoolean");
|
|
8546
8633
|
|
|
8547
8634
|
// src/client/helpers/schema-db-pusher.ts
|
|
8548
|
-
var
|
|
8635
|
+
var import_common_helpers14 = require("@zenstackhq/common-helpers");
|
|
8549
8636
|
var import_kysely11 = require("kysely");
|
|
8550
8637
|
var import_toposort = __toESM(require("toposort"), 1);
|
|
8551
8638
|
var import_ts_pattern14 = require("ts-pattern");
|
|
@@ -8571,7 +8658,7 @@ var SchemaDbPusher = class {
|
|
|
8571
8658
|
return f.name;
|
|
8572
8659
|
} else {
|
|
8573
8660
|
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
8574
|
-
(0,
|
|
8661
|
+
(0, import_common_helpers14.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
8575
8662
|
return mappedName;
|
|
8576
8663
|
}
|
|
8577
8664
|
});
|
|
@@ -8698,7 +8785,7 @@ var SchemaDbPusher = class {
|
|
|
8698
8785
|
}
|
|
8699
8786
|
addUniqueConstraint(table, modelDef) {
|
|
8700
8787
|
for (const [key, value] of Object.entries(modelDef.uniqueFields)) {
|
|
8701
|
-
(0,
|
|
8788
|
+
(0, import_common_helpers14.invariant)(typeof value === "object", "expecting an object");
|
|
8702
8789
|
if ("type" in value) {
|
|
8703
8790
|
const fieldDef = modelDef.fields[key];
|
|
8704
8791
|
if (fieldDef.unique) {
|
|
@@ -8773,7 +8860,7 @@ var SchemaDbPusher = class {
|
|
|
8773
8860
|
return f.name;
|
|
8774
8861
|
} else {
|
|
8775
8862
|
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
8776
|
-
(0,
|
|
8863
|
+
(0, import_common_helpers14.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
8777
8864
|
return mappedName;
|
|
8778
8865
|
}
|
|
8779
8866
|
});
|
|
@@ -8808,7 +8895,7 @@ var SchemaDbPusher = class {
|
|
|
8808
8895
|
return fieldDef.default && schema_exports.ExpressionUtils.isCall(fieldDef.default) && fieldDef.default.function === "autoincrement";
|
|
8809
8896
|
}
|
|
8810
8897
|
addForeignKeyConstraint(table, model, fieldName, fieldDef) {
|
|
8811
|
-
(0,
|
|
8898
|
+
(0, import_common_helpers14.invariant)(fieldDef.relation, "field must be a relation");
|
|
8812
8899
|
if (!fieldDef.relation.fields || !fieldDef.relation.references) {
|
|
8813
8900
|
return table;
|
|
8814
8901
|
}
|
|
@@ -9129,7 +9216,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9129
9216
|
for (const [modelName, modelDef] of Object.entries(this.$schema.models)) {
|
|
9130
9217
|
if (modelDef.computedFields) {
|
|
9131
9218
|
for (const fieldName of Object.keys(modelDef.computedFields)) {
|
|
9132
|
-
const modelConfig = computedFieldsConfig?.[(0,
|
|
9219
|
+
const modelConfig = computedFieldsConfig?.[(0, import_common_helpers15.lowerCaseFirst)(modelName)] ?? computedFieldsConfig?.[modelName];
|
|
9133
9220
|
const fieldConfig = modelConfig?.[fieldName];
|
|
9134
9221
|
if (fieldConfig === null || fieldConfig === void 0) {
|
|
9135
9222
|
throw createConfigError(`Computed field "${fieldName}" in model "${modelName}" does not have a configuration. Please provide an implementation in the computedFields option.`);
|
|
@@ -9161,7 +9248,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
9161
9248
|
}
|
|
9162
9249
|
// implementation
|
|
9163
9250
|
async $transaction(input, options) {
|
|
9164
|
-
(0,
|
|
9251
|
+
(0, import_common_helpers15.invariant)(typeof input === "function" || Array.isArray(input) && input.every((p) => p.then && p.cb), "Invalid transaction input, expected a function or an array of ZenStackPromise");
|
|
9165
9252
|
if (typeof input === "function") {
|
|
9166
9253
|
return this.interactiveTransaction(input, options);
|
|
9167
9254
|
} else {
|
|
@@ -9560,7 +9647,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
9560
9647
|
};
|
|
9561
9648
|
const slicing = client.$options.slicing;
|
|
9562
9649
|
if (slicing?.models) {
|
|
9563
|
-
const modelSlicing = slicing.models[(0,
|
|
9650
|
+
const modelSlicing = slicing.models[(0, import_common_helpers15.lowerCaseFirst)(model)];
|
|
9564
9651
|
const allSlicing = slicing.models.$all;
|
|
9565
9652
|
const includedOperations = modelSlicing?.includedOperations ?? allSlicing?.includedOperations;
|
|
9566
9653
|
const excludedOperations = modelSlicing?.excludedOperations ?? allSlicing?.excludedOperations;
|
|
@@ -9611,7 +9698,7 @@ function collectExtResultFieldDefs(model, schema, plugins) {
|
|
|
9611
9698
|
for (const plugin of plugins) {
|
|
9612
9699
|
const resultConfig = plugin.result;
|
|
9613
9700
|
if (resultConfig) {
|
|
9614
|
-
const modelConfig = resultConfig[(0,
|
|
9701
|
+
const modelConfig = resultConfig[(0, import_common_helpers15.lowerCaseFirst)(model)];
|
|
9615
9702
|
if (modelConfig) {
|
|
9616
9703
|
for (const [fieldName, fieldDef] of Object.entries(modelConfig)) {
|
|
9617
9704
|
if (getField(schema, model, fieldName)) {
|