种子数据库,其实体包含typeorm中的方法

时间:2020-06-22 23:15:14

标签: nestjs typeorm

我正在使用typeorm和nestjs。

我有一个看起来像这样但具有额外属性的实体:

 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);
  }
}

我通过创建模拟对象来播种数据库,例如:

 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: ''
  };

&出现此错误:

      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'.

是否有装饰器来装饰方法并在对象构造中忽略它们?

0 个答案:

没有答案