@zenstackhq/runtime 3.0.0-beta.11 → 3.0.0-beta.13
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 +184 -94
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +184 -94
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -90,7 +90,6 @@ __export(query_utils_exports, {
|
|
|
90
90
|
requireField: () => requireField,
|
|
91
91
|
requireIdFields: () => requireIdFields,
|
|
92
92
|
requireModel: () => requireModel,
|
|
93
|
-
safeJSONStringify: () => safeJSONStringify,
|
|
94
93
|
stripAlias: () => stripAlias
|
|
95
94
|
});
|
|
96
95
|
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
@@ -205,10 +204,11 @@ var InputValidationError = class extends ZenStackError {
|
|
|
205
204
|
static {
|
|
206
205
|
__name(this, "InputValidationError");
|
|
207
206
|
}
|
|
208
|
-
|
|
207
|
+
model;
|
|
208
|
+
constructor(model, message, cause) {
|
|
209
209
|
super(message, {
|
|
210
210
|
cause
|
|
211
|
-
});
|
|
211
|
+
}), this.model = model;
|
|
212
212
|
}
|
|
213
213
|
};
|
|
214
214
|
var QueryError = class extends ZenStackError {
|
|
@@ -230,8 +230,9 @@ var NotFoundError = class extends ZenStackError {
|
|
|
230
230
|
static {
|
|
231
231
|
__name(this, "NotFoundError");
|
|
232
232
|
}
|
|
233
|
+
model;
|
|
233
234
|
constructor(model, details) {
|
|
234
|
-
super(`Entity not found for model "${model}"${details ? `: ${details}` : ""}`);
|
|
235
|
+
super(`Entity not found for model "${model}"${details ? `: ${details}` : ""}`), this.model = model;
|
|
235
236
|
}
|
|
236
237
|
};
|
|
237
238
|
var RejectedByPolicyReason = /* @__PURE__ */ function(RejectedByPolicyReason2) {
|
|
@@ -545,16 +546,6 @@ function ensureArray(value) {
|
|
|
545
546
|
}
|
|
546
547
|
}
|
|
547
548
|
__name(ensureArray, "ensureArray");
|
|
548
|
-
function safeJSONStringify(value) {
|
|
549
|
-
return JSON.stringify(value, (_, v) => {
|
|
550
|
-
if (typeof v === "bigint") {
|
|
551
|
-
return v.toString();
|
|
552
|
-
} else {
|
|
553
|
-
return v;
|
|
554
|
-
}
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
__name(safeJSONStringify, "safeJSONStringify");
|
|
558
549
|
function extractIdFields(entity, schema, model) {
|
|
559
550
|
const idFields = requireIdFields(schema, model);
|
|
560
551
|
return extractFields(entity, idFields);
|
|
@@ -1415,7 +1406,7 @@ var PostgresCrudDialect = class extends BaseCrudDialect {
|
|
|
1415
1406
|
}
|
|
1416
1407
|
}
|
|
1417
1408
|
transformOutputBytes(value) {
|
|
1418
|
-
return Buffer.isBuffer(value) ? Uint8Array.from(value) : value;
|
|
1409
|
+
return Buffer.isBuffer(value) ? Uint8Array.from(value) : typeof value === "string" && value.startsWith("\\x") ? Uint8Array.from(Buffer.from(value.slice(2), "hex")) : value;
|
|
1419
1410
|
}
|
|
1420
1411
|
buildRelationSelection(query, model, relationField, parentAlias, payload) {
|
|
1421
1412
|
const relationResultName = `${parentAlias}$${relationField}`;
|
|
@@ -1937,7 +1928,7 @@ var BaseOperationHandler = class {
|
|
|
1937
1928
|
buildCountSelection(query, model, parentAlias, payload) {
|
|
1938
1929
|
return query.select((eb) => this.dialect.buildCountJson(model, eb, parentAlias, payload).as("_count"));
|
|
1939
1930
|
}
|
|
1940
|
-
async create(kysely, model, data, fromRelation, creatingForDelegate = false) {
|
|
1931
|
+
async create(kysely, model, data, fromRelation, creatingForDelegate = false, returnFields) {
|
|
1941
1932
|
const modelDef = this.requireModel(model);
|
|
1942
1933
|
if (modelDef.isDelegate && !creatingForDelegate) {
|
|
1943
1934
|
throw new QueryError(`Model "${this.model}" is a delegate and cannot be created directly.`);
|
|
@@ -1990,8 +1981,8 @@ var BaseOperationHandler = class {
|
|
|
1990
1981
|
createFields = baseCreateResult.remainingFields;
|
|
1991
1982
|
}
|
|
1992
1983
|
const updatedData = this.fillGeneratedAndDefaultValues(modelDef, createFields);
|
|
1993
|
-
|
|
1994
|
-
const query = kysely.insertInto(model).$if(Object.keys(updatedData).length === 0, (qb) => qb.defaultValues()).$if(Object.keys(updatedData).length > 0, (qb) => qb.values(updatedData)).returning(
|
|
1984
|
+
returnFields = returnFields ?? requireIdFields(this.schema, model);
|
|
1985
|
+
const query = kysely.insertInto(model).$if(Object.keys(updatedData).length === 0, (qb) => qb.defaultValues()).$if(Object.keys(updatedData).length > 0, (qb) => qb.values(updatedData)).returning(returnFields).modifyEnd(this.makeContextComment({
|
|
1995
1986
|
model,
|
|
1996
1987
|
operation: "create"
|
|
1997
1988
|
}));
|
|
@@ -2196,7 +2187,7 @@ var BaseOperationHandler = class {
|
|
|
2196
2187
|
}
|
|
2197
2188
|
}
|
|
2198
2189
|
}
|
|
2199
|
-
async createMany(kysely, model, input, returnData, fromRelation) {
|
|
2190
|
+
async createMany(kysely, model, input, returnData, fromRelation, fieldsToReturn) {
|
|
2200
2191
|
if (!input.data || Array.isArray(input.data) && input.data.length === 0) {
|
|
2201
2192
|
return returnData ? [] : {
|
|
2202
2193
|
count: 0
|
|
@@ -2265,8 +2256,8 @@ var BaseOperationHandler = class {
|
|
|
2265
2256
|
count: Number(result.numAffectedRows)
|
|
2266
2257
|
};
|
|
2267
2258
|
} else {
|
|
2268
|
-
|
|
2269
|
-
const result = await query.returning(
|
|
2259
|
+
fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
|
|
2260
|
+
const result = await query.returning(fieldsToReturn).execute();
|
|
2270
2261
|
return result;
|
|
2271
2262
|
}
|
|
2272
2263
|
}
|
|
@@ -2346,7 +2337,7 @@ var BaseOperationHandler = class {
|
|
|
2346
2337
|
return void 0;
|
|
2347
2338
|
}
|
|
2348
2339
|
}
|
|
2349
|
-
async update(kysely, model, where, data, fromRelation, allowRelationUpdate = true, throwIfNotFound = true) {
|
|
2340
|
+
async update(kysely, model, where, data, fromRelation, allowRelationUpdate = true, throwIfNotFound = true, fieldsToReturn) {
|
|
2350
2341
|
if (!data || typeof data !== "object") {
|
|
2351
2342
|
throw new InternalError("data must be an object");
|
|
2352
2343
|
}
|
|
@@ -2452,8 +2443,8 @@ var BaseOperationHandler = class {
|
|
|
2452
2443
|
if (!hasFieldUpdate) {
|
|
2453
2444
|
return combinedWhere;
|
|
2454
2445
|
} else {
|
|
2455
|
-
|
|
2456
|
-
const query = kysely.updateTable(model).where(() => this.dialect.buildFilter(model, model, combinedWhere)).set(updateFields).returning(
|
|
2446
|
+
fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
|
|
2447
|
+
const query = kysely.updateTable(model).where(() => this.dialect.buildFilter(model, model, combinedWhere)).set(updateFields).returning(fieldsToReturn).modifyEnd(this.makeContextComment({
|
|
2457
2448
|
model,
|
|
2458
2449
|
operation: "update"
|
|
2459
2450
|
}));
|
|
@@ -2545,7 +2536,7 @@ var BaseOperationHandler = class {
|
|
|
2545
2536
|
makeContextComment(_context) {
|
|
2546
2537
|
return import_kysely5.sql``;
|
|
2547
2538
|
}
|
|
2548
|
-
async updateMany(kysely, model, where, data, limit, returnData, filterModel) {
|
|
2539
|
+
async updateMany(kysely, model, where, data, limit, returnData, filterModel, fieldsToReturn) {
|
|
2549
2540
|
if (typeof data !== "object") {
|
|
2550
2541
|
throw new InternalError("data must be an object");
|
|
2551
2542
|
}
|
|
@@ -2602,8 +2593,8 @@ var BaseOperationHandler = class {
|
|
|
2602
2593
|
count: Number(result.numAffectedRows)
|
|
2603
2594
|
};
|
|
2604
2595
|
} else {
|
|
2605
|
-
|
|
2606
|
-
const finalQuery = query.returning(
|
|
2596
|
+
fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
|
|
2597
|
+
const finalQuery = query.returning(fieldsToReturn);
|
|
2607
2598
|
const result = await this.executeQuery(kysely, finalQuery, "update");
|
|
2608
2599
|
return result.rows;
|
|
2609
2600
|
}
|
|
@@ -3007,7 +2998,7 @@ var BaseOperationHandler = class {
|
|
|
3007
2998
|
});
|
|
3008
2999
|
}
|
|
3009
3000
|
}
|
|
3010
|
-
if (throwForNotFound && expectedDeleteCount > deleteResult.
|
|
3001
|
+
if (throwForNotFound && expectedDeleteCount > deleteResult.rows.length) {
|
|
3011
3002
|
throw new NotFoundError(deleteFromModel);
|
|
3012
3003
|
}
|
|
3013
3004
|
}
|
|
@@ -3015,7 +3006,7 @@ var BaseOperationHandler = class {
|
|
|
3015
3006
|
return enumerate(data).map((item) => flattenCompoundUniqueFilters(this.schema, model, item));
|
|
3016
3007
|
}
|
|
3017
3008
|
// #endregion
|
|
3018
|
-
async delete(kysely, model, where, limit, filterModel) {
|
|
3009
|
+
async delete(kysely, model, where, limit, filterModel, fieldsToReturn) {
|
|
3019
3010
|
filterModel ??= model;
|
|
3020
3011
|
const modelDef = this.requireModel(model);
|
|
3021
3012
|
if (modelDef.baseModel) {
|
|
@@ -3024,7 +3015,8 @@ var BaseOperationHandler = class {
|
|
|
3024
3015
|
}
|
|
3025
3016
|
return this.processBaseModelDelete(kysely, modelDef.baseModel, where, limit, filterModel);
|
|
3026
3017
|
}
|
|
3027
|
-
|
|
3018
|
+
fieldsToReturn = fieldsToReturn ?? requireIdFields(this.schema, model);
|
|
3019
|
+
let query = kysely.deleteFrom(model).returning(fieldsToReturn);
|
|
3028
3020
|
let needIdFilter = false;
|
|
3029
3021
|
if (limit !== void 0 && !this.dialect.supportsDeleteWithLimit) {
|
|
3030
3022
|
needIdFilter = true;
|
|
@@ -3044,10 +3036,7 @@ var BaseOperationHandler = class {
|
|
|
3044
3036
|
model,
|
|
3045
3037
|
operation: "delete"
|
|
3046
3038
|
}));
|
|
3047
|
-
|
|
3048
|
-
return {
|
|
3049
|
-
count: Number(result.numAffectedRows)
|
|
3050
|
-
};
|
|
3039
|
+
return this.executeQuery(kysely, query, "delete");
|
|
3051
3040
|
}
|
|
3052
3041
|
async processDelegateRelationDelete(kysely, modelDef, where, limit) {
|
|
3053
3042
|
for (const fieldDef of Object.values(modelDef.fields)) {
|
|
@@ -3101,7 +3090,7 @@ var BaseOperationHandler = class {
|
|
|
3101
3090
|
return callback(this.kysely);
|
|
3102
3091
|
} else {
|
|
3103
3092
|
let txBuilder = this.kysely.transaction();
|
|
3104
|
-
txBuilder = txBuilder.setIsolationLevel(isolationLevel ?? TransactionIsolationLevel.
|
|
3093
|
+
txBuilder = txBuilder.setIsolationLevel(isolationLevel ?? TransactionIsolationLevel.ReadCommitted);
|
|
3105
3094
|
return txBuilder.execute(callback);
|
|
3106
3095
|
}
|
|
3107
3096
|
}
|
|
@@ -3153,6 +3142,48 @@ var BaseOperationHandler = class {
|
|
|
3153
3142
|
}
|
|
3154
3143
|
return result.rows[0];
|
|
3155
3144
|
}
|
|
3145
|
+
mutationNeedsReadBack(model, args) {
|
|
3146
|
+
if (this.hasPolicyEnabled) {
|
|
3147
|
+
return {
|
|
3148
|
+
needReadBack: true,
|
|
3149
|
+
selectedFields: void 0
|
|
3150
|
+
};
|
|
3151
|
+
}
|
|
3152
|
+
if (args.include && typeof args.include === "object" && Object.keys(args.include).length > 0) {
|
|
3153
|
+
return {
|
|
3154
|
+
needReadBack: true,
|
|
3155
|
+
selectedFields: void 0
|
|
3156
|
+
};
|
|
3157
|
+
}
|
|
3158
|
+
const modelDef = this.requireModel(model);
|
|
3159
|
+
if (modelDef.baseModel || modelDef.isDelegate) {
|
|
3160
|
+
return {
|
|
3161
|
+
needReadBack: true,
|
|
3162
|
+
selectedFields: void 0
|
|
3163
|
+
};
|
|
3164
|
+
}
|
|
3165
|
+
const allFields = Object.keys(modelDef.fields);
|
|
3166
|
+
const relationFields = Object.values(modelDef.fields).filter((f) => f.relation).map((f) => f.name);
|
|
3167
|
+
const computedFields = Object.values(modelDef.fields).filter((f) => f.computed).map((f) => f.name);
|
|
3168
|
+
const omit = Object.entries(args.omit ?? {}).filter(([, v]) => v).map(([k]) => k);
|
|
3169
|
+
const allFieldsSelected = [];
|
|
3170
|
+
if (!args.select || typeof args.select !== "object") {
|
|
3171
|
+
allFieldsSelected.push(...allFields.filter((f) => !relationFields.includes(f) && !omit.includes(f)));
|
|
3172
|
+
} else {
|
|
3173
|
+
allFieldsSelected.push(...Object.entries(args.select).filter(([k, v]) => v && !omit.includes(k)).map(([k]) => k));
|
|
3174
|
+
}
|
|
3175
|
+
if (allFieldsSelected.some((f) => relationFields.includes(f) || computedFields.includes(f))) {
|
|
3176
|
+
return {
|
|
3177
|
+
needReadBack: true,
|
|
3178
|
+
selectedFields: void 0
|
|
3179
|
+
};
|
|
3180
|
+
} else {
|
|
3181
|
+
return {
|
|
3182
|
+
needReadBack: false,
|
|
3183
|
+
selectedFields: allFieldsSelected
|
|
3184
|
+
};
|
|
3185
|
+
}
|
|
3186
|
+
}
|
|
3156
3187
|
};
|
|
3157
3188
|
|
|
3158
3189
|
// src/client/crud/operations/aggregate.ts
|
|
@@ -3313,14 +3344,19 @@ var CreateOperationHandler = class extends BaseOperationHandler {
|
|
|
3313
3344
|
}).exhaustive();
|
|
3314
3345
|
}
|
|
3315
3346
|
async runCreate(args) {
|
|
3347
|
+
const { needReadBack, selectedFields } = this.mutationNeedsReadBack(this.model, args);
|
|
3316
3348
|
const result = await this.safeTransaction(async (tx) => {
|
|
3317
|
-
const createResult = await this.create(tx, this.model, args.data);
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3349
|
+
const createResult = await this.create(tx, this.model, args.data, void 0, false, selectedFields);
|
|
3350
|
+
if (needReadBack) {
|
|
3351
|
+
return this.readUnique(tx, this.model, {
|
|
3352
|
+
select: args.select,
|
|
3353
|
+
include: args.include,
|
|
3354
|
+
omit: args.omit,
|
|
3355
|
+
where: getIdValues(this.schema, this.model, createResult)
|
|
3356
|
+
});
|
|
3357
|
+
} else {
|
|
3358
|
+
return createResult;
|
|
3359
|
+
}
|
|
3324
3360
|
});
|
|
3325
3361
|
if (!result && this.hasPolicyEnabled) {
|
|
3326
3362
|
throw new RejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, `result is not allowed to be read back`);
|
|
@@ -3339,15 +3375,20 @@ var CreateOperationHandler = class extends BaseOperationHandler {
|
|
|
3339
3375
|
if (args === void 0) {
|
|
3340
3376
|
return [];
|
|
3341
3377
|
}
|
|
3378
|
+
const { needReadBack, selectedFields } = this.mutationNeedsReadBack(this.model, args);
|
|
3342
3379
|
return this.safeTransaction(async (tx) => {
|
|
3343
|
-
const createResult = await this.createMany(tx, this.model, args, true);
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3380
|
+
const createResult = await this.createMany(tx, this.model, args, true, void 0, selectedFields);
|
|
3381
|
+
if (needReadBack) {
|
|
3382
|
+
return this.read(tx, this.model, {
|
|
3383
|
+
select: args.select,
|
|
3384
|
+
omit: args.omit,
|
|
3385
|
+
where: {
|
|
3386
|
+
OR: createResult.map((item) => getIdValues(this.schema, this.model, item))
|
|
3387
|
+
}
|
|
3388
|
+
});
|
|
3389
|
+
} else {
|
|
3390
|
+
return createResult;
|
|
3391
|
+
}
|
|
3351
3392
|
});
|
|
3352
3393
|
}
|
|
3353
3394
|
};
|
|
@@ -3363,27 +3404,34 @@ var DeleteOperationHandler = class extends BaseOperationHandler {
|
|
|
3363
3404
|
return (0, import_ts_pattern9.match)(operation).with("delete", () => this.runDelete(this.inputValidator.validateDeleteArgs(this.model, normalizedArgs))).with("deleteMany", () => this.runDeleteMany(this.inputValidator.validateDeleteManyArgs(this.model, normalizedArgs))).exhaustive();
|
|
3364
3405
|
}
|
|
3365
3406
|
async runDelete(args) {
|
|
3366
|
-
const
|
|
3367
|
-
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3407
|
+
const { needReadBack, selectedFields } = this.mutationNeedsReadBack(this.model, args);
|
|
3408
|
+
const result = await this.safeTransaction(async (tx) => {
|
|
3409
|
+
let preDeleteRead = void 0;
|
|
3410
|
+
if (needReadBack) {
|
|
3411
|
+
preDeleteRead = await this.readUnique(tx, this.model, {
|
|
3412
|
+
select: args.select,
|
|
3413
|
+
include: args.include,
|
|
3414
|
+
omit: args.omit,
|
|
3415
|
+
where: args.where
|
|
3416
|
+
});
|
|
3417
|
+
}
|
|
3418
|
+
const deleteResult = await this.delete(tx, this.model, args.where, void 0, void 0, selectedFields);
|
|
3419
|
+
if (deleteResult.rows.length === 0) {
|
|
3375
3420
|
throw new NotFoundError(this.model);
|
|
3376
3421
|
}
|
|
3422
|
+
return needReadBack ? preDeleteRead : deleteResult.rows[0];
|
|
3377
3423
|
});
|
|
3378
|
-
if (!
|
|
3424
|
+
if (!result && this.hasPolicyEnabled) {
|
|
3379
3425
|
throw new RejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3380
3426
|
}
|
|
3381
|
-
return
|
|
3427
|
+
return result;
|
|
3382
3428
|
}
|
|
3383
3429
|
async runDeleteMany(args) {
|
|
3384
3430
|
return await this.safeTransaction(async (tx) => {
|
|
3385
3431
|
const result = await this.delete(tx, this.model, args?.where, args?.limit);
|
|
3386
|
-
return
|
|
3432
|
+
return {
|
|
3433
|
+
count: result.rows.length
|
|
3434
|
+
};
|
|
3387
3435
|
});
|
|
3388
3436
|
}
|
|
3389
3437
|
};
|
|
@@ -3524,29 +3572,31 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3524
3572
|
return (0, import_ts_pattern11.match)(operation).with("update", () => this.runUpdate(this.inputValidator.validateUpdateArgs(this.model, normalizedArgs))).with("updateMany", () => this.runUpdateMany(this.inputValidator.validateUpdateManyArgs(this.model, normalizedArgs))).with("updateManyAndReturn", () => this.runUpdateManyAndReturn(this.inputValidator.validateUpdateManyAndReturnArgs(this.model, normalizedArgs))).with("upsert", () => this.runUpsert(this.inputValidator.validateUpsertArgs(this.model, normalizedArgs))).exhaustive();
|
|
3525
3573
|
}
|
|
3526
3574
|
async runUpdate(args) {
|
|
3527
|
-
const
|
|
3528
|
-
|
|
3529
|
-
const
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3575
|
+
const { needReadBack, selectedFields } = this.needReadBack(args);
|
|
3576
|
+
const result = await this.safeTransaction(async (tx) => {
|
|
3577
|
+
const updateResult = await this.update(tx, this.model, args.where, args.data, void 0, void 0, void 0, selectedFields);
|
|
3578
|
+
if (needReadBack) {
|
|
3579
|
+
const readFilter = updateResult ?? args.where;
|
|
3580
|
+
let readBackResult = void 0;
|
|
3581
|
+
readBackResult = await this.readUnique(tx, this.model, {
|
|
3533
3582
|
select: args.select,
|
|
3534
3583
|
include: args.include,
|
|
3535
3584
|
omit: args.omit,
|
|
3536
3585
|
where: readFilter
|
|
3537
3586
|
});
|
|
3538
|
-
|
|
3587
|
+
return readBackResult;
|
|
3588
|
+
} else {
|
|
3589
|
+
return updateResult;
|
|
3539
3590
|
}
|
|
3540
|
-
return readBackResult2;
|
|
3541
3591
|
});
|
|
3542
|
-
if (!
|
|
3592
|
+
if (!result) {
|
|
3543
3593
|
if (this.hasPolicyEnabled) {
|
|
3544
3594
|
throw new RejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3545
3595
|
} else {
|
|
3546
3596
|
return null;
|
|
3547
3597
|
}
|
|
3548
3598
|
} else {
|
|
3549
|
-
return
|
|
3599
|
+
return result;
|
|
3550
3600
|
}
|
|
3551
3601
|
}
|
|
3552
3602
|
async runUpdateMany(args) {
|
|
@@ -3558,19 +3608,27 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3558
3608
|
if (!args) {
|
|
3559
3609
|
return [];
|
|
3560
3610
|
}
|
|
3611
|
+
const { needReadBack, selectedFields } = this.needReadBack(args);
|
|
3561
3612
|
const { readBackResult, updateResult } = await this.safeTransaction(async (tx) => {
|
|
3562
|
-
const updateResult2 = await this.updateMany(tx, this.model, args.where, args.data, args.limit, true);
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3613
|
+
const updateResult2 = await this.updateMany(tx, this.model, args.where, args.data, args.limit, true, void 0, selectedFields);
|
|
3614
|
+
if (needReadBack) {
|
|
3615
|
+
const readBackResult2 = await this.read(tx, this.model, {
|
|
3616
|
+
select: args.select,
|
|
3617
|
+
omit: args.omit,
|
|
3618
|
+
where: {
|
|
3619
|
+
OR: updateResult2.map((item) => getIdValues(this.schema, this.model, item))
|
|
3620
|
+
}
|
|
3621
|
+
});
|
|
3622
|
+
return {
|
|
3623
|
+
readBackResult: readBackResult2,
|
|
3624
|
+
updateResult: updateResult2
|
|
3625
|
+
};
|
|
3626
|
+
} else {
|
|
3627
|
+
return {
|
|
3628
|
+
readBackResult: updateResult2,
|
|
3629
|
+
updateResult: updateResult2
|
|
3630
|
+
};
|
|
3631
|
+
}
|
|
3574
3632
|
});
|
|
3575
3633
|
if (readBackResult.length < updateResult.length && this.hasPolicyEnabled) {
|
|
3576
3634
|
throw new RejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
@@ -3578,23 +3636,49 @@ var UpdateOperationHandler = class extends BaseOperationHandler {
|
|
|
3578
3636
|
return readBackResult;
|
|
3579
3637
|
}
|
|
3580
3638
|
async runUpsert(args) {
|
|
3639
|
+
const { needReadBack, selectedFields } = this.needReadBack(args);
|
|
3581
3640
|
const result = await this.safeTransaction(async (tx) => {
|
|
3582
|
-
let mutationResult = await this.update(tx, this.model, args.where, args.update, void 0, true, false);
|
|
3641
|
+
let mutationResult = await this.update(tx, this.model, args.where, args.update, void 0, true, false, selectedFields);
|
|
3583
3642
|
if (!mutationResult) {
|
|
3584
|
-
mutationResult = await this.create(tx, this.model, args.create);
|
|
3643
|
+
mutationResult = await this.create(tx, this.model, args.create, void 0, void 0, selectedFields);
|
|
3644
|
+
}
|
|
3645
|
+
if (needReadBack) {
|
|
3646
|
+
return this.readUnique(tx, this.model, {
|
|
3647
|
+
select: args.select,
|
|
3648
|
+
include: args.include,
|
|
3649
|
+
omit: args.omit,
|
|
3650
|
+
where: getIdValues(this.schema, this.model, mutationResult)
|
|
3651
|
+
});
|
|
3652
|
+
} else {
|
|
3653
|
+
return mutationResult;
|
|
3585
3654
|
}
|
|
3586
|
-
return this.readUnique(tx, this.model, {
|
|
3587
|
-
select: args.select,
|
|
3588
|
-
include: args.include,
|
|
3589
|
-
omit: args.omit,
|
|
3590
|
-
where: getIdValues(this.schema, this.model, mutationResult)
|
|
3591
|
-
});
|
|
3592
3655
|
});
|
|
3593
3656
|
if (!result && this.hasPolicyEnabled) {
|
|
3594
3657
|
throw new RejectedByPolicyError(this.model, RejectedByPolicyReason.CANNOT_READ_BACK, "result is not allowed to be read back");
|
|
3595
3658
|
}
|
|
3596
3659
|
return result;
|
|
3597
3660
|
}
|
|
3661
|
+
needReadBack(args) {
|
|
3662
|
+
const baseResult = this.mutationNeedsReadBack(this.model, args);
|
|
3663
|
+
if (baseResult.needReadBack) {
|
|
3664
|
+
return baseResult;
|
|
3665
|
+
}
|
|
3666
|
+
const modelDef = this.requireModel(this.model);
|
|
3667
|
+
const nonRelationFields = Object.entries(modelDef.fields).filter(([_, def]) => !def.relation).map(([name, _]) => name);
|
|
3668
|
+
if (args.data && !Object.keys(args.data).some((field) => nonRelationFields.includes(field))) {
|
|
3669
|
+
return {
|
|
3670
|
+
needReadBack: true,
|
|
3671
|
+
selectedFields: void 0
|
|
3672
|
+
};
|
|
3673
|
+
}
|
|
3674
|
+
if (args.update && !Object.keys(args.update).some((field) => nonRelationFields.includes(field))) {
|
|
3675
|
+
return {
|
|
3676
|
+
needReadBack: true,
|
|
3677
|
+
selectedFields: void 0
|
|
3678
|
+
};
|
|
3679
|
+
}
|
|
3680
|
+
return baseResult;
|
|
3681
|
+
}
|
|
3598
3682
|
};
|
|
3599
3683
|
|
|
3600
3684
|
// src/client/crud/validator/index.ts
|
|
@@ -4044,7 +4128,7 @@ var InputValidator = class {
|
|
|
4044
4128
|
}
|
|
4045
4129
|
const { error, data } = schema.safeParse(args);
|
|
4046
4130
|
if (error) {
|
|
4047
|
-
throw new InputValidationError(`Invalid ${operation} args for model "${model}": ${formatError(error)}`, error);
|
|
4131
|
+
throw new InputValidationError(model, `Invalid ${operation} args for model "${model}": ${formatError(error)}`, error);
|
|
4048
4132
|
}
|
|
4049
4133
|
return data;
|
|
4050
4134
|
}
|
|
@@ -5598,7 +5682,7 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
|
|
|
5598
5682
|
try {
|
|
5599
5683
|
if (this.isMutationNode(compiledQuery.query) && !this.driver.isTransactionConnection(connection)) {
|
|
5600
5684
|
await this.driver.beginTransaction(connection, {
|
|
5601
|
-
isolationLevel: TransactionIsolationLevel.
|
|
5685
|
+
isolationLevel: TransactionIsolationLevel.ReadCommitted
|
|
5602
5686
|
});
|
|
5603
5687
|
startedTx = true;
|
|
5604
5688
|
}
|
|
@@ -6199,9 +6283,15 @@ var SchemaDbPusher = class {
|
|
|
6199
6283
|
table = table.addForeignKeyConstraint(`fk_${model}_${fieldName}`, fieldDef.relation.fields.map((f) => this.getColumnName(modelDef.fields[f])), this.getTableName(relationModelDef), fieldDef.relation.references.map((f) => this.getColumnName(relationModelDef.fields[f])), (cb) => {
|
|
6200
6284
|
if (fieldDef.relation?.onDelete) {
|
|
6201
6285
|
cb = cb.onDelete(this.mapCascadeAction(fieldDef.relation.onDelete));
|
|
6286
|
+
} else if (fieldDef.optional) {
|
|
6287
|
+
cb = cb.onDelete("set null");
|
|
6288
|
+
} else {
|
|
6289
|
+
cb = cb.onDelete("restrict");
|
|
6202
6290
|
}
|
|
6203
6291
|
if (fieldDef.relation?.onUpdate) {
|
|
6204
6292
|
cb = cb.onUpdate(this.mapCascadeAction(fieldDef.relation.onUpdate));
|
|
6293
|
+
} else {
|
|
6294
|
+
cb = cb.onUpdate("cascade");
|
|
6205
6295
|
}
|
|
6206
6296
|
return cb;
|
|
6207
6297
|
});
|