From 60869683136111ac04455fbb752e3866d592a181 Mon Sep 17 00:00:00 2001 From: Solanamonk Date: Wed, 20 Mar 2024 18:35:27 +0100 Subject: [PATCH] fix: save parent on update --- src/__tests__/polymorphic.repository.spec.ts | 27 ++++++++++++++++++++ src/polymorphic.repository.ts | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/__tests__/polymorphic.repository.spec.ts b/src/__tests__/polymorphic.repository.spec.ts index 6a486df..1c970b7 100644 --- a/src/__tests__/polymorphic.repository.spec.ts +++ b/src/__tests__/polymorphic.repository.spec.ts @@ -86,6 +86,33 @@ describe('AbstractPolymorphicRepository', () => { expect(result.entityId).toBe(result.owner.id); }); + it('Can update parent', async () => { + const repository = AbstractPolymorphicRepository.createRepository( + connection, + AdvertRepository, + ); + const userRepository = connection.getRepository(UserEntity); + + const user = await userRepository.save(new UserEntity()); + + const result = await repository.save( + repository.create({ + owner: user, + }), + ); + + const otherUser = await userRepository.save(new UserEntity()); + result.owner = otherUser; + await repository.save(result); + + expect(result).toBeInstanceOf(AdvertEntity); + expect(result.owner).toBeInstanceOf(UserEntity); + expect(result.id).toBeTruthy(); + expect(result.owner.id).toBeTruthy(); + expect(result.entityType).toBe(UserEntity.name); + expect(result.entityId).toBe(otherUser.id); + }); + it('Can save many with cascade parent', async () => { const repository = AbstractPolymorphicRepository.createRepository( connection, diff --git a/src/polymorphic.repository.ts b/src/polymorphic.repository.ts index 89b2555..246efdd 100644 --- a/src/polymorphic.repository.ts +++ b/src/polymorphic.repository.ts @@ -262,7 +262,7 @@ export abstract class AbstractPolymorphicRepository< ).map((entity: E | DeepPartial) => { const parent = entity[options.propertyKey]; - if (!parent || entity[entityIdColumn(options)] !== undefined) { + if (!parent) { return entity; }