Skip to content

Commit

Permalink
Merge pull request #48 from solanamonk/permit_nullable_parent
Browse files Browse the repository at this point in the history
feat: permit nullable parent
  • Loading branch information
bashleigh authored Mar 29, 2024
2 parents 9663935 + 97ea0d8 commit da645cd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/entities/advert.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class AdvertEntity {
})
owner: UserEntity | MerchantEntity;

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

@Column()
@Column({ nullable: true })
entityType: string;
}
47 changes: 39 additions & 8 deletions src/__tests__/polymorphic.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import { MerchantEntity } from './entities/merchant.entity';
describe('AbstractPolymorphicRepository', () => {
let connection: DataSource;

let userRepository: Repository<UserEntity>;
let repository: AbstractPolymorphicRepository<AdvertEntity>;

beforeAll(async () => {
config({
path: resolve(__dirname, '.', '..', '..', '.env'),
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
});
});
4 changes: 2 additions & 2 deletions src/polymorphic.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export abstract class AbstractPolymorphicRepository<
return values.reduce<E>((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
Expand All @@ -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,
),
);

Expand Down

0 comments on commit da645cd

Please sign in to comment.