@zenstackhq/orm 3.0.0-beta.19 → 3.0.0-beta.21
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 +479 -194
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +172 -140
- package/dist/index.d.ts +172 -140
- package/dist/index.js +483 -194
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -35,18 +35,14 @@ __export(src_exports, {
|
|
|
35
35
|
BaseCrudDialect: () => BaseCrudDialect,
|
|
36
36
|
CRUD: () => CRUD,
|
|
37
37
|
CRUD_EXT: () => CRUD_EXT,
|
|
38
|
-
InputValidationError: () => InputValidationError,
|
|
39
|
-
InternalError: () => InternalError,
|
|
40
38
|
KyselyUtils: () => kysely_utils_exports,
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
ORMError: () => ORMError,
|
|
40
|
+
ORMErrorReason: () => ORMErrorReason,
|
|
43
41
|
QueryUtils: () => query_utils_exports,
|
|
44
|
-
RejectedByPolicyError: () => RejectedByPolicyError,
|
|
45
42
|
RejectedByPolicyReason: () => RejectedByPolicyReason,
|
|
46
43
|
SchemaUtils: () => schema_utils_exports,
|
|
47
44
|
TransactionIsolationLevel: () => TransactionIsolationLevel,
|
|
48
45
|
ZenStackClient: () => ZenStackClient,
|
|
49
|
-
ZenStackError: () => ZenStackError,
|
|
50
46
|
definePlugin: () => definePlugin,
|
|
51
47
|
getCrudDialect: () => getCrudDialect
|
|
52
48
|
});
|
|
@@ -114,63 +110,110 @@ function fieldsToSelectObject(fields) {
|
|
|
114
110
|
}
|
|
115
111
|
__name(fieldsToSelectObject, "fieldsToSelectObject");
|
|
116
112
|
|
|
117
|
-
// src/client/
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
var InputValidationError = class extends ZenStackError {
|
|
124
|
-
static {
|
|
125
|
-
__name(this, "InputValidationError");
|
|
126
|
-
}
|
|
127
|
-
model;
|
|
128
|
-
constructor(model, message, cause) {
|
|
129
|
-
super(message, {
|
|
130
|
-
cause
|
|
131
|
-
}), this.model = model;
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
|
-
var QueryError = class extends ZenStackError {
|
|
135
|
-
static {
|
|
136
|
-
__name(this, "QueryError");
|
|
137
|
-
}
|
|
138
|
-
constructor(message, cause) {
|
|
139
|
-
super(message, {
|
|
140
|
-
cause
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
var InternalError = class extends ZenStackError {
|
|
145
|
-
static {
|
|
146
|
-
__name(this, "InternalError");
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
var NotFoundError = class extends ZenStackError {
|
|
150
|
-
static {
|
|
151
|
-
__name(this, "NotFoundError");
|
|
152
|
-
}
|
|
153
|
-
model;
|
|
154
|
-
constructor(model, details) {
|
|
155
|
-
super(`Entity not found for model "${model}"${details ? `: ${details}` : ""}`), this.model = model;
|
|
113
|
+
// src/client/executor/error-processor.ts
|
|
114
|
+
function getDbErrorCode(error) {
|
|
115
|
+
if (error instanceof Error && "code" in error) {
|
|
116
|
+
return error.code;
|
|
117
|
+
} else {
|
|
118
|
+
return void 0;
|
|
156
119
|
}
|
|
157
|
-
}
|
|
120
|
+
}
|
|
121
|
+
__name(getDbErrorCode, "getDbErrorCode");
|
|
122
|
+
|
|
123
|
+
// src/client/errors.ts
|
|
124
|
+
var ORMErrorReason = /* @__PURE__ */ function(ORMErrorReason2) {
|
|
125
|
+
ORMErrorReason2["CONFIG_ERROR"] = "config-error";
|
|
126
|
+
ORMErrorReason2["INVALID_INPUT"] = "invalid-input";
|
|
127
|
+
ORMErrorReason2["NOT_FOUND"] = "not-found";
|
|
128
|
+
ORMErrorReason2["REJECTED_BY_POLICY"] = "rejected-by-policy";
|
|
129
|
+
ORMErrorReason2["DB_QUERY_ERROR"] = "db-query-error";
|
|
130
|
+
ORMErrorReason2["NOT_SUPPORTED"] = "not-supported";
|
|
131
|
+
ORMErrorReason2["INTERNAL_ERROR"] = "internal-error";
|
|
132
|
+
return ORMErrorReason2;
|
|
133
|
+
}({});
|
|
158
134
|
var RejectedByPolicyReason = /* @__PURE__ */ function(RejectedByPolicyReason2) {
|
|
159
135
|
RejectedByPolicyReason2["NO_ACCESS"] = "no-access";
|
|
160
136
|
RejectedByPolicyReason2["CANNOT_READ_BACK"] = "cannot-read-back";
|
|
161
137
|
RejectedByPolicyReason2["OTHER"] = "other";
|
|
162
138
|
return RejectedByPolicyReason2;
|
|
163
139
|
}({});
|
|
164
|
-
var
|
|
140
|
+
var ORMError = class extends Error {
|
|
165
141
|
static {
|
|
166
|
-
__name(this, "
|
|
142
|
+
__name(this, "ORMError");
|
|
167
143
|
}
|
|
168
|
-
model;
|
|
169
144
|
reason;
|
|
170
|
-
constructor(
|
|
171
|
-
super(message
|
|
145
|
+
constructor(reason, message, options) {
|
|
146
|
+
super(message, options), this.reason = reason;
|
|
172
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The name of the model that the error pertains to.
|
|
150
|
+
*/
|
|
151
|
+
model;
|
|
152
|
+
/**
|
|
153
|
+
* The error code given by the underlying database driver.
|
|
154
|
+
*/
|
|
155
|
+
dbErrorCode;
|
|
156
|
+
/**
|
|
157
|
+
* The error message given by the underlying database driver.
|
|
158
|
+
*/
|
|
159
|
+
dbErrorMessage;
|
|
160
|
+
/**
|
|
161
|
+
* The reason code for policy rejection. Only available when `reason` is `REJECTED_BY_POLICY`.
|
|
162
|
+
*/
|
|
163
|
+
rejectedByPolicyReason;
|
|
164
|
+
/**
|
|
165
|
+
* The SQL query that was executed. Only available when `reason` is `DB_QUERY_ERROR`.
|
|
166
|
+
*/
|
|
167
|
+
sql;
|
|
168
|
+
/**
|
|
169
|
+
* The parameters used in the SQL query. Only available when `reason` is `DB_QUERY_ERROR`.
|
|
170
|
+
*/
|
|
171
|
+
sqlParams;
|
|
173
172
|
};
|
|
173
|
+
function createConfigError(message, options) {
|
|
174
|
+
return new ORMError("config-error", message, options);
|
|
175
|
+
}
|
|
176
|
+
__name(createConfigError, "createConfigError");
|
|
177
|
+
function createNotFoundError(model, message, options) {
|
|
178
|
+
const error = new ORMError("not-found", message ?? "Record not found", options);
|
|
179
|
+
error.model = model;
|
|
180
|
+
return error;
|
|
181
|
+
}
|
|
182
|
+
__name(createNotFoundError, "createNotFoundError");
|
|
183
|
+
function createInvalidInputError(message, model, options) {
|
|
184
|
+
const error = new ORMError("invalid-input", message, options);
|
|
185
|
+
error.model = model;
|
|
186
|
+
return error;
|
|
187
|
+
}
|
|
188
|
+
__name(createInvalidInputError, "createInvalidInputError");
|
|
189
|
+
function createDBQueryError(message, dbError, sql8, parameters) {
|
|
190
|
+
const error = new ORMError("db-query-error", message, {
|
|
191
|
+
cause: dbError
|
|
192
|
+
});
|
|
193
|
+
error.dbErrorCode = getDbErrorCode(dbError);
|
|
194
|
+
error.dbErrorMessage = dbError instanceof Error ? dbError.message : void 0;
|
|
195
|
+
error.sql = sql8;
|
|
196
|
+
error.sqlParams = parameters;
|
|
197
|
+
return error;
|
|
198
|
+
}
|
|
199
|
+
__name(createDBQueryError, "createDBQueryError");
|
|
200
|
+
function createRejectedByPolicyError(model, reason, message, options) {
|
|
201
|
+
const error = new ORMError("rejected-by-policy", message, options);
|
|
202
|
+
error.model = model;
|
|
203
|
+
error.rejectedByPolicyReason = reason;
|
|
204
|
+
return error;
|
|
205
|
+
}
|
|
206
|
+
__name(createRejectedByPolicyError, "createRejectedByPolicyError");
|
|
207
|
+
function createNotSupportedError(message, options) {
|
|
208
|
+
return new ORMError("not-supported", message, options);
|
|
209
|
+
}
|
|
210
|
+
__name(createNotSupportedError, "createNotSupportedError");
|
|
211
|
+
function createInternalError(message, model, options) {
|
|
212
|
+
const error = new ORMError("internal-error", message, options);
|
|
213
|
+
error.model = model;
|
|
214
|
+
return error;
|
|
215
|
+
}
|
|
216
|
+
__name(createInternalError, "createInternalError");
|
|
174
217
|
|
|
175
218
|
// src/client/query-utils.ts
|
|
176
219
|
function hasModel(schema, model) {
|
|
@@ -188,7 +231,7 @@ __name(getTypeDef, "getTypeDef");
|
|
|
188
231
|
function requireModel(schema, model) {
|
|
189
232
|
const modelDef = getModel(schema, model);
|
|
190
233
|
if (!modelDef) {
|
|
191
|
-
throw
|
|
234
|
+
throw createInternalError(`Model "${model}" not found in schema`, model);
|
|
192
235
|
}
|
|
193
236
|
return modelDef;
|
|
194
237
|
}
|
|
@@ -202,7 +245,7 @@ function requireField(schema, modelOrType, field) {
|
|
|
202
245
|
const modelDef = getModel(schema, modelOrType);
|
|
203
246
|
if (modelDef) {
|
|
204
247
|
if (!modelDef.fields[field]) {
|
|
205
|
-
throw
|
|
248
|
+
throw createInternalError(`Field "${field}" not found in model "${modelOrType}"`, modelOrType);
|
|
206
249
|
} else {
|
|
207
250
|
return modelDef.fields[field];
|
|
208
251
|
}
|
|
@@ -210,12 +253,12 @@ function requireField(schema, modelOrType, field) {
|
|
|
210
253
|
const typeDef = getTypeDef(schema, modelOrType);
|
|
211
254
|
if (typeDef) {
|
|
212
255
|
if (!typeDef.fields[field]) {
|
|
213
|
-
throw
|
|
256
|
+
throw createInternalError(`Field "${field}" not found in type "${modelOrType}"`, modelOrType);
|
|
214
257
|
} else {
|
|
215
258
|
return typeDef.fields[field];
|
|
216
259
|
}
|
|
217
260
|
}
|
|
218
|
-
throw
|
|
261
|
+
throw createInternalError(`Model or type "${modelOrType}" not found in schema`, modelOrType);
|
|
219
262
|
}
|
|
220
263
|
__name(requireField, "requireField");
|
|
221
264
|
function getIdFields(schema, model) {
|
|
@@ -227,7 +270,7 @@ function requireIdFields(schema, model) {
|
|
|
227
270
|
const modelDef = requireModel(schema, model);
|
|
228
271
|
const result = modelDef?.idFields;
|
|
229
272
|
if (!result) {
|
|
230
|
-
throw
|
|
273
|
+
throw createInternalError(`Model "${model}" does not have ID field(s)`, model);
|
|
231
274
|
}
|
|
232
275
|
return result;
|
|
233
276
|
}
|
|
@@ -235,11 +278,11 @@ __name(requireIdFields, "requireIdFields");
|
|
|
235
278
|
function getRelationForeignKeyFieldPairs(schema, model, relationField) {
|
|
236
279
|
const fieldDef = requireField(schema, model, relationField);
|
|
237
280
|
if (!fieldDef?.relation) {
|
|
238
|
-
throw
|
|
281
|
+
throw createInternalError(`Field "${relationField}" is not a relation`, model);
|
|
239
282
|
}
|
|
240
283
|
if (fieldDef.relation.fields) {
|
|
241
284
|
if (!fieldDef.relation.references) {
|
|
242
|
-
throw
|
|
285
|
+
throw createInternalError(`Relation references not defined for field "${relationField}"`, model);
|
|
243
286
|
}
|
|
244
287
|
return {
|
|
245
288
|
keyPairs: fieldDef.relation.fields.map((f, i) => ({
|
|
@@ -250,17 +293,17 @@ function getRelationForeignKeyFieldPairs(schema, model, relationField) {
|
|
|
250
293
|
};
|
|
251
294
|
} else {
|
|
252
295
|
if (!fieldDef.relation.opposite) {
|
|
253
|
-
throw
|
|
296
|
+
throw createInternalError(`Opposite relation not defined for field "${relationField}"`, model);
|
|
254
297
|
}
|
|
255
298
|
const oppositeField = requireField(schema, fieldDef.type, fieldDef.relation.opposite);
|
|
256
299
|
if (!oppositeField.relation) {
|
|
257
|
-
throw
|
|
300
|
+
throw createInternalError(`Field "${fieldDef.relation.opposite}" is not a relation`, model);
|
|
258
301
|
}
|
|
259
302
|
if (!oppositeField.relation.fields) {
|
|
260
|
-
throw
|
|
303
|
+
throw createInternalError(`Relation fields not defined for field "${relationField}"`, model);
|
|
261
304
|
}
|
|
262
305
|
if (!oppositeField.relation.references) {
|
|
263
|
-
throw
|
|
306
|
+
throw createInternalError(`Relation references not defined for field "${relationField}"`, model);
|
|
264
307
|
}
|
|
265
308
|
return {
|
|
266
309
|
keyPairs: oppositeField.relation.fields.map((f, i) => ({
|
|
@@ -297,7 +340,7 @@ function getUniqueFields(schema, model) {
|
|
|
297
340
|
const result = [];
|
|
298
341
|
for (const [key, value] of Object.entries(modelDef.uniqueFields)) {
|
|
299
342
|
if (typeof value !== "object") {
|
|
300
|
-
throw
|
|
343
|
+
throw createInternalError(`Invalid unique field definition for "${key}"`, model);
|
|
301
344
|
}
|
|
302
345
|
if (typeof value.type === "string") {
|
|
303
346
|
result.push({
|
|
@@ -320,7 +363,7 @@ __name(getUniqueFields, "getUniqueFields");
|
|
|
320
363
|
function getIdValues(schema, model, data) {
|
|
321
364
|
const idFields = getIdFields(schema, model);
|
|
322
365
|
if (!idFields) {
|
|
323
|
-
throw
|
|
366
|
+
throw createInternalError(`ID fields not defined for model "${model}"`, model);
|
|
324
367
|
}
|
|
325
368
|
return idFields.reduce((acc, field) => ({
|
|
326
369
|
...acc,
|
|
@@ -479,7 +522,7 @@ function getDiscriminatorField(schema, model) {
|
|
|
479
522
|
}
|
|
480
523
|
const discriminator = delegateAttr.args?.find((arg) => arg.name === "discriminator");
|
|
481
524
|
if (!discriminator || !schema_exports.ExpressionUtils.isField(discriminator.value)) {
|
|
482
|
-
throw
|
|
525
|
+
throw createInternalError(`Discriminator field not defined for model "${model}"`, model);
|
|
483
526
|
}
|
|
484
527
|
return discriminator.value.field;
|
|
485
528
|
}
|
|
@@ -641,7 +684,7 @@ var BaseCrudDialect = class {
|
|
|
641
684
|
if (this.supportsDistinctOn) {
|
|
642
685
|
result = result.distinctOn(distinct.map((f) => this.eb.ref(`${modelAlias}.${f}`)));
|
|
643
686
|
} else {
|
|
644
|
-
throw
|
|
687
|
+
throw createNotSupportedError(`"distinct" is not supported by "${this.schema.provider.type}" provider`);
|
|
645
688
|
}
|
|
646
689
|
}
|
|
647
690
|
if (args.cursor) {
|
|
@@ -848,7 +891,7 @@ var BaseCrudDialect = class {
|
|
|
848
891
|
break;
|
|
849
892
|
}
|
|
850
893
|
default: {
|
|
851
|
-
throw
|
|
894
|
+
throw createInvalidInputError(`Invalid array filter key: ${key}`);
|
|
852
895
|
}
|
|
853
896
|
}
|
|
854
897
|
}
|
|
@@ -862,9 +905,9 @@ var BaseCrudDialect = class {
|
|
|
862
905
|
return this.buildEnumFilter(fieldRef, fieldDef, payload);
|
|
863
906
|
}
|
|
864
907
|
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", () => {
|
|
865
|
-
throw
|
|
908
|
+
throw createNotSupportedError("JSON filters are not supported yet");
|
|
866
909
|
}).with("Unsupported", () => {
|
|
867
|
-
throw
|
|
910
|
+
throw createInvalidInputError(`Unsupported field cannot be used in filters`);
|
|
868
911
|
}).exhaustive();
|
|
869
912
|
}
|
|
870
913
|
buildLiteralFilter(lhs, type, rhs) {
|
|
@@ -909,7 +952,7 @@ var BaseCrudDialect = class {
|
|
|
909
952
|
return this.and(...innerResult.conditions);
|
|
910
953
|
}).otherwise(() => {
|
|
911
954
|
if (throwIfInvalid) {
|
|
912
|
-
throw
|
|
955
|
+
throw createInvalidInputError(`Invalid filter key: ${op}`);
|
|
913
956
|
} else {
|
|
914
957
|
return void 0;
|
|
915
958
|
}
|
|
@@ -938,7 +981,7 @@ var BaseCrudDialect = class {
|
|
|
938
981
|
continue;
|
|
939
982
|
}
|
|
940
983
|
const condition = (0, import_ts_pattern2.match)(key).with("contains", () => mode === "insensitive" ? this.eb(fieldRef, "ilike", import_kysely2.sql.val(`%${value}%`)) : this.eb(fieldRef, "like", import_kysely2.sql.val(`%${value}%`))).with("startsWith", () => mode === "insensitive" ? this.eb(fieldRef, "ilike", import_kysely2.sql.val(`${value}%`)) : this.eb(fieldRef, "like", import_kysely2.sql.val(`${value}%`))).with("endsWith", () => mode === "insensitive" ? this.eb(fieldRef, "ilike", import_kysely2.sql.val(`%${value}`)) : this.eb(fieldRef, "like", import_kysely2.sql.val(`%${value}`))).otherwise(() => {
|
|
941
|
-
throw
|
|
984
|
+
throw createInvalidInputError(`Invalid string filter key: ${key}`);
|
|
942
985
|
});
|
|
943
986
|
if (condition) {
|
|
944
987
|
conditions.push(condition);
|
|
@@ -1018,7 +1061,7 @@ var BaseCrudDialect = class {
|
|
|
1018
1061
|
(0, import_common_helpers2.invariant)(value && typeof value === "object", `invalid orderBy value for field "${field}"`);
|
|
1019
1062
|
for (const [k, v] of Object.entries(value)) {
|
|
1020
1063
|
(0, import_common_helpers2.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1021
|
-
result = result.orderBy((eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field),
|
|
1064
|
+
result = result.orderBy((eb) => aggregate(eb, buildFieldRef(model, k, modelAlias), field), this.negateSort(v, negated));
|
|
1022
1065
|
}
|
|
1023
1066
|
continue;
|
|
1024
1067
|
}
|
|
@@ -1027,7 +1070,7 @@ var BaseCrudDialect = class {
|
|
|
1027
1070
|
(0, import_common_helpers2.invariant)(value && typeof value === "object", 'invalid orderBy value for field "_count"');
|
|
1028
1071
|
for (const [k, v] of Object.entries(value)) {
|
|
1029
1072
|
(0, import_common_helpers2.invariant)(v === "asc" || v === "desc", `invalid orderBy value for field "${field}"`);
|
|
1030
|
-
result = result.orderBy((eb) => eb.fn.count(buildFieldRef(model, k, modelAlias)),
|
|
1073
|
+
result = result.orderBy((eb) => eb.fn.count(buildFieldRef(model, k, modelAlias)), this.negateSort(v, negated));
|
|
1031
1074
|
}
|
|
1032
1075
|
continue;
|
|
1033
1076
|
}
|
|
@@ -1040,13 +1083,18 @@ var BaseCrudDialect = class {
|
|
|
1040
1083
|
if (value === "asc" || value === "desc") {
|
|
1041
1084
|
result = result.orderBy(fieldRef, this.negateSort(value, negated));
|
|
1042
1085
|
} else if (value && typeof value === "object" && "nulls" in value && "sort" in value && (value.sort === "asc" || value.sort === "desc") && (value.nulls === "first" || value.nulls === "last")) {
|
|
1043
|
-
result = result.orderBy(fieldRef,
|
|
1086
|
+
result = result.orderBy(fieldRef, (ob) => {
|
|
1087
|
+
const dir = this.negateSort(value.sort, negated);
|
|
1088
|
+
ob = dir === "asc" ? ob.asc() : ob.desc();
|
|
1089
|
+
ob = value.nulls === "first" ? ob.nullsFirst() : ob.nullsLast();
|
|
1090
|
+
return ob;
|
|
1091
|
+
});
|
|
1044
1092
|
}
|
|
1045
1093
|
} else {
|
|
1046
1094
|
const relationModel = fieldDef.type;
|
|
1047
1095
|
if (fieldDef.array) {
|
|
1048
1096
|
if (typeof value !== "object") {
|
|
1049
|
-
throw
|
|
1097
|
+
throw createInvalidInputError(`invalid orderBy value for field "${field}"`);
|
|
1050
1098
|
}
|
|
1051
1099
|
if ("_count" in value) {
|
|
1052
1100
|
(0, import_common_helpers2.invariant)(value._count === "asc" || value._count === "desc", 'invalid orderBy value for field "_count"');
|
|
@@ -1217,7 +1265,7 @@ var BaseCrudDialect = class {
|
|
|
1217
1265
|
computer = computedFields?.[fieldDef.originModel ?? model]?.[field];
|
|
1218
1266
|
}
|
|
1219
1267
|
if (!computer) {
|
|
1220
|
-
throw
|
|
1268
|
+
throw createConfigError(`Computed field "${field}" implementation not provided for model "${model}"`);
|
|
1221
1269
|
}
|
|
1222
1270
|
return computer(this.eb, {
|
|
1223
1271
|
modelAlias
|
|
@@ -1448,7 +1496,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1448
1496
|
}
|
|
1449
1497
|
getFieldSqlType(fieldDef) {
|
|
1450
1498
|
if (fieldDef.relation) {
|
|
1451
|
-
throw
|
|
1499
|
+
throw createInternalError("Cannot get SQL type of a relation field");
|
|
1452
1500
|
}
|
|
1453
1501
|
let result;
|
|
1454
1502
|
if (this.schema.enums?.[fieldDef.type]) {
|
|
@@ -1538,7 +1586,9 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1538
1586
|
try {
|
|
1539
1587
|
return JSON.parse(value);
|
|
1540
1588
|
} catch (e) {
|
|
1541
|
-
throw
|
|
1589
|
+
throw createInternalError("Invalid JSON returned", void 0, {
|
|
1590
|
+
cause: e
|
|
1591
|
+
});
|
|
1542
1592
|
}
|
|
1543
1593
|
}
|
|
1544
1594
|
return value;
|
|
@@ -1681,10 +1731,10 @@ var SqliteCrudDialect = class extends BaseCrudDialect {
|
|
|
1681
1731
|
}
|
|
1682
1732
|
getFieldSqlType(fieldDef) {
|
|
1683
1733
|
if (fieldDef.relation) {
|
|
1684
|
-
throw
|
|
1734
|
+
throw createInternalError("Cannot get SQL type of a relation field");
|
|
1685
1735
|
}
|
|
1686
1736
|
if (fieldDef.array) {
|
|
1687
|
-
throw
|
|
1737
|
+
throw createInternalError("SQLite does not support scalar list type");
|
|
1688
1738
|
}
|
|
1689
1739
|
if (this.schema.enums?.[fieldDef.type]) {
|
|
1690
1740
|
return "text";
|
|
@@ -1772,16 +1822,12 @@ var BaseOperationHandler = class {
|
|
|
1772
1822
|
operation: "read"
|
|
1773
1823
|
}));
|
|
1774
1824
|
let result = [];
|
|
1775
|
-
const
|
|
1776
|
-
queryId: `zenstack-${(0, import_cuid2.createId)()}`
|
|
1777
|
-
};
|
|
1778
|
-
const compiled = kysely.getExecutor().compileQuery(query.toOperationNode(), queryId);
|
|
1825
|
+
const compiled = kysely.getExecutor().compileQuery(query.toOperationNode(), (0, import_kysely5.createQueryId)());
|
|
1779
1826
|
try {
|
|
1780
|
-
const r = await kysely.getExecutor().executeQuery(compiled
|
|
1827
|
+
const r = await kysely.getExecutor().executeQuery(compiled);
|
|
1781
1828
|
result = r.rows;
|
|
1782
1829
|
} catch (err) {
|
|
1783
|
-
|
|
1784
|
-
throw new QueryError(message, err);
|
|
1830
|
+
throw createDBQueryError("Failed to execute query", err, compiled.sql, compiled.parameters);
|
|
1785
1831
|
}
|
|
1786
1832
|
return result;
|
|
1787
1833
|
}
|
|
@@ -1807,7 +1853,7 @@ var BaseOperationHandler = class {
|
|
|
1807
1853
|
result = this.dialect.buildSelectField(result, model, parentAlias, field);
|
|
1808
1854
|
} else {
|
|
1809
1855
|
if (!fieldDef.array && !fieldDef.optional && payload.where) {
|
|
1810
|
-
throw
|
|
1856
|
+
throw createInternalError(`Field "${field}" does not support filtering`, model);
|
|
1811
1857
|
}
|
|
1812
1858
|
if (fieldDef.originModel) {
|
|
1813
1859
|
result = this.dialect.buildRelationSelection(result, fieldDef.originModel, field, fieldDef.originModel, payload);
|
|
@@ -1824,7 +1870,7 @@ var BaseOperationHandler = class {
|
|
|
1824
1870
|
async create(kysely, model, data, fromRelation, creatingForDelegate = false, returnFields) {
|
|
1825
1871
|
const modelDef = this.requireModel(model);
|
|
1826
1872
|
if (modelDef.isDelegate && !creatingForDelegate) {
|
|
1827
|
-
throw
|
|
1873
|
+
throw createNotSupportedError(`Model "${model}" is a delegate and cannot be created directly.`);
|
|
1828
1874
|
}
|
|
1829
1875
|
let createFields = {};
|
|
1830
1876
|
let updateParent = void 0;
|
|
@@ -1929,7 +1975,7 @@ var BaseOperationHandler = class {
|
|
|
1929
1975
|
}
|
|
1930
1976
|
});
|
|
1931
1977
|
if (!extraRead) {
|
|
1932
|
-
throw
|
|
1978
|
+
throw createInternalError(`Field "${pair.pk}" not found in parent created data`, model);
|
|
1933
1979
|
} else {
|
|
1934
1980
|
Object.assign(entity, extraRead);
|
|
1935
1981
|
}
|
|
@@ -2013,7 +2059,7 @@ var BaseOperationHandler = class {
|
|
|
2013
2059
|
select: fieldsToSelectObject(referencedPkFields)
|
|
2014
2060
|
});
|
|
2015
2061
|
if (!relationEntity) {
|
|
2016
|
-
throw
|
|
2062
|
+
throw createNotFoundError(relationModel, `Could not find the entity to connect for the relation "${relationField.name}"`);
|
|
2017
2063
|
}
|
|
2018
2064
|
result = relationEntity;
|
|
2019
2065
|
}
|
|
@@ -2030,7 +2076,7 @@ var BaseOperationHandler = class {
|
|
|
2030
2076
|
break;
|
|
2031
2077
|
}
|
|
2032
2078
|
default:
|
|
2033
|
-
throw
|
|
2079
|
+
throw createInvalidInputError(`Invalid relation action: ${action}`);
|
|
2034
2080
|
}
|
|
2035
2081
|
}
|
|
2036
2082
|
return result;
|
|
@@ -2076,7 +2122,7 @@ var BaseOperationHandler = class {
|
|
|
2076
2122
|
break;
|
|
2077
2123
|
}
|
|
2078
2124
|
default:
|
|
2079
|
-
throw
|
|
2125
|
+
throw createInvalidInputError(`Invalid relation action: ${action}`);
|
|
2080
2126
|
}
|
|
2081
2127
|
}
|
|
2082
2128
|
}
|
|
@@ -2091,7 +2137,7 @@ var BaseOperationHandler = class {
|
|
|
2091
2137
|
if (fromRelation) {
|
|
2092
2138
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
2093
2139
|
if (ownedByModel) {
|
|
2094
|
-
throw
|
|
2140
|
+
throw createInvalidInputError("incorrect relation hierarchy for createMany", model);
|
|
2095
2141
|
}
|
|
2096
2142
|
relationKeyPairs = keyPairs;
|
|
2097
2143
|
}
|
|
@@ -2134,7 +2180,7 @@ var BaseOperationHandler = class {
|
|
|
2134
2180
|
}
|
|
2135
2181
|
if (modelDef.baseModel) {
|
|
2136
2182
|
if (input.skipDuplicates) {
|
|
2137
|
-
throw
|
|
2183
|
+
throw createNotSupportedError('"skipDuplicates" options is not supported for polymorphic models');
|
|
2138
2184
|
}
|
|
2139
2185
|
const baseCreateResult = await this.processBaseModelCreateMany(kysely, modelDef.baseModel, createData, !!input.skipDuplicates, model);
|
|
2140
2186
|
createData = baseCreateResult.remainingFieldRows;
|
|
@@ -2232,7 +2278,7 @@ var BaseOperationHandler = class {
|
|
|
2232
2278
|
}
|
|
2233
2279
|
async update(kysely, model, where, data, fromRelation, allowRelationUpdate = true, throwIfNotFound = true, fieldsToReturn) {
|
|
2234
2280
|
if (!data || typeof data !== "object") {
|
|
2235
|
-
throw
|
|
2281
|
+
throw createInvalidInputError("data must be an object");
|
|
2236
2282
|
}
|
|
2237
2283
|
const parentWhere = {};
|
|
2238
2284
|
let m2m = void 0;
|
|
@@ -2294,7 +2340,7 @@ var BaseOperationHandler = class {
|
|
|
2294
2340
|
select: this.makeIdSelect(model)
|
|
2295
2341
|
});
|
|
2296
2342
|
if (!readResult && throwIfNotFound) {
|
|
2297
|
-
throw
|
|
2343
|
+
throw createNotFoundError(model);
|
|
2298
2344
|
}
|
|
2299
2345
|
combinedWhere = readResult;
|
|
2300
2346
|
}
|
|
@@ -2311,13 +2357,13 @@ var BaseOperationHandler = class {
|
|
|
2311
2357
|
updateFields[field] = this.processScalarFieldUpdateData(model, field, finalData);
|
|
2312
2358
|
} else {
|
|
2313
2359
|
if (!allowRelationUpdate) {
|
|
2314
|
-
throw
|
|
2360
|
+
throw createNotSupportedError(`Relation update not allowed for field "${field}"`);
|
|
2315
2361
|
}
|
|
2316
2362
|
if (!thisEntity) {
|
|
2317
2363
|
thisEntity = await this.getEntityIds(kysely, model, combinedWhere);
|
|
2318
2364
|
if (!thisEntity) {
|
|
2319
2365
|
if (throwIfNotFound) {
|
|
2320
|
-
throw
|
|
2366
|
+
throw createNotFoundError(model);
|
|
2321
2367
|
} else {
|
|
2322
2368
|
return null;
|
|
2323
2369
|
}
|
|
@@ -2344,7 +2390,7 @@ var BaseOperationHandler = class {
|
|
|
2344
2390
|
const updatedEntity = await this.executeQueryTakeFirst(kysely, query, "update");
|
|
2345
2391
|
if (!updatedEntity) {
|
|
2346
2392
|
if (throwIfNotFound) {
|
|
2347
|
-
throw
|
|
2393
|
+
throw createNotFoundError(model);
|
|
2348
2394
|
} else {
|
|
2349
2395
|
return null;
|
|
2350
2396
|
}
|
|
@@ -2408,7 +2454,7 @@ var BaseOperationHandler = class {
|
|
|
2408
2454
|
const eb = (0, import_kysely5.expressionBuilder)();
|
|
2409
2455
|
const fieldRef = this.dialect.fieldRef(model, field);
|
|
2410
2456
|
return (0, import_ts_pattern6.match)(key).with("set", () => value).with("increment", () => eb(fieldRef, "+", value)).with("decrement", () => eb(fieldRef, "-", value)).with("multiply", () => eb(fieldRef, "*", value)).with("divide", () => eb(fieldRef, "/", value)).otherwise(() => {
|
|
2411
|
-
throw
|
|
2457
|
+
throw createInvalidInputError(`Invalid incremental update operation: ${key}`);
|
|
2412
2458
|
});
|
|
2413
2459
|
}
|
|
2414
2460
|
transformScalarListUpdate(model, field, fieldDef, payload) {
|
|
@@ -2420,7 +2466,7 @@ var BaseOperationHandler = class {
|
|
|
2420
2466
|
return (0, import_ts_pattern6.match)(key).with("set", () => value).with("push", () => {
|
|
2421
2467
|
return eb(fieldRef, "||", eb.val(ensureArray(value)));
|
|
2422
2468
|
}).otherwise(() => {
|
|
2423
|
-
throw
|
|
2469
|
+
throw createInvalidInputError(`Invalid array update operation: ${key}`);
|
|
2424
2470
|
});
|
|
2425
2471
|
}
|
|
2426
2472
|
isNumericField(fieldDef) {
|
|
@@ -2431,7 +2477,7 @@ var BaseOperationHandler = class {
|
|
|
2431
2477
|
}
|
|
2432
2478
|
async updateMany(kysely, model, where, data, limit, returnData, filterModel, fieldsToReturn) {
|
|
2433
2479
|
if (typeof data !== "object") {
|
|
2434
|
-
throw
|
|
2480
|
+
throw createInvalidInputError("data must be an object");
|
|
2435
2481
|
}
|
|
2436
2482
|
if (Object.keys(data).length === 0) {
|
|
2437
2483
|
return returnData ? [] : {
|
|
@@ -2440,7 +2486,7 @@ var BaseOperationHandler = class {
|
|
|
2440
2486
|
}
|
|
2441
2487
|
const modelDef = this.requireModel(model);
|
|
2442
2488
|
if (modelDef.baseModel && limit !== void 0) {
|
|
2443
|
-
throw
|
|
2489
|
+
throw createNotSupportedError("Updating with a limit is not supported for polymorphic models");
|
|
2444
2490
|
}
|
|
2445
2491
|
filterModel ??= model;
|
|
2446
2492
|
let updateFields = {};
|
|
@@ -2594,7 +2640,7 @@ var BaseOperationHandler = class {
|
|
|
2594
2640
|
break;
|
|
2595
2641
|
}
|
|
2596
2642
|
default: {
|
|
2597
|
-
throw
|
|
2643
|
+
throw createInvalidInputError(`Invalid relation update operation: ${key}`);
|
|
2598
2644
|
}
|
|
2599
2645
|
}
|
|
2600
2646
|
}
|
|
@@ -2612,13 +2658,13 @@ var BaseOperationHandler = class {
|
|
|
2612
2658
|
for (const d of _data) {
|
|
2613
2659
|
const ids = await this.getEntityIds(kysely, model, d);
|
|
2614
2660
|
if (!ids) {
|
|
2615
|
-
throw
|
|
2661
|
+
throw createNotFoundError(model);
|
|
2616
2662
|
}
|
|
2617
2663
|
const r = await this.handleManyToManyRelation(kysely, "connect", fromRelation.model, fromRelation.field, fromRelation.ids, m2m.otherModel, m2m.otherField, ids, m2m.joinTable);
|
|
2618
2664
|
results.push(r);
|
|
2619
2665
|
}
|
|
2620
2666
|
if (_data.length > results.filter((r) => !!r).length) {
|
|
2621
|
-
throw
|
|
2667
|
+
throw createNotFoundError(model);
|
|
2622
2668
|
}
|
|
2623
2669
|
} else {
|
|
2624
2670
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
@@ -2628,7 +2674,7 @@ var BaseOperationHandler = class {
|
|
|
2628
2674
|
where: _data[0]
|
|
2629
2675
|
});
|
|
2630
2676
|
if (!target) {
|
|
2631
|
-
throw
|
|
2677
|
+
throw createNotFoundError(model);
|
|
2632
2678
|
}
|
|
2633
2679
|
for (const { fk, pk } of keyPairs) {
|
|
2634
2680
|
fromRelation.parentUpdates[fk] = target[pk];
|
|
@@ -2654,7 +2700,7 @@ var BaseOperationHandler = class {
|
|
|
2654
2700
|
}));
|
|
2655
2701
|
const updateResult = await this.executeQuery(kysely, query, "connect");
|
|
2656
2702
|
if (!updateResult.numAffectedRows || _data.length > updateResult.numAffectedRows) {
|
|
2657
|
-
throw
|
|
2703
|
+
throw createNotFoundError(model);
|
|
2658
2704
|
}
|
|
2659
2705
|
}
|
|
2660
2706
|
}
|
|
@@ -2770,17 +2816,17 @@ var BaseOperationHandler = class {
|
|
|
2770
2816
|
for (const d of _data) {
|
|
2771
2817
|
const ids = await this.getEntityIds(kysely, model, d);
|
|
2772
2818
|
if (!ids) {
|
|
2773
|
-
throw
|
|
2819
|
+
throw createNotFoundError(model);
|
|
2774
2820
|
}
|
|
2775
2821
|
results.push(await this.handleManyToManyRelation(kysely, "connect", fromRelation.model, fromRelation.field, fromRelation.ids, m2m.otherModel, m2m.otherField, ids, m2m.joinTable));
|
|
2776
2822
|
}
|
|
2777
2823
|
if (_data.length > results.filter((r) => !!r).length) {
|
|
2778
|
-
throw
|
|
2824
|
+
throw createNotFoundError(model);
|
|
2779
2825
|
}
|
|
2780
2826
|
} else {
|
|
2781
2827
|
const { ownedByModel, keyPairs } = getRelationForeignKeyFieldPairs(this.schema, fromRelation.model, fromRelation.field);
|
|
2782
2828
|
if (ownedByModel) {
|
|
2783
|
-
throw
|
|
2829
|
+
throw createInternalError("relation can only be set from the non-owning side", fromRelation.model);
|
|
2784
2830
|
}
|
|
2785
2831
|
const fkConditions = keyPairs.reduce((acc, { fk, pk }) => ({
|
|
2786
2832
|
...acc,
|
|
@@ -2809,7 +2855,7 @@ var BaseOperationHandler = class {
|
|
|
2809
2855
|
}));
|
|
2810
2856
|
const r = await this.executeQuery(kysely, query2, "connect");
|
|
2811
2857
|
if (!r.numAffectedRows || _data.length > r.numAffectedRows) {
|
|
2812
|
-
throw
|
|
2858
|
+
throw createNotFoundError(model);
|
|
2813
2859
|
}
|
|
2814
2860
|
}
|
|
2815
2861
|
}
|
|
@@ -2860,7 +2906,7 @@ var BaseOperationHandler = class {
|
|
|
2860
2906
|
where: fromRelation.ids
|
|
2861
2907
|
});
|
|
2862
2908
|
if (!fromEntity) {
|
|
2863
|
-
throw
|
|
2909
|
+
throw createNotFoundError(fromRelation.model);
|
|
2864
2910
|
}
|
|
2865
2911
|
const fieldDef = this.requireField(fromRelation.model, fromRelation.field);
|
|
2866
2912
|
(0, import_common_helpers5.invariant)(fieldDef.relation?.opposite);
|
|
@@ -2892,7 +2938,7 @@ var BaseOperationHandler = class {
|
|
|
2892
2938
|
}
|
|
2893
2939
|
}
|
|
2894
2940
|
if (throwForNotFound && expectedDeleteCount > deleteResult.rows.length) {
|
|
2895
|
-
throw
|
|
2941
|
+
throw createNotFoundError(deleteFromModel);
|
|
2896
2942
|
}
|
|
2897
2943
|
}
|
|
2898
2944
|
normalizeRelationManipulationInput(model, data) {
|
|
@@ -2904,7 +2950,7 @@ var BaseOperationHandler = class {
|
|
|
2904
2950
|
const modelDef = this.requireModel(model);
|
|
2905
2951
|
if (modelDef.baseModel) {
|
|
2906
2952
|
if (limit !== void 0) {
|
|
2907
|
-
throw
|
|
2953
|
+
throw createNotSupportedError("Deleting with a limit is not supported for polymorphic models");
|
|
2908
2954
|
}
|
|
2909
2955
|
return this.processBaseModelDelete(kysely, modelDef.baseModel, where, limit, filterModel);
|
|
2910
2956
|
}
|
|
@@ -2938,7 +2984,7 @@ var BaseOperationHandler = class {
|
|
|
2938
2984
|
const oppositeRelation = this.requireField(fieldDef.type, fieldDef.relation.opposite);
|
|
2939
2985
|
if (oppositeModelDef.baseModel && oppositeRelation.relation?.onDelete === "Cascade") {
|
|
2940
2986
|
if (limit !== void 0) {
|
|
2941
|
-
throw
|
|
2987
|
+
throw createNotSupportedError("Deleting with a limit is not supported for polymorphic models");
|
|
2942
2988
|
}
|
|
2943
2989
|
await this.delete(kysely, fieldDef.type, {
|
|
2944
2990
|
[fieldDef.relation.opposite]: where
|
|
@@ -3016,22 +3062,17 @@ var BaseOperationHandler = class {
|
|
|
3016
3062
|
}
|
|
3017
3063
|
}
|
|
3018
3064
|
}
|
|
3019
|
-
|
|
3020
|
-
return
|
|
3021
|
-
queryId: `${operation}-${(0, import_cuid2.createId)()}`
|
|
3022
|
-
};
|
|
3023
|
-
}
|
|
3024
|
-
executeQuery(kysely, query, operation) {
|
|
3025
|
-
return kysely.executeQuery(query.compile(), this.makeQueryId(operation));
|
|
3065
|
+
executeQuery(kysely, query, _operation) {
|
|
3066
|
+
return kysely.executeQuery(query.compile());
|
|
3026
3067
|
}
|
|
3027
|
-
async executeQueryTakeFirst(kysely, query,
|
|
3028
|
-
const result = await kysely.executeQuery(query.compile()
|
|
3068
|
+
async executeQueryTakeFirst(kysely, query, _operation) {
|
|
3069
|
+
const result = await kysely.executeQuery(query.compile());
|
|
3029
3070
|
return result.rows[0];
|
|
3030
3071
|
}
|
|
3031
|
-
async executeQueryTakeFirstOrThrow(kysely, query,
|
|
3032
|
-
const result = await kysely.executeQuery(query.compile()
|
|
3072
|
+
async executeQueryTakeFirstOrThrow(kysely, query, _operation) {
|
|
3073
|
+
const result = await kysely.executeQuery(query.compile());
|
|
3033
3074
|
if (result.rows.length === 0) {
|
|
3034
|
-
throw new
|
|
3075
|
+
throw new ORMError(ORMErrorReason.NOT_FOUND, "No rows found");
|
|
3035
3076
|
}
|
|
3036
3077
|
return result.rows[0];
|
|
3037
3078
|
}
|
|
@@ -3252,7 +3293,7 @@ var CreateOperationHandler = class extends BaseOperationHandler {
|
|
|
3252
3293
|
}
|
|
3253
3294
|
});
|
|
3254
3295
|
if (!result && this.hasPolicyEnabled) {
|
|
3255
|
-
throw
|
|
3296
|
+
throw createRejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, `result is not allowed to be read back`);
|
|
3256
3297
|
}
|
|
3257
3298
|
return result;
|
|
3258
3299
|
}
|
|
@@ -3310,12 +3351,12 @@ var DeleteOperationHandler = class extends BaseOperationHandler {
|
|
|
3310
3351
|
}
|
|
3311
3352
|
const deleteResult = await this.delete(tx, this.model, args.where, void 0, void 0, selectedFields);
|
|
3312
3353
|
if (deleteResult.rows.length === 0) {
|
|
3313
|
-
throw
|
|
3354
|
+
throw createNotFoundError(this.model);
|
|
3314
3355
|
}
|
|
3315
3356
|
return needReadBack ? preDeleteRead : deleteResult.rows[0];
|
|
3316
3357
|
});
|
|
3317
3358
|
if (!result && this.hasPolicyEnabled) {
|
|
3318
|
-
throw
|
|
3359
|
+
throw createRejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3319
3360
|
}
|
|
3320
3361
|
return result;
|
|
3321
3362
|
}
|
|
@@ -3484,7 +3525,7 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3484
3525
|
});
|
|
3485
3526
|
if (!result) {
|
|
3486
3527
|
if (this.hasPolicyEnabled) {
|
|
3487
|
-
throw
|
|
3528
|
+
throw createRejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3488
3529
|
} else {
|
|
3489
3530
|
return null;
|
|
3490
3531
|
}
|
|
@@ -3524,7 +3565,7 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3524
3565
|
}
|
|
3525
3566
|
});
|
|
3526
3567
|
if (readBackResult.length < updateResult.length && this.hasPolicyEnabled) {
|
|
3527
|
-
throw
|
|
3568
|
+
throw createRejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3528
3569
|
}
|
|
3529
3570
|
return readBackResult;
|
|
3530
3571
|
}
|
|
@@ -3547,7 +3588,7 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3547
3588
|
}
|
|
3548
3589
|
});
|
|
3549
3590
|
if (!result && this.hasPolicyEnabled) {
|
|
3550
|
-
throw
|
|
3591
|
+
throw createRejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3551
3592
|
}
|
|
3552
3593
|
return result;
|
|
3553
3594
|
}
|
|
@@ -3944,7 +3985,7 @@ function evalCall(data, expr) {
|
|
|
3944
3985
|
(0, import_common_helpers6.invariant)(Array.isArray(fieldArg), `"${f}" first argument must be an array field`);
|
|
3945
3986
|
return fieldArg.length === 0;
|
|
3946
3987
|
}).otherwise(() => {
|
|
3947
|
-
throw
|
|
3988
|
+
throw createNotSupportedError(`Unsupported function "${expr.function}"`);
|
|
3948
3989
|
});
|
|
3949
3990
|
}
|
|
3950
3991
|
__name(evalCall, "evalCall");
|
|
@@ -4035,7 +4076,9 @@ var InputValidator = class {
|
|
|
4035
4076
|
}
|
|
4036
4077
|
const { error, data } = schema.safeParse(args);
|
|
4037
4078
|
if (error) {
|
|
4038
|
-
throw
|
|
4079
|
+
throw createInvalidInputError(`Invalid ${operation} args for model "${model}": ${formatError(error)}`, model, {
|
|
4080
|
+
cause: error
|
|
4081
|
+
});
|
|
4039
4082
|
}
|
|
4040
4083
|
return data;
|
|
4041
4084
|
}
|
|
@@ -4073,6 +4116,8 @@ var InputValidator = class {
|
|
|
4073
4116
|
makePrimitiveSchema(type, attributes) {
|
|
4074
4117
|
if (this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
4075
4118
|
return this.makeTypeDefSchema(type);
|
|
4119
|
+
} else if (this.schema.enums && type in this.schema.enums) {
|
|
4120
|
+
return this.makeEnumSchema(type);
|
|
4076
4121
|
} else {
|
|
4077
4122
|
return (0, import_ts_pattern13.match)(type).with("String", () => this.extraValidationsEnabled ? addStringValidation(import_zod2.z.string(), attributes) : import_zod2.z.string()).with("Int", () => this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number().int(), attributes) : import_zod2.z.number().int()).with("Float", () => this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number(), attributes) : import_zod2.z.number()).with("Boolean", () => import_zod2.z.boolean()).with("BigInt", () => import_zod2.z.union([
|
|
4078
4123
|
this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number().int(), attributes) : import_zod2.z.number().int(),
|
|
@@ -4089,6 +4134,21 @@ var InputValidator = class {
|
|
|
4089
4134
|
])).with("Bytes", () => import_zod2.z.instanceof(Uint8Array)).otherwise(() => import_zod2.z.unknown());
|
|
4090
4135
|
}
|
|
4091
4136
|
}
|
|
4137
|
+
makeEnumSchema(type) {
|
|
4138
|
+
const key = (0, import_json_stable_stringify.default)({
|
|
4139
|
+
type: "enum",
|
|
4140
|
+
name: type
|
|
4141
|
+
});
|
|
4142
|
+
let schema = this.getSchemaCache(key);
|
|
4143
|
+
if (schema) {
|
|
4144
|
+
return schema;
|
|
4145
|
+
}
|
|
4146
|
+
const enumDef = getEnum(this.schema, type);
|
|
4147
|
+
(0, import_common_helpers7.invariant)(enumDef, `Enum "${type}" not found in schema`);
|
|
4148
|
+
schema = import_zod2.z.enum(Object.keys(enumDef.values));
|
|
4149
|
+
this.setSchemaCache(key, schema);
|
|
4150
|
+
return schema;
|
|
4151
|
+
}
|
|
4092
4152
|
makeTypeDefSchema(type) {
|
|
4093
4153
|
const key = (0, import_json_stable_stringify.default)({
|
|
4094
4154
|
type: "typedef",
|
|
@@ -4099,9 +4159,9 @@ var InputValidator = class {
|
|
|
4099
4159
|
if (schema) {
|
|
4100
4160
|
return schema;
|
|
4101
4161
|
}
|
|
4102
|
-
const typeDef = this.schema
|
|
4162
|
+
const typeDef = getTypeDef(this.schema, type);
|
|
4103
4163
|
(0, import_common_helpers7.invariant)(typeDef, `Type definition "${type}" not found in schema`);
|
|
4104
|
-
schema = import_zod2.z.
|
|
4164
|
+
schema = import_zod2.z.looseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
4105
4165
|
let fieldSchema = this.makePrimitiveSchema(def.type);
|
|
4106
4166
|
if (def.array) {
|
|
4107
4167
|
fieldSchema = fieldSchema.array();
|
|
@@ -4113,7 +4173,7 @@ var InputValidator = class {
|
|
|
4113
4173
|
field,
|
|
4114
4174
|
fieldSchema
|
|
4115
4175
|
];
|
|
4116
|
-
})))
|
|
4176
|
+
})));
|
|
4117
4177
|
this.setSchemaCache(key, schema);
|
|
4118
4178
|
return schema;
|
|
4119
4179
|
}
|
|
@@ -4150,7 +4210,7 @@ var InputValidator = class {
|
|
|
4150
4210
|
} else {
|
|
4151
4211
|
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
4152
4212
|
if (enumDef) {
|
|
4153
|
-
if (Object.keys(enumDef).length > 0) {
|
|
4213
|
+
if (Object.keys(enumDef.values).length > 0) {
|
|
4154
4214
|
fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations);
|
|
4155
4215
|
}
|
|
4156
4216
|
} else if (fieldDef.array) {
|
|
@@ -4172,7 +4232,7 @@ var InputValidator = class {
|
|
|
4172
4232
|
let fieldSchema;
|
|
4173
4233
|
const enumDef = getEnum(this.schema, def.type);
|
|
4174
4234
|
if (enumDef) {
|
|
4175
|
-
if (Object.keys(enumDef).length > 0) {
|
|
4235
|
+
if (Object.keys(enumDef.values).length > 0) {
|
|
4176
4236
|
fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false);
|
|
4177
4237
|
} else {
|
|
4178
4238
|
fieldSchema = import_zod2.z.never();
|
|
@@ -4197,7 +4257,7 @@ var InputValidator = class {
|
|
|
4197
4257
|
if (unique) {
|
|
4198
4258
|
const uniqueFields = getUniqueFields(this.schema, model);
|
|
4199
4259
|
if (uniqueFields.length === 0) {
|
|
4200
|
-
throw
|
|
4260
|
+
throw createInternalError(`Model "${model}" has no unique fields`);
|
|
4201
4261
|
}
|
|
4202
4262
|
if (uniqueFields.length === 1) {
|
|
4203
4263
|
result = baseWhere.required({
|
|
@@ -4212,7 +4272,7 @@ var InputValidator = class {
|
|
|
4212
4272
|
return result;
|
|
4213
4273
|
}
|
|
4214
4274
|
makeEnumFilterSchema(enumDef, optional, withAggregations) {
|
|
4215
|
-
const baseSchema = import_zod2.z.enum(Object.keys(enumDef));
|
|
4275
|
+
const baseSchema = import_zod2.z.enum(Object.keys(enumDef.values));
|
|
4216
4276
|
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => import_zod2.z.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations)), [
|
|
4217
4277
|
"equals",
|
|
4218
4278
|
"in",
|
|
@@ -5260,13 +5320,26 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5260
5320
|
if (!node.into) {
|
|
5261
5321
|
return super.transformInsertQuery(node);
|
|
5262
5322
|
}
|
|
5323
|
+
const model = extractModelName(node.into);
|
|
5324
|
+
(0, import_common_helpers8.invariant)(model, 'InsertQueryNode must have a model name in the "into" clause');
|
|
5263
5325
|
return this.withScope({
|
|
5264
|
-
model
|
|
5265
|
-
}, () =>
|
|
5266
|
-
|
|
5267
|
-
|
|
5268
|
-
|
|
5269
|
-
|
|
5326
|
+
model
|
|
5327
|
+
}, () => {
|
|
5328
|
+
const baseResult = super.transformInsertQuery(node);
|
|
5329
|
+
let values = baseResult.values;
|
|
5330
|
+
if (node.columns && values) {
|
|
5331
|
+
values = this.processEnumMappingForColumns(model, node.columns, values);
|
|
5332
|
+
}
|
|
5333
|
+
return {
|
|
5334
|
+
...baseResult,
|
|
5335
|
+
// map table name
|
|
5336
|
+
into: this.processTableRef(node.into),
|
|
5337
|
+
values
|
|
5338
|
+
};
|
|
5339
|
+
});
|
|
5340
|
+
}
|
|
5341
|
+
isOperationNode(value) {
|
|
5342
|
+
return !!value && typeof value === "object" && "kind" in value;
|
|
5270
5343
|
}
|
|
5271
5344
|
transformReturning(node) {
|
|
5272
5345
|
return {
|
|
@@ -5289,7 +5362,7 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5289
5362
|
mappedTableName = this.mapTableName(scope.model);
|
|
5290
5363
|
}
|
|
5291
5364
|
}
|
|
5292
|
-
return import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(mappedFieldName), mappedTableName ?
|
|
5365
|
+
return import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(mappedFieldName), mappedTableName ? this.createTableNode(mappedTableName, void 0) : void 0);
|
|
5293
5366
|
} else {
|
|
5294
5367
|
return node;
|
|
5295
5368
|
}
|
|
@@ -5310,12 +5383,24 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5310
5383
|
if (!innerTable || !import_kysely6.TableNode.is(innerTable)) {
|
|
5311
5384
|
return super.transformUpdateQuery(node);
|
|
5312
5385
|
}
|
|
5386
|
+
const model = extractModelName(innerTable);
|
|
5387
|
+
(0, import_common_helpers8.invariant)(model, 'UpdateQueryNode must have a model name in the "table" clause');
|
|
5313
5388
|
return this.withScope({
|
|
5314
|
-
model
|
|
5389
|
+
model,
|
|
5315
5390
|
alias
|
|
5316
5391
|
}, () => {
|
|
5392
|
+
const baseResult = super.transformUpdateQuery(node);
|
|
5393
|
+
const updates = baseResult.updates?.map((update, i) => {
|
|
5394
|
+
if (import_kysely6.ColumnNode.is(update.column)) {
|
|
5395
|
+
const origColumn = node.updates[i].column;
|
|
5396
|
+
return import_kysely6.ColumnUpdateNode.create(update.column, this.processEnumMappingForValue(model, origColumn, update.value));
|
|
5397
|
+
} else {
|
|
5398
|
+
return update;
|
|
5399
|
+
}
|
|
5400
|
+
});
|
|
5317
5401
|
return {
|
|
5318
|
-
...
|
|
5402
|
+
...baseResult,
|
|
5403
|
+
updates,
|
|
5319
5404
|
// map table name
|
|
5320
5405
|
table: this.wrapAlias(this.processTableRef(innerTable), alias)
|
|
5321
5406
|
};
|
|
@@ -5350,29 +5435,54 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5350
5435
|
processSelectQuerySelections(node) {
|
|
5351
5436
|
const selections = [];
|
|
5352
5437
|
for (const selection of node.selections ?? []) {
|
|
5438
|
+
const processedSelections = [];
|
|
5353
5439
|
if (import_kysely6.SelectAllNode.is(selection.selection)) {
|
|
5354
|
-
const scope = this.
|
|
5440
|
+
const scope = this.requireCurrentScope();
|
|
5355
5441
|
if (scope?.model && !scope.namesMapped) {
|
|
5356
|
-
|
|
5442
|
+
processedSelections.push(...this.createSelectAllFields(scope.model, scope.alias));
|
|
5357
5443
|
} else {
|
|
5358
|
-
|
|
5444
|
+
processedSelections.push({
|
|
5445
|
+
originalField: void 0,
|
|
5446
|
+
selection: super.transformSelection(selection)
|
|
5447
|
+
});
|
|
5359
5448
|
}
|
|
5360
5449
|
} else if (import_kysely6.ReferenceNode.is(selection.selection) || import_kysely6.ColumnNode.is(selection.selection)) {
|
|
5361
5450
|
const transformed = this.transformNode(selection.selection);
|
|
5451
|
+
const originalField = extractFieldName(selection.selection);
|
|
5362
5452
|
if (import_kysely6.AliasNode.is(transformed)) {
|
|
5363
|
-
|
|
5453
|
+
processedSelections.push({
|
|
5454
|
+
originalField,
|
|
5455
|
+
selection: import_kysely6.SelectionNode.create(transformed)
|
|
5456
|
+
});
|
|
5364
5457
|
} else {
|
|
5365
|
-
const origFieldName = extractFieldName(selection.selection);
|
|
5366
5458
|
const fieldName = extractFieldName(transformed);
|
|
5367
|
-
if (fieldName !==
|
|
5368
|
-
|
|
5459
|
+
if (fieldName !== originalField) {
|
|
5460
|
+
processedSelections.push({
|
|
5461
|
+
originalField,
|
|
5462
|
+
selection: import_kysely6.SelectionNode.create(this.wrapAlias(transformed, originalField ? import_kysely6.IdentifierNode.create(originalField) : void 0))
|
|
5463
|
+
});
|
|
5369
5464
|
} else {
|
|
5370
|
-
|
|
5465
|
+
processedSelections.push({
|
|
5466
|
+
originalField,
|
|
5467
|
+
selection: import_kysely6.SelectionNode.create(transformed)
|
|
5468
|
+
});
|
|
5371
5469
|
}
|
|
5372
5470
|
}
|
|
5373
5471
|
} else {
|
|
5374
|
-
|
|
5472
|
+
const { node: innerNode } = stripAlias(selection.selection);
|
|
5473
|
+
processedSelections.push({
|
|
5474
|
+
originalField: extractFieldName(innerNode),
|
|
5475
|
+
selection: super.transformSelection(selection)
|
|
5476
|
+
});
|
|
5375
5477
|
}
|
|
5478
|
+
const enumProcessedSelections = processedSelections.map(({ originalField, selection: selection2 }) => {
|
|
5479
|
+
if (!originalField) {
|
|
5480
|
+
return selection2;
|
|
5481
|
+
} else {
|
|
5482
|
+
return import_kysely6.SelectionNode.create(this.processEnumSelection(selection2.selection, originalField));
|
|
5483
|
+
}
|
|
5484
|
+
});
|
|
5485
|
+
selections.push(...enumProcessedSelections);
|
|
5376
5486
|
}
|
|
5377
5487
|
return selections;
|
|
5378
5488
|
}
|
|
@@ -5436,7 +5546,9 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5436
5546
|
if (!import_kysely6.TableNode.is(node)) {
|
|
5437
5547
|
return super.transformNode(node);
|
|
5438
5548
|
}
|
|
5439
|
-
|
|
5549
|
+
const mappedName = this.mapTableName(node.table.identifier.name);
|
|
5550
|
+
const tableSchema = this.getTableSchema(node.table.identifier.name);
|
|
5551
|
+
return this.createTableNode(mappedName, tableSchema);
|
|
5440
5552
|
}
|
|
5441
5553
|
getMappedName(def) {
|
|
5442
5554
|
const mapAttr = def.attributes?.find((attr) => attr.name === "@@map" || attr.name === "@map");
|
|
@@ -5476,8 +5588,9 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5476
5588
|
const modelName = innerNode.table.identifier.name;
|
|
5477
5589
|
const mappedName = this.mapTableName(modelName);
|
|
5478
5590
|
const finalAlias = alias ?? (mappedName !== modelName ? import_kysely6.IdentifierNode.create(modelName) : void 0);
|
|
5591
|
+
const tableSchema = this.getTableSchema(modelName);
|
|
5479
5592
|
return {
|
|
5480
|
-
node: this.wrapAlias(
|
|
5593
|
+
node: this.wrapAlias(this.createTableNode(mappedName, tableSchema), finalAlias),
|
|
5481
5594
|
scope: {
|
|
5482
5595
|
alias: alias ?? import_kysely6.IdentifierNode.create(modelName),
|
|
5483
5596
|
model: modelName,
|
|
@@ -5495,6 +5608,20 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5495
5608
|
};
|
|
5496
5609
|
}
|
|
5497
5610
|
}
|
|
5611
|
+
getTableSchema(model) {
|
|
5612
|
+
if (this.schema.provider.type !== "postgresql") {
|
|
5613
|
+
return void 0;
|
|
5614
|
+
}
|
|
5615
|
+
let schema = this.schema.provider.defaultSchema ?? "public";
|
|
5616
|
+
const schemaAttr = this.schema.models[model]?.attributes?.find((attr) => attr.name === "@@schema");
|
|
5617
|
+
if (schemaAttr) {
|
|
5618
|
+
const nameArg = schemaAttr.args?.find((arg) => arg.name === "map");
|
|
5619
|
+
if (nameArg && nameArg.value.kind === "literal") {
|
|
5620
|
+
schema = nameArg.value.value;
|
|
5621
|
+
}
|
|
5622
|
+
}
|
|
5623
|
+
return schema;
|
|
5624
|
+
}
|
|
5498
5625
|
createSelectAllFields(model, alias) {
|
|
5499
5626
|
const modelDef = requireModel(this.schema, model);
|
|
5500
5627
|
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
@@ -5502,9 +5629,15 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5502
5629
|
const columnRef = import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(columnName), alias && import_kysely6.IdentifierNode.is(alias) ? import_kysely6.TableNode.create(alias.name) : void 0);
|
|
5503
5630
|
if (columnName !== fieldDef.name) {
|
|
5504
5631
|
const aliased = import_kysely6.AliasNode.create(columnRef, import_kysely6.IdentifierNode.create(fieldDef.name));
|
|
5505
|
-
return
|
|
5632
|
+
return {
|
|
5633
|
+
originalField: fieldDef.name,
|
|
5634
|
+
selection: import_kysely6.SelectionNode.create(aliased)
|
|
5635
|
+
};
|
|
5506
5636
|
} else {
|
|
5507
|
-
return
|
|
5637
|
+
return {
|
|
5638
|
+
originalField: fieldDef.name,
|
|
5639
|
+
selection: import_kysely6.SelectionNode.create(columnRef)
|
|
5640
|
+
};
|
|
5508
5641
|
}
|
|
5509
5642
|
});
|
|
5510
5643
|
}
|
|
@@ -5528,26 +5661,155 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
|
|
|
5528
5661
|
return result;
|
|
5529
5662
|
}
|
|
5530
5663
|
processSelection(node) {
|
|
5531
|
-
|
|
5532
|
-
|
|
5533
|
-
|
|
5664
|
+
const { alias, node: innerNode } = stripAlias(node);
|
|
5665
|
+
const originalField = extractFieldName(innerNode);
|
|
5666
|
+
let result = super.transformNode(node);
|
|
5667
|
+
if (originalField) {
|
|
5668
|
+
result = this.processEnumSelection(result, originalField);
|
|
5669
|
+
}
|
|
5670
|
+
if (!import_kysely6.AliasNode.is(result)) {
|
|
5671
|
+
const addAlias = alias ?? (originalField ? import_kysely6.IdentifierNode.create(originalField) : void 0);
|
|
5672
|
+
if (addAlias) {
|
|
5673
|
+
result = this.wrapAlias(result, addAlias);
|
|
5674
|
+
}
|
|
5534
5675
|
}
|
|
5535
|
-
|
|
5536
|
-
return this.wrapAlias(result, alias ? import_kysely6.IdentifierNode.create(alias) : void 0);
|
|
5676
|
+
return result;
|
|
5537
5677
|
}
|
|
5538
5678
|
processSelectAll(node) {
|
|
5539
|
-
const scope = this.
|
|
5540
|
-
(
|
|
5541
|
-
if (!scope.model || !this.hasMappedColumns(scope.model)) {
|
|
5679
|
+
const scope = this.requireCurrentScope();
|
|
5680
|
+
if (!scope.model || !(this.hasMappedColumns(scope.model) || this.modelUsesEnumWithMappedValues(scope.model))) {
|
|
5542
5681
|
return super.transformSelectAll(node);
|
|
5543
5682
|
}
|
|
5544
5683
|
const modelDef = requireModel(this.schema, scope.model);
|
|
5545
5684
|
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
5546
5685
|
const columnName = this.mapFieldName(modelDef.name, fieldDef.name);
|
|
5547
5686
|
const columnRef = import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(columnName));
|
|
5548
|
-
|
|
5687
|
+
const enumProcessed = this.processEnumSelection(columnRef, fieldDef.name);
|
|
5688
|
+
return columnName !== fieldDef.name && !import_kysely6.AliasNode.is(enumProcessed) ? this.wrapAlias(enumProcessed, import_kysely6.IdentifierNode.create(fieldDef.name)) : enumProcessed;
|
|
5689
|
+
});
|
|
5690
|
+
}
|
|
5691
|
+
createTableNode(tableName, schemaName) {
|
|
5692
|
+
return schemaName ? import_kysely6.TableNode.createWithSchema(schemaName, tableName) : import_kysely6.TableNode.create(tableName);
|
|
5693
|
+
}
|
|
5694
|
+
requireCurrentScope() {
|
|
5695
|
+
const scope = this.scopes[this.scopes.length - 1];
|
|
5696
|
+
(0, import_common_helpers8.invariant)(scope, "No scope available");
|
|
5697
|
+
return scope;
|
|
5698
|
+
}
|
|
5699
|
+
// #endregion
|
|
5700
|
+
// #region enum value mapping
|
|
5701
|
+
modelUsesEnumWithMappedValues(model) {
|
|
5702
|
+
const modelDef = getModel(this.schema, model);
|
|
5703
|
+
if (!modelDef) {
|
|
5704
|
+
return false;
|
|
5705
|
+
}
|
|
5706
|
+
return this.getModelFields(modelDef).some((fieldDef) => {
|
|
5707
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5708
|
+
if (!enumDef) {
|
|
5709
|
+
return false;
|
|
5710
|
+
}
|
|
5711
|
+
return Object.values(enumDef.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
|
|
5549
5712
|
});
|
|
5550
5713
|
}
|
|
5714
|
+
getEnumValueMapping(enumDef) {
|
|
5715
|
+
const mapping = {};
|
|
5716
|
+
for (const [key, field] of Object.entries(enumDef.fields ?? {})) {
|
|
5717
|
+
const mappedName = this.getMappedName(field);
|
|
5718
|
+
if (mappedName) {
|
|
5719
|
+
mapping[key] = mappedName;
|
|
5720
|
+
}
|
|
5721
|
+
}
|
|
5722
|
+
return mapping;
|
|
5723
|
+
}
|
|
5724
|
+
processEnumMappingForColumns(model, columns, values) {
|
|
5725
|
+
if (import_kysely6.ValuesNode.is(values)) {
|
|
5726
|
+
return import_kysely6.ValuesNode.create(values.values.map((valueItems) => {
|
|
5727
|
+
if (import_kysely6.PrimitiveValueListNode.is(valueItems)) {
|
|
5728
|
+
return import_kysely6.PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
|
|
5729
|
+
} else {
|
|
5730
|
+
return import_kysely6.ValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
|
|
5731
|
+
}
|
|
5732
|
+
}));
|
|
5733
|
+
} else if (import_kysely6.PrimitiveValueListNode.is(values)) {
|
|
5734
|
+
return import_kysely6.PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, values.values));
|
|
5735
|
+
} else {
|
|
5736
|
+
return values;
|
|
5737
|
+
}
|
|
5738
|
+
}
|
|
5739
|
+
processEnumMappingForValues(model, columns, values) {
|
|
5740
|
+
const result = [];
|
|
5741
|
+
for (let i = 0; i < columns.length; i++) {
|
|
5742
|
+
const value = values[i];
|
|
5743
|
+
if (value === null || value === void 0) {
|
|
5744
|
+
result.push(value);
|
|
5745
|
+
continue;
|
|
5746
|
+
}
|
|
5747
|
+
result.push(this.processEnumMappingForValue(model, columns[i], value));
|
|
5748
|
+
}
|
|
5749
|
+
return result;
|
|
5750
|
+
}
|
|
5751
|
+
processEnumMappingForValue(model, column, value) {
|
|
5752
|
+
const fieldDef = getField(this.schema, model, column.column.name);
|
|
5753
|
+
if (!fieldDef) {
|
|
5754
|
+
return value;
|
|
5755
|
+
}
|
|
5756
|
+
if (!isEnum(this.schema, fieldDef.type)) {
|
|
5757
|
+
return value;
|
|
5758
|
+
}
|
|
5759
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5760
|
+
if (!enumDef) {
|
|
5761
|
+
return value;
|
|
5762
|
+
}
|
|
5763
|
+
const enumValueMapping = this.getEnumValueMapping(enumDef);
|
|
5764
|
+
if (this.isOperationNode(value) && import_kysely6.ValueNode.is(value) && typeof value.value === "string") {
|
|
5765
|
+
const mappedValue = enumValueMapping[value.value];
|
|
5766
|
+
if (mappedValue) {
|
|
5767
|
+
return import_kysely6.ValueNode.create(mappedValue);
|
|
5768
|
+
}
|
|
5769
|
+
} else if (typeof value === "string") {
|
|
5770
|
+
const mappedValue = enumValueMapping[value];
|
|
5771
|
+
if (mappedValue) {
|
|
5772
|
+
return mappedValue;
|
|
5773
|
+
}
|
|
5774
|
+
}
|
|
5775
|
+
return value;
|
|
5776
|
+
}
|
|
5777
|
+
processEnumSelection(selection, fieldName) {
|
|
5778
|
+
const { alias, node } = stripAlias(selection);
|
|
5779
|
+
const fieldScope = this.resolveFieldFromScopes(fieldName);
|
|
5780
|
+
if (!fieldScope || !fieldScope.model) {
|
|
5781
|
+
return selection;
|
|
5782
|
+
}
|
|
5783
|
+
const aliasName = alias && import_kysely6.IdentifierNode.is(alias) ? alias.name : fieldName;
|
|
5784
|
+
const fieldDef = getField(this.schema, fieldScope.model, fieldName);
|
|
5785
|
+
if (!fieldDef) {
|
|
5786
|
+
return selection;
|
|
5787
|
+
}
|
|
5788
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5789
|
+
if (!enumDef) {
|
|
5790
|
+
return selection;
|
|
5791
|
+
}
|
|
5792
|
+
const enumValueMapping = this.getEnumValueMapping(enumDef);
|
|
5793
|
+
if (Object.keys(enumValueMapping).length === 0) {
|
|
5794
|
+
return selection;
|
|
5795
|
+
}
|
|
5796
|
+
const eb = (0, import_kysely6.expressionBuilder)();
|
|
5797
|
+
const caseBuilder = eb.case();
|
|
5798
|
+
let caseWhen;
|
|
5799
|
+
for (const [key, value] of Object.entries(enumValueMapping)) {
|
|
5800
|
+
if (!caseWhen) {
|
|
5801
|
+
caseWhen = caseBuilder.when(new import_kysely6.ExpressionWrapper(node), "=", value).then(key);
|
|
5802
|
+
} else {
|
|
5803
|
+
caseWhen = caseWhen.when(new import_kysely6.ExpressionWrapper(node), "=", value).then(key);
|
|
5804
|
+
}
|
|
5805
|
+
}
|
|
5806
|
+
const finalExpr = caseWhen.else(eb.cast(new import_kysely6.ExpressionWrapper(node), "text")).end();
|
|
5807
|
+
if (aliasName) {
|
|
5808
|
+
return finalExpr.as(aliasName).toOperationNode();
|
|
5809
|
+
} else {
|
|
5810
|
+
return finalExpr.toOperationNode();
|
|
5811
|
+
}
|
|
5812
|
+
}
|
|
5551
5813
|
};
|
|
5552
5814
|
|
|
5553
5815
|
// src/client/executor/zenstack-query-executor.ts
|
|
@@ -5563,7 +5825,8 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5563
5825
|
nameMapper;
|
|
5564
5826
|
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
5565
5827
|
super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
|
|
5566
|
-
if (
|
|
5828
|
+
if (client.$schema.provider.type === "postgresql" || // postgres queries need to be schema-qualified
|
|
5829
|
+
this.schemaHasMappedNames(client.$schema)) {
|
|
5567
5830
|
this.nameMapper = new QueryNameMapper(client.$schema);
|
|
5568
5831
|
}
|
|
5569
5832
|
}
|
|
@@ -5582,7 +5845,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5582
5845
|
get options() {
|
|
5583
5846
|
return this.client.$options;
|
|
5584
5847
|
}
|
|
5585
|
-
executeQuery(compiledQuery
|
|
5848
|
+
executeQuery(compiledQuery) {
|
|
5586
5849
|
const queryParams = compiledQuery.$raw ? compiledQuery.parameters : void 0;
|
|
5587
5850
|
return this.provideConnection(async (connection) => {
|
|
5588
5851
|
let startedTx = false;
|
|
@@ -5593,7 +5856,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5593
5856
|
});
|
|
5594
5857
|
startedTx = true;
|
|
5595
5858
|
}
|
|
5596
|
-
const result = await this.proceedQueryWithKyselyInterceptors(connection, compiledQuery.query, queryParams,
|
|
5859
|
+
const result = await this.proceedQueryWithKyselyInterceptors(connection, compiledQuery.query, queryParams, compiledQuery.queryId);
|
|
5597
5860
|
if (startedTx) {
|
|
5598
5861
|
await this.driver.commitTransaction(connection);
|
|
5599
5862
|
}
|
|
@@ -5602,11 +5865,10 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5602
5865
|
if (startedTx) {
|
|
5603
5866
|
await this.driver.rollbackTransaction(connection);
|
|
5604
5867
|
}
|
|
5605
|
-
if (err instanceof
|
|
5868
|
+
if (err instanceof ORMError) {
|
|
5606
5869
|
throw err;
|
|
5607
5870
|
} else {
|
|
5608
|
-
|
|
5609
|
-
throw new QueryError(message, err);
|
|
5871
|
+
throw createDBQueryError("Failed to execute query", err, compiledQuery.sql, compiledQuery.parameters);
|
|
5610
5872
|
}
|
|
5611
5873
|
}
|
|
5612
5874
|
});
|
|
@@ -5657,7 +5919,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5657
5919
|
let compiled;
|
|
5658
5920
|
if (this.suppressMutationHooks || !this.isMutationNode(query) || !this.hasEntityMutationPlugins) {
|
|
5659
5921
|
const finalQuery2 = this.processNameMapping(query);
|
|
5660
|
-
compiled = this.compileQuery(finalQuery2);
|
|
5922
|
+
compiled = this.compileQuery(finalQuery2, queryId);
|
|
5661
5923
|
if (parameters) {
|
|
5662
5924
|
compiled = {
|
|
5663
5925
|
...compiled,
|
|
@@ -5675,7 +5937,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5675
5937
|
};
|
|
5676
5938
|
}
|
|
5677
5939
|
const finalQuery = this.processNameMapping(query);
|
|
5678
|
-
compiled = this.compileQuery(finalQuery);
|
|
5940
|
+
compiled = this.compileQuery(finalQuery, queryId);
|
|
5679
5941
|
if (parameters) {
|
|
5680
5942
|
compiled = {
|
|
5681
5943
|
...compiled,
|
|
@@ -5764,7 +6026,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5764
6026
|
(0, import_common_helpers9.invariant)(import_kysely7.TableNode.is(tableNode), "DeleteQueryNode must use a TableNode");
|
|
5765
6027
|
return tableNode.table.identifier.name;
|
|
5766
6028
|
}).otherwise((node) => {
|
|
5767
|
-
throw
|
|
6029
|
+
throw createInternalError(`Invalid query node: ${node}`);
|
|
5768
6030
|
});
|
|
5769
6031
|
}
|
|
5770
6032
|
async callBeforeMutationHooks(queryNode, mutationInfo, loadBeforeMutationEntities, client, queryId) {
|
|
@@ -5829,7 +6091,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5829
6091
|
...selectQueryNode,
|
|
5830
6092
|
where: this.andNodes(selectQueryNode.where, where)
|
|
5831
6093
|
};
|
|
5832
|
-
const compiled = this.compileQuery(selectQueryNode);
|
|
6094
|
+
const compiled = this.compileQuery(selectQueryNode, (0, import_kysely7.createQueryId)());
|
|
5833
6095
|
const result = await connection.executeQuery(compiled);
|
|
5834
6096
|
return result.rows;
|
|
5835
6097
|
}
|
|
@@ -6005,7 +6267,22 @@ var SchemaDbPusher = class {
|
|
|
6005
6267
|
await this.kysely.transaction().execute(async (tx) => {
|
|
6006
6268
|
if (this.schema.enums && this.schema.provider.type === "postgresql") {
|
|
6007
6269
|
for (const [name, enumDef] of Object.entries(this.schema.enums)) {
|
|
6008
|
-
|
|
6270
|
+
let enumValues;
|
|
6271
|
+
if (enumDef.fields) {
|
|
6272
|
+
enumValues = Object.values(enumDef.fields).map((f) => {
|
|
6273
|
+
const mapAttr = f.attributes?.find((a) => a.name === "@map");
|
|
6274
|
+
if (!mapAttr || !mapAttr.args?.[0]) {
|
|
6275
|
+
return f.name;
|
|
6276
|
+
} else {
|
|
6277
|
+
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
6278
|
+
(0, import_common_helpers11.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
6279
|
+
return mappedName;
|
|
6280
|
+
}
|
|
6281
|
+
});
|
|
6282
|
+
} else {
|
|
6283
|
+
enumValues = Object.values(enumDef.values);
|
|
6284
|
+
}
|
|
6285
|
+
const createEnum = tx.schema.createType(name).asEnum(enumValues);
|
|
6009
6286
|
await createEnum.execute();
|
|
6010
6287
|
}
|
|
6011
6288
|
}
|
|
@@ -6484,7 +6761,7 @@ var ClientImpl = class _ClientImpl {
|
|
|
6484
6761
|
}
|
|
6485
6762
|
async handleProc(name, args) {
|
|
6486
6763
|
if (!("procedures" in this.$options) || !this.$options || typeof this.$options.procedures !== "object") {
|
|
6487
|
-
throw
|
|
6764
|
+
throw createConfigError("Procedures are not configured for the client.");
|
|
6488
6765
|
}
|
|
6489
6766
|
const procOptions = this.$options.procedures;
|
|
6490
6767
|
if (!procOptions[name] || typeof procOptions[name] !== "function") {
|
|
@@ -6615,7 +6892,7 @@ function createModelCrudHandler(client, model, inputValidator, resultProcessor)
|
|
|
6615
6892
|
const _handler = txClient ? handler.withClient(txClient) : handler;
|
|
6616
6893
|
const r = await _handler.handle(operation, _args);
|
|
6617
6894
|
if (!r && throwIfNoResult) {
|
|
6618
|
-
throw
|
|
6895
|
+
throw createNotFoundError(model);
|
|
6619
6896
|
}
|
|
6620
6897
|
let result;
|
|
6621
6898
|
if (r && postProcess) {
|
|
@@ -7010,6 +7287,18 @@ var DefaultOperationNodeVisitor = class extends import_kysely11.OperationNodeVis
|
|
|
7010
7287
|
visitOutput(node) {
|
|
7011
7288
|
this.defaultVisit(node);
|
|
7012
7289
|
}
|
|
7290
|
+
visitRenameConstraint(node) {
|
|
7291
|
+
this.defaultVisit(node);
|
|
7292
|
+
}
|
|
7293
|
+
visitRefreshMaterializedView(node) {
|
|
7294
|
+
this.defaultVisit(node);
|
|
7295
|
+
}
|
|
7296
|
+
visitOrAction(node) {
|
|
7297
|
+
this.defaultVisit(node);
|
|
7298
|
+
}
|
|
7299
|
+
visitCollate(node) {
|
|
7300
|
+
this.defaultVisit(node);
|
|
7301
|
+
}
|
|
7013
7302
|
};
|
|
7014
7303
|
|
|
7015
7304
|
// src/utils/schema-utils.ts
|
|
@@ -7073,18 +7362,14 @@ var ExpressionVisitor = class {
|
|
|
7073
7362
|
BaseCrudDialect,
|
|
7074
7363
|
CRUD,
|
|
7075
7364
|
CRUD_EXT,
|
|
7076
|
-
InputValidationError,
|
|
7077
|
-
InternalError,
|
|
7078
7365
|
KyselyUtils,
|
|
7079
|
-
|
|
7080
|
-
|
|
7366
|
+
ORMError,
|
|
7367
|
+
ORMErrorReason,
|
|
7081
7368
|
QueryUtils,
|
|
7082
|
-
RejectedByPolicyError,
|
|
7083
7369
|
RejectedByPolicyReason,
|
|
7084
7370
|
SchemaUtils,
|
|
7085
7371
|
TransactionIsolationLevel,
|
|
7086
7372
|
ZenStackClient,
|
|
7087
|
-
ZenStackError,
|
|
7088
7373
|
definePlugin,
|
|
7089
7374
|
getCrudDialect
|
|
7090
7375
|
});
|