Skip to content

Commit 4a72ed4

Browse files
committed
update
1 parent 63690b5 commit 4a72ed4

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

packages/runtime/src/client/crud/operations/base.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,18 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
942942
const modelDef = this.requireModel(model);
943943
let finalData = data;
944944

945+
// fill in automatically updated fields
946+
const autoUpdatedFields: string[] = [];
947+
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
948+
if (fieldDef.updatedAt) {
949+
if (finalData === data) {
950+
finalData = clone(data);
951+
}
952+
finalData[fieldName] = this.dialect.transformPrimitive(new Date(), 'DateTime', false);
953+
autoUpdatedFields.push(fieldName);
954+
}
955+
}
956+
945957
if (Object.keys(finalData).length === 0) {
946958
// nothing to update, return the original filter so that caller can identify the entity
947959
return combinedWhere;
@@ -1018,20 +1030,13 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
10181030
}
10191031
}
10201032

1021-
// fill in automatically updated fields
1022-
const scalarFields = Object.values(modelDef.fields)
1023-
.filter((f) => !f.relation)
1024-
.map((f) => f.name);
1025-
if (Object.keys(updateFields).some((f) => scalarFields.includes(f))) {
1026-
// if any scalar fields are being updated, also update the `updatedAt` fields
1027-
for (const [fieldName, fieldDef] of Object.entries(modelDef.fields)) {
1028-
if (fieldDef.updatedAt) {
1029-
updateFields[fieldName] = this.dialect.transformPrimitive(new Date(), 'DateTime', false);
1030-
}
1031-
}
1033+
let hasFieldUpdate = Object.keys(updateFields).length > 0;
1034+
if (hasFieldUpdate) {
1035+
// check if only updating auto-updated fields, if so, we can skip the update
1036+
hasFieldUpdate = Object.keys(updateFields).some((f) => !autoUpdatedFields.includes(f));
10321037
}
10331038

1034-
if (Object.keys(updateFields).length === 0) {
1039+
if (!hasFieldUpdate) {
10351040
// nothing to update, return the filter so that the caller can identify the entity
10361041
return combinedWhere;
10371042
} else {

tests/e2e/orm/client-api/update.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ describe('Client update tests', () => {
3838
email: user.email,
3939
name: user.name,
4040
});
41-
expect(updated.updatedAt.getTime()).toBeGreaterThan(user.updatedAt.getTime());
41+
// should not update updatedAt
42+
expect(updated.updatedAt.getTime()).toEqual(user.updatedAt.getTime());
4243

4344
// id as filter
4445
updated = await client.user.update({

tests/e2e/orm/policy/migrated/omit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('prisma omit', () => {
1010
name String
1111
profile Profile?
1212
age Int
13-
value Int @allow('read', age > 20)
13+
value Int
1414
@@allow('all', age > 18)
1515
}
1616

0 commit comments

Comments
 (0)