Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: use a constistent name for entityId column option #50

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/__tests__/entities/advert.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ export class AdvertEntity {

@Column({ nullable: true })
entityType: string;

@PolymorphicParent(() => [UserEntity, MerchantEntity], {
entityTypeColumn: 'creatorType',
entityIdColumn: 'creatorId',
eager: true,
})
creator: UserEntity | MerchantEntity;

@Column({ nullable: true })
creatorId: number;

@Column({ nullable: true })
creatorType: string;
}
7 changes: 7 additions & 0 deletions src/__tests__/entities/merchant.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ export class MerchantEntity {
eager: false,
})
adverts: AdvertEntity[];

@PolymorphicChildren(() => AdvertEntity, {
entityTypeColumn: 'creatorType',
entityIdColumn: 'creatorId',
eager: false,
})
createdAdverts: AdvertEntity[];
}
7 changes: 7 additions & 0 deletions src/__tests__/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ export class UserEntity {
eager: false,
})
adverts: AdvertEntity[];

@PolymorphicChildren(() => AdvertEntity, {
entityTypeColumn: 'creatorType',
entityIdColumn: 'creatorId',
eager: false,
})
createdAdverts: AdvertEntity[];
}
57 changes: 57 additions & 0 deletions src/__tests__/polymorphic.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,31 @@ describe('AbstractPolymorphicRepository', () => {
expect(result?.entityId).toBeNull();
expect(result?.entityType).toBeNull();
});

it('Can find entity with customized entity and id columns', async () => {
const repository = AbstractPolymorphicRepository.createRepository(
connection,
AdvertRepository,
);
const userRepository = connection.getRepository(UserEntity);

const user = await userRepository.save(new UserEntity());

const advert = await repository.save(
repository.create({
creator: user,
}),
);

const result = await repository.findOne({ where: { id: advert.id } });

expect(result).toBeInstanceOf(AdvertEntity);

expect(result?.creator).toBeInstanceOf(UserEntity);
expect(result?.creator.id).toBe(result?.creatorId);
expect(result?.creatorType).toBe(UserEntity.name);
expect(result?.creatorId).toBe(result?.creator.id);
});
});

describe('find', () => {
Expand Down Expand Up @@ -265,6 +290,38 @@ describe('AbstractPolymorphicRepository', () => {
expect(result?.adverts[0].entityType).toBe(UserEntity.name);
expect(result?.adverts[0].entityId).toBe(user.id);
});

it('Can find parent entity with children with customized entity and id columns', async () => {
const repository = AbstractPolymorphicRepository.createRepository(
connection,
UserRepository,
);
const advertRepository = AbstractPolymorphicRepository.createRepository(
connection,
AdvertRepository,
);

const user = await repository.save(new UserEntity());

const advert = await advertRepository.save(
advertRepository.create({
creator: user,
}),
);

let result = await repository.findOne({
where: { id: user.id },
});

result = await repository.hydrateOne(result);

expect(result).toBeInstanceOf(UserEntity);
expect(result?.createdAdverts).toHaveLength(1);
expect(result?.createdAdverts[0]).toBeInstanceOf(AdvertEntity);
expect(result?.createdAdverts[0].id).toBe(advert.id);
expect(result?.createdAdverts[0].creatorType).toBe(UserEntity.name);
expect(result?.createdAdverts[0].creatorId).toBe(user.id);
});
});
});
});
4 changes: 2 additions & 2 deletions src/polymorphic.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface PolymorphicInterface {
hasMany: boolean;
primaryColumn?: string;
entityTypeColumn?: string;
entityTypeId?: string;
entityIdColumn?: string;
eager: boolean;
cascade: boolean;
deleteBeforeUpdate: boolean;
Expand All @@ -31,7 +31,7 @@ export interface PolymorphicDecoratorOptionsInterface {
cascade?: boolean;
eager?: boolean;
entityTypeColumn?: string;
entityTypeId?: string;
entityIdColumn?: string;
}

export type PolymorphicChildType = {
Expand Down
2 changes: 1 addition & 1 deletion src/polymorphic.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type PolymorphicHydrationType = {
const entityTypeColumn = (options: PolymorphicMetadataInterface): string =>
options.entityTypeColumn || 'entityType';
const entityIdColumn = (options: PolymorphicMetadataInterface): string =>
options.entityTypeId || 'entityId';
options.entityIdColumn || 'entityId';
const PrimaryColumn = (options: PolymorphicMetadataInterface): string =>
options.primaryColumn || 'id';

Expand Down
Loading