I faced a situation where I want extends multiple classes. I have class A named a.entity.ts
and I'm extending this class to BaseEntity
(which is the predefined class in typeORM) as follow:
@Entity()
export class A extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column({length: 100, nullable: true})
description: string;
}
Also, I have my own custom abstract class DateAudit
for Auditing date purpose as follow:
export abstract class DateAudit {
@CreateDateColumn()
created: Date;
@UpdateDateColumn()
updated: Date;
}
I want to use this DateAudit
along with BaseEntity
class in my class A like :
export class A extends BaseEntity, DateAudit
I know multiple inheritances are not possible. Looking forward to knowing how I can achieve this type of scenario.
Thanks in advance!
You can do it as follow:
export abstract class DateAudit extends BaseEntity {
@CreateDateColumn()
created: Date;
@UpdateDateColumn()
updated: Date;
}
then your entity will do:
@Entity()
export class A extends DateAudit {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column({length: 100, nullable: true})
description: string;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With