Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we extends multiple classes in NestJS for TypeORM?

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!

like image 278
I'm_Pratik Avatar asked Dec 18 '22 11:12

I'm_Pratik


1 Answers

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;
}
like image 185
cojack Avatar answered Dec 31 '22 10:12

cojack