How I can use getter and setter in TypeOrm.
I saw issues in here and also here, but not found answer
For example a left my User entity
export class User {
@PrimaryGeneratedColumn()
private id: number;
@Column()
@Length(4, 20)
@IsNotEmpty()
private name: string;
@Column()
@Length(4, 100)
@IsNotEmpty()
private password: string;
public getId(): number {
return this.id;
}
public getPassword(password: string): string {
return this.password;
}
public setPassword(password: string): User {
this.password = bcrypt.hashSync(password, 8);
return this;
}
public setName(name: string): User {
this.name = name;
return this;
}
}
I use orm version 0.2.7
@BeforeInsert
& @AfterLoad
BeforeInsert https://typeorm.io/#/listeners-and-subscribers/beforeinsert
AfterLoad https://typeorm.io/#/listeners-and-subscribers/afterload
Use @BeforeInsert
as setter
Use @AfterLoad
as getter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With