Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: permit nullable parent #48

Merged
merged 1 commit into from
Mar 29, 2024
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
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
Loading