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.

Make previous value available in @AfterUpdate function

See original GitHub issue

By popular request, it would be very useful if we could also access the previous state of objects being updated in @AfterUpdate functions

For example:

@Entity()
export class CartItem {
    
    @AfterUpdate()
    notifyCustomer() {
        sendEmail(`An item in your cart has changed price from ??? to ${this.price}`
    }
}

Could become

@Entity()
export class CartItem {
    
    @AfterUpdate()
    notifyCustomer() {
        sendEmail(`An item in your cart has changed price from ${this.previous.price} to ${this.price}`
    }
}

TypeORM already knows the previous value, as it will only call @AfterUpdate if it detects a change, therefore, the feature would only require that to be exposed to developer.

Please see comment and thread - https://github.com/typeorm/typeorm/issues/1609#issuecomment-484303500

Thank you!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:39
  • Comments:14

github_iconTop GitHub Comments

37reactions
Jantje2000commented, Feb 4, 2020

Is there a workarround for this @pleerock ?

I needed this, so I made some code to use this. It’s written in a subscriber, in my case to add timeline and audit trail logging.


    beforeUpdate(event: UpdateEvent<any>): Promise<any> | void {
        event.updatedColumns.forEach((column) => {
            let key: string = column.databaseName;
            let oldValue = event.databaseEntity[column.propertyName];
            let newValue = event.entity[column.propertyName];

            console.log(`${key} was ${oldValue} en is nu ${newValue}`);
        });
    }

This code will loop through the updatedColumns which we know from the event, and will lookup them in the databaseEntity and the events entity.

Maybe it will help someone

9reactions
valjic1commented, May 1, 2021

When using EventSubscriber you can hook to afterLoad event and set the retrieved entity as a class property.

@EventSubscriber()
export class CustomerSubscriber implements EntitySubscriberInterface<Customer> {
  customer: Customer;

  listenTo() {
    return Customer;
  }
  
  afterLoad(entity: Customer) {
    this.customer = entity;
  }

  afterUpdate(event: UpdateEvent<Customer>) {
    console.log(`Previous value: ${this.customer}`);
    console.log(`New value: ${event.entity}`);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Get OLD Value in MySQL Trigger AFTER Update Statement
In an UPDATE TRIGGER , you can use the OLD keyword to access the row data which is being replaced by the update....
Read more >
Trigger.old and Trigger.new values before and after update ...
When a field value is changed to certain value, we use trigger.old and trigger.new to compare the older and new version values of...
Read more >
Form.AfterUpdate event (Access) - Microsoft Learn
The AfterUpdate event is triggered when a control or record is updated. Within a record, changed data in each control is updated when...
Read more >
Make previous value available in @AfterUpdate function
Make previous value available in @AfterUpdate function · Issue type: [ ] question · Database system/driver: Any · TypeORM version: Any · Steps...
Read more >
Checking 'Correct' Previous value method in Trigger design ...
My method of choice when it comes to checking for field changes, is Dan Appleman's 'Correct' old Value method found in Chapter 6...
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