How to create entity classes for typeorm on-the-fly at runtime

Viewed 501

I am trying to create Typeorm entities on-the-fly at runtime, then add them to a connection explicitly as I create it. I'm running into some trouble figuring out how to use the property decorators.

As a control, creating the entity declaratively works fine:

@Entity('table_name')
export class NewEntity extends BaseEntity {

  @PrimaryGeneratedColumn()
  public id: number;

}

Then the class decorator works fine as a regular function:

class NewEntity {

    @PrimaryGeneratedColumn()
    public id: number;

}

Entity('table_name')(NewEntity);

But using PrimaryGeneratedColumn() as a regular function:

class NewEntity {
    public id: number;
}

PrimaryGeneratedColumn()(NewEntity, 'id');
Entity('table_name')(NewEntity);

gives: Entity "NewEntity" does not have a primary column. Primary column is required to have in all your entities. Use @PrimaryColumn decorator to add a primary column to your entity. when the typeorm connection is created.

I know this is a weird use case! But if anyone knows how to do this it would be much appreciated!! :)

0 Answers
Related