Created
September 24, 2022 18:06
-
-
Save MuyembeII/4814d5ad4ec822853f76a4cb2eef8406 to your computer and use it in GitHub Desktop.
Nest JS Typescript DTO Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const toUserDto = (data: UserEntity): UserDto => { | |
const { id, username, email } = data; | |
let userDto: UserDto = { id, username, email, }; | |
return userDto; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class UserDto { | |
@IsNotEmpty() id: string; | |
@IsNotEmpty() username: string; | |
@IsNotEmpty() @IsEmail() email: string; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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