question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@BeforeUpdate Not called when using Model.update(id, args)

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Hi. I’ve got a function with the @BeforeUpdate decorator that is not being called when I use Model.update(id, args)

My model is extending from BaseEntity

Is this expected? On the listeners-subscribers documentation it says that @BeforeUpdate gets called on .save(), shouldn’t it be also called on .update()?

Repo: https://github.com/serranoarevalo/nuber/tree/33d2e729a36c8fb08d721a5453e5c2d03d24e8d4

Example of the GraphQL Resolver:

updateUser: async (parent, args, { entities: { User } }) => {
      try {
        await User.update(args.id, args);
        return true;
      } catch (error) {
        console.log(error);
        return false;
      }
    }

Example of the model

@Entity()
class User extends BaseEntity {
...
@BeforeUpdate()
  async updatePassword(): Promise<void> {
    if (this.password) {
      const hashedPassword = await this.hashPassword(this.password);
      this.password = hashedPassword;
    }
  }
}

Anyways thanks for the great work and for looking into this!

N.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
leognmottacommented, Jul 24, 2020
 Object.keys(entity).forEach(key => {
    e[key] = entity[key];
  });

To make it easier there is Object.assign

public async update(dto: UpdateDTO): Promise<Entity> {
    const entity = await this.repository.findOne(dto.id);

    return await this.repository.save(Object.assign(entity, dto));
}
10reactions
danielprrazevedocommented, Feb 6, 2020

In the documentation it says that updates must be made directly to the model https://github.com/typeorm/typeorm/blob/master/docs/listeners-and-subscribers.md#beforeupdate

const user = this.userRepository.findOne(id);
user.password = '123456';
await this.userRepository.save(user);

alternatively, I used the following (T = User)

async update(id: number | string, entity: Partial<T>) {
  const e = await this.repository.findOne(id);
  Object.keys(entity).forEach(key => {
    e[key] = entity[key];
  });
  return await this.repository.save(e);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM / NestJS - @BeforeUpdate hook not working
I'm trying to update the user entity password, by passing a simple string and calling the save method to trigger the beforeUpdate hook...
Read more >
Before Update not working in a Trigger framework
I'm trying to override the attachment name with parent record name. I have a similar code in before Insert which works. But the...
Read more >
Vincit/objection.js - Gitter
On $beforeUpdate , when mutating the queryContext object, that object is not passed to the final queries making it impossible to pass any...
Read more >
React.Component
componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render. Use this as an opportunity to operate...
Read more >
Form.BeforeUpdate event (Access) - Microsoft Learn
Changing data in a control by using Visual Basic or a macro ... For forms, you can use the BeforeUpdate event to cancel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found