[BUG] @BeforeInsert method isn't executed.
Issue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[x] 0.2.25 (or put your version here)
Steps to reproduce or a small repository showing the problem: I have an User entity:
import {Entity, Column, PrimaryGeneratedColumn, BeforeInsert} from 'typeorm';
import bcrypt from 'bcrypt';
// Esta clase puede heredar de otra entidad y compartir sus columnas
@Entity()
export class User {
@BeforeInsert()
private encryptPassword() {
console.log("Encrypting password... UwU");
this.password = bcrypt.hashSync(this.password, 10);
}
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({
type: 'varchar',
nullable: false,
length: 50,
})
name: string;
@Column({
type: 'varchar',
nullable: false,
length: 64,
})
email: string;
@Column({
type: "varchar",
nullable: false,
length: 64,
})
password: string;
}
and a repository:
import { getManager, UpdateResult, DeleteResult } from 'typeorm';
import { User } from '../Entities/User';
export class UserRepository {
createUser(user: User): Promise<User> {
return getManager().getRepository(User).save(user);
}
getUserList(): Promise<User[]> {
return getManager().getRepository(User).find();
}
getUser(idUser: string): Promise<User | undefined> {
return getManager().getRepository(User).findOne({
where: {
id: idUser
}
});
}
updateUser(idUser: string, newUserData: User): Promise<UpdateResult>{
return getManager().getRepository(User).update({id: idUser}, newUserData);
}
deleteUser(idUser: string): Promise<DeleteResult> {
return getManager().getRepository(User).delete({id: idUser});
}
}
And the method whit the @BeforeInsert decorator isn't running when I put new data to the database, I call createUser() inside an express route:
app.post('/user', async (req, res) => {
try {
res.send(await userRepo.createUser(req.body));
} catch (e) {
if (e == QueryFailedError){
res.send("Error in the query structure")
}
res.send("Error creating user");
}
});
I think it is a bug, but maybe I have some mistake, I'm learning Typescript and typeorm.
Sorry if my english isn't the best UwU.
It looks like your type definitions are wrong.
This function is typed to receive an instance the entity user, which would be correct, but it's receiving a non-entity req.body object from your route/controller.
createUser(user: User): Promise<User> {
return getManager().getRepository(User).save(user);
}
You need in instantiate the entity before passing it to the repository to save.
https://typeorm.io/#/undefined/using-repositories https://github.com/typeorm/typeorm/issues/674#issuecomment-474400834
It looks like your type definitions are wrong.
This function is typed to receive an instance the entity user, which would be correct, but it's receiving a non-entity
req.bodyobject from your route/controller.createUser(user: User): Promise<User> { return getManager().getRepository(User).save(user); }You need in instantiate the entity before passing it to the repository to save.
https://typeorm.io/#/undefined/using-repositories #674 (comment)
Thank you <3 At the end I don't use my own repository. I don't need it for now.