seed database with an entity containing methods in typeorm

Viewed 1238

I'm using typeorm & nestjs.

I have an entity that looks like this but with extra attributes:

 export class User extends BaseEntity {

  @ApiModelProperty({ uniqueItems: true, example: 'myuser', description: 'User login' })
  @Column({ unique: true })
  login: string;

  @ApiModelProperty({ example: 'MyUser', description: 'User first name' })
  @Column()
  firstName: string;


  @ApiModelProperty({ example: 'myuser', description: 'User password' })
  @Column()
  password: string;

  @BeforeInsert()
  private async hashPassword() {
    this.password = bcrypt.hashSync(this.password, 10);
  }
}

I seed the database by creating mock objects ex:

 user1: User = {
    login: 'system',
    password: 'system',
    firstName: 'System',
    lastName: 'System',
    email: 'system@localhost.it',
    imageUrl: '',
    activated: true,
    langKey: 'en',
    createdBy: 'system',
    lastModifiedBy: 'system',
    phone: '0565659562',
    verifiedPhone: false,
    referal: '',
    referedBy: '',
    miniBio: ''
  };

& get this error:

      src/domain/user.entity.ts:49:17
    49   private async hashPassword() {
                       ~~~~~~~~~~~~
    'hashPassword' is declared here.
src/migrations/1570200490072-SeedUsersRoles.ts:64:3 - error TS2741: Property 'hashPassword' is missing in type '{ login: string; password: string; firstName: string; lastName: string; email: string; imageUrl: string; activated: true; langKey: string; createdBy: string; lastModifiedBy: string; phone: string; verifiedPhone: false; referal: string; referedBy: string; miniBio: string; }' but required in type 'User'.

is there any decorator to decorate methods & ignore them in object construction ?

0 Answers
Related