Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeforeInsert Issue #2924

Closed
JamesCoonce opened this issue Oct 12, 2018 · 12 comments
Closed

BeforeInsert Issue #2924

JamesCoonce opened this issue Oct 12, 2018 · 12 comments

Comments

@JamesCoonce
Copy link

Issue type:

[ ] question
[x ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x ] mysql / mariadb
[ ] oracle
[x ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Placing @BeforeInsert() and @AfterInsert do not work

@AfterInsert()
  modifyText() {
    console.log("Something is being inserted");
  }
@BeforeInsert()
  modifyText() {
    this.text = `${this.text} was modified before insert`;
  }
@BeforeInsert()
hashpassword(){
   this.password = ........
}

I've seen this in other issues and they were closed but the issue was never actually resolved.

@billyjov
Copy link

@JamesCoonce @BeforeInsert work for me using version 0.2.7. You have to be extra careful when working with asynchronous processes e.g your last use case for hashing a password.

It should work with the folowing syntax:

@BeforeInsert
async hashpassword() {
    this.password = await your-hash-process;
}

@rustamwin
Copy link
Contributor

try it in subscribers

@JamesCoonce
Copy link
Author

I'm also using 0.2.7. Here is a basic example. This doesn't even work. It does no modify.

import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert} from 'typeorm';

@Entity()
export class Todo {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ length: 500 })
  text: string;

  @Column()
  complete: boolean;

  @BeforeInsert()
  modifyText() {
    this.text = `${this.text} was modified before insert`;
  }
}

Inside of my todoService I have this

public async create(todo: CreateTodosDTO): Promise<Todo> {
        return await this.todoRepository.save(todo);
    }

@feather-jmalone
Copy link
Contributor

what does the value of todo look like? is it an object that has the properties of Todo (e.g. {id: 1, text: 'text', complete: true}, or is it an actual instance (e.g. const todo = new Todo())?

@JamesCoonce
Copy link
Author

When I send the post request it's

{
  "text": "Today is Monday",
  "complete": false
}

What I get back is

{
	"text": "Today is Monday",
	"complete": false,
	"id": 5
}

I check the database and it's the same. It's not being modified before insert.

@feather-jmalone
Copy link
Contributor

i could be wrong about this, but my understanding is that because BeforeInsert is an instance-level method, you need an actual instance of Todo in order for it to be invoked (you can't reference this.password if there's no this). The object you're passing in the request is just an object that happens to have the same properties as Todo.

@JamesCoonce
Copy link
Author

That works. I need to create an actual instance first.

@gintsgints
Copy link

@JamesCoonce consider closing issue then.

@pilattebe
Copy link

I know why this works that way but it shouldn’t IMO.

@vinimdocarmo
Copy link

That works. I need to create an actual instance first.

How can one call a validation method using repository.save passing just a plain object instead of an entity instance?

@liqwid
Copy link

liqwid commented Oct 11, 2019

That works. I need to create an actual instance first.

How can one call a validation method using repository.save passing just a plain object instead of an entity instance?

Entity.prototype[methodName].apply(object, args)
given entity often is a DTO, that's useful IMO

of course this brings the problem that any other instance methods would be unavailable in the call

another obvious solution is to instantiate.

Anyway it should be explicitly mentioned in docs that instantiation is required to use hooks

@niyodusengaclement
Copy link

You can use the create method to create an instanceconst data = repository.create(yourDto) and then save returned data repository.save(data)

This will solve the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants