2
votes

If I want to add similar columns to all my entities in TypeORM, for example, each entity should have a CreatedDate and an UpdatedDate column in their respected database table and entity, using TypeORM's built in @CreateDateColumn and @UpdatedDateColumn decorators. I can achieve this by creating an embedded entity, and it works as expected, but when I query it and it returns its JSON format, the embedded entity is a sub-object. Is there a way to either flatten that sub-object in the JSON, or add these columns to all entities another way? I also used inheritance but I want these particular columns to show up as the last columns in the table/entity. When using inheritance they show up first.

1

1 Answers

0
votes

If you don't want to use composition for that, you can use inheritance with an abstract class.

export abstract class AbstractEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @CreatedDateColumn()
    createdDate: Date;
}

@Entity()
export class MyEntity extends AbstractEntity {
    // Some code
}