Skip to content

Commit d49c39e

Browse files
authored
fix: enhance delegate model interaction with afterEntityMutation plugin support in child updates (#2369)
1 parent 608133a commit d49c39e

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,10 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
12281228
);
12291229
// only fields not consumed by base update will be used for this model
12301230
finalData = baseUpdateResult.remainingFields;
1231-
// base update may change entity ids, update the filter
1232-
combinedWhere = baseUpdateResult.baseEntity;
1231+
// make sure to include only the id fields from the base entity in the final filter
1232+
combinedWhere = baseUpdateResult.baseEntity
1233+
? getIdValues(this.schema, modelDef.baseModel!, baseUpdateResult.baseEntity)
1234+
: baseUpdateResult.baseEntity;
12331235

12341236
// update this entity with fields in updated base
12351237
if (baseUpdateResult.baseEntity) {

tests/e2e/orm/plugin-infra/entity-mutation-hooks.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,3 +692,45 @@ describe('Entity mutation hooks tests', () => {
692692
});
693693
});
694694
});
695+
696+
describe('Entity mutation hooks - delegate model interaction', () => {
697+
it('update on child model succeeds with afterEntityMutation plugin', async () => {
698+
console.log('RUNNING!!!!');
699+
const client = await createTestClient(
700+
`
701+
model Base {
702+
id Int @id @default(autoincrement())
703+
orgId Int
704+
type String
705+
@@delegate(type)
706+
}
707+
708+
model Child extends Base {
709+
childField Int
710+
}
711+
`,
712+
);
713+
714+
const withPlugin = client.$use({
715+
id: 'test',
716+
onEntityMutation: {
717+
afterEntityMutation() {},
718+
},
719+
});
720+
721+
const created = await withPlugin.child.create({
722+
data: { orgId: 1, childField: 10 },
723+
});
724+
725+
const updated = await withPlugin.child.update({
726+
where: { id: created.id },
727+
data: { orgId: 2, childField: 20 },
728+
});
729+
730+
expect(updated).toBeTruthy();
731+
expect(updated.orgId).toBe(2);
732+
expect(updated.childField).toBe(20);
733+
734+
await client.$disconnect();
735+
});
736+
});

0 commit comments

Comments
 (0)