With the solution posted below my create
function player.service.ts
now looks like this:
async create(createPlayerDto: CreatePlayerDto): Promise<Player> {
const newPlayer = this.playerRepository.create(createPlayerDto);
return await this.playerRepository.save(newPlayer);
}
The hook within my player.entity.ts:
@BeforeInsert()
async hashPassword() {
console.log('Hash password!');
this.password = await bcrypt.hash(this.password, this.saltOrRounds);
}
For my project with NestJS I have created a Player entity (player.entity.ts
), which has the following columns and one hook. I have connected a MySQL8.0 database via the TypeORM package.
import {
Entity,
Column,
PrimaryGeneratedColumn,
CreateDateColumn,
UpdateDateColumn,
BeforeInsert,
} from 'typeorm';
import * as bcrypt from 'bcrypt';
@Entity({ name: 'players' })
export class Player {
readonly saltOrRounds = 10;
@PrimaryGeneratedColumn()
id: number;
@Column({
type: 'varchar',
unique: true,
})
username: string;
@Column()
password: string;
@Column({
unique: true,
type: 'varchar',
})
email: string;
@CreateDateColumn({
name: 'created_at',
type: 'datetime',
})
created_at: 'datetime';
@UpdateDateColumn({
name: 'updated_at',
type: 'datetime',
})
updated_at: 'datetime';
@BeforeInsert()
async hashPassword() {
return (
this.password && (await bcrypt.hash(this.password, this.saltOrRounds))
);
}
}
As you can see the @BeforeInsert()
hook should take the password, hash it and then return the hashed password.
The relevant route for the creation of a new player is placed within the players.controller.ts
:
import { Body, Controller, Delete, Get, Param, Post } from '@nestjs/common';
import { PlayersService } from './players.service';
import { CreatePlayerDto } from './dto/create-player.dto';
import { Player } from './interfaces/player.interface';
@Controller('players')
export class PlayersController {
constructor(private playerService: PlayersService) {}
@Post()
async create(@Body() createPlayerDto: CreatePlayerDto) {
return this.playerService.create(createPlayerDto);
}
}
The controller utilizes the player.service.ts
and makes uses the EntityManager to perform the create/insert operation:
import { Injectable } from '@nestjs/common';
import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm';
import { Player } from './entities/player.entity';
import { EntityManager, Repository } from 'typeorm';
import { CreatePlayerDto } from './dto/create-player.dto';
@Injectable()
export class PlayersService {
constructor(
@InjectEntityManager()
private entityManager: EntityManager,
@InjectRepository(Player)
private playerRepository: Repository<Player>,
) {}
async create(createPlayerDto: CreatePlayerDto): Promise<Player> {
return this.entityManager.save(Player, createPlayerDto);
}
}
I've also tried to use the Repository with the same result. A new player is created everytime I make a POST request to the /create
endpoint. But unfortunatelly none of the used hooks and/or listeners work.
Instantiate the Entity, assign the attributes to it and then save.
async create(attributes: DeepPartial<T>) {
const playerEntity = Object.assign(new Player(), attributes);
return this.repository.save(playerEntity);
}
or you could use the create
method on the repository and then save it.
const record = playerRepository.create(attributes);
await playerRepository.save(record);