Skip to content

Commit

Permalink
Merge pull request #49 from solanamonk/fix_save_parent_on_update
Browse files Browse the repository at this point in the history
fix: save parent on update
  • Loading branch information
bashleigh authored Mar 29, 2024
2 parents da645cd + 6086968 commit 0db9b2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/__tests__/polymorphic.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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,
Expand Down
2 changes: 1 addition & 1 deletion src/polymorphic.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ export abstract class AbstractPolymorphicRepository<
).map((entity: E | DeepPartial<E>) => {
const parent = entity[options.propertyKey];

if (!parent || entity[entityIdColumn(options)] !== undefined) {
if (!parent) {
return entity;
}

Expand Down

0 comments on commit 0db9b2e

Please sign in to comment.