From 97ea0d80ccfb52607995e6ae84faae6e93123429 Mon Sep 17 00:00:00 2001 From: Solanamonk Date: Sat, 16 Mar 2024 21:44:12 +0100 Subject: [PATCH] feat: permit nullable parent --- src/__tests__/entities/advert.entity.ts | 4 +- src/__tests__/polymorphic.repository.spec.ts | 47 ++++++++++++++++---- src/polymorphic.repository.ts | 4 +- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/__tests__/entities/advert.entity.ts b/src/__tests__/entities/advert.entity.ts index 9356aea..a307f86 100644 --- a/src/__tests__/entities/advert.entity.ts +++ b/src/__tests__/entities/advert.entity.ts @@ -13,9 +13,9 @@ export class AdvertEntity { }) owner: UserEntity | MerchantEntity; - @Column() + @Column({ nullable: true }) entityId: number; - @Column() + @Column({ nullable: true }) entityType: string; } diff --git a/src/__tests__/polymorphic.repository.spec.ts b/src/__tests__/polymorphic.repository.spec.ts index 6a486df..debb811 100644 --- a/src/__tests__/polymorphic.repository.spec.ts +++ b/src/__tests__/polymorphic.repository.spec.ts @@ -10,9 +10,6 @@ import { MerchantEntity } from './entities/merchant.entity'; describe('AbstractPolymorphicRepository', () => { let connection: DataSource; - let userRepository: Repository; - let repository: AbstractPolymorphicRepository; - beforeAll(async () => { config({ path: resolve(__dirname, '.', '..', '..', '.env'), @@ -33,14 +30,14 @@ describe('AbstractPolymorphicRepository', () => { afterAll(async () => { await connection.destroy(); + }); + + afterEach(async () => { + const userRepository = connection.getRepository(UserEntity); + const repository = connection.getRepository(AdvertEntity); await userRepository.createQueryBuilder().delete().execute(); await repository.createQueryBuilder().delete().execute(); - - await Promise.all([ - userRepository.createQueryBuilder().delete().execute(), - repository.createQueryBuilder().delete().execute(), - ]); }); describe('Childen', () => { @@ -138,6 +135,22 @@ describe('AbstractPolymorphicRepository', () => { expect(result?.owner.id).toBe(result?.entityId); expect(result?.entityType).toBe(UserEntity.name); }); + + it('Can find entity without parent', async () => { + const repository = AbstractPolymorphicRepository.createRepository( + connection, + AdvertRepository, + ); + + const advert = await repository.save(repository.create({})); + + const result = await repository.findOne({ where: { id: advert.id } }); + + expect(result).toBeInstanceOf(AdvertEntity); + expect(result?.owner).toBeNull(); + expect(result?.entityId).toBeNull(); + expect(result?.entityType).toBeNull(); + }); }); describe('find', () => { @@ -170,6 +183,24 @@ describe('AbstractPolymorphicRepository', () => { expect(res.entityId).toBe(res.owner.id); }); }); + + it('Can find entities without parent', async () => { + const repository = AbstractPolymorphicRepository.createRepository( + connection, + AdvertRepository, + ); + + await repository.save([repository.create({}), repository.create({})]); + + const result = await repository.find(); + + result.forEach((res) => { + expect(res).toBeInstanceOf(AdvertEntity); + expect(res.owner).toBeNull(); + expect(res.entityId).toBeNull(); + expect(res.entityType).toBeNull(); + }); + }); }); }); }); diff --git a/src/polymorphic.repository.ts b/src/polymorphic.repository.ts index 89b2555..8ec9a82 100644 --- a/src/polymorphic.repository.ts +++ b/src/polymorphic.repository.ts @@ -127,7 +127,7 @@ export abstract class AbstractPolymorphicRepository< return values.reduce((e: E, vals: PolymorphicHydrationType) => { const values = vals.type === 'parent' && Array.isArray(vals.values) - ? vals.values.filter((v) => typeof v !== 'undefined' && v !== null) + ? vals.values.filter((v) => typeof v !== 'undefined') : vals.values; const polys = vals.type === 'parent' && Array.isArray(values) ? values[0] : values; // TODO should be condition for !hasMany @@ -153,7 +153,7 @@ export abstract class AbstractPolymorphicRepository< // TODO if not hasMany, should I return if one is found? const results = await Promise.all( entityTypes.map((type: Function) => - this.findPolymorphs(entity, type, options), + type ? this.findPolymorphs(entity, type, options) : null, ), );