Skip to content

Instantly share code, notes, and snippets.

@MuyembeII
Created September 24, 2022 18:06
Show Gist options
  • Save MuyembeII/4814d5ad4ec822853f76a4cb2eef8406 to your computer and use it in GitHub Desktop.
Save MuyembeII/4814d5ad4ec822853f76a4cb2eef8406 to your computer and use it in GitHub Desktop.
Nest JS Typescript DTO Helper
export const toUserDto = (data: UserEntity): UserDto => {
const { id, username, email } = data;
let userDto: UserDto = { id, username, email, };
return userDto;
};
export class UserDto {
@IsNotEmpty() id: string;
@IsNotEmpty() username: string;
@IsNotEmpty() @IsEmail() email: string;
}
@Entity('user')
export class UserEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column({
type: 'varchar',
nullable: false,
unique: true
})
username: string;
@Column({
type: 'varchar',
nullable: false
})
password: string; @Column({
type: 'varchar',
nullable: false
})
email: string;
@BeforeInsert() async hashPassword() {
this.password = await bcrypt.hash(this.password, 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment