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.

@AfterInsert() / @BeforeInsert() can only be invoked once per server re-start

See original GitHub issue

Issue type:

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

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] 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:

I’m testing this against my local mysql@5.7 db as well as serverless aurora via typeorm-aurora-data-api-driver (for what thats worth).

I’m also running this code via serverless-offline

The before/after-insert hooks appear to only run if they are the very first invocation made by the DB runner - all subsequent actions work correctly, selecting / creating / updating / deleting - but they skip these hooks.

Also I’ve tested this via both ActiveRecord and Data Mapper approaches and the result appears to be the same.

example code

@Entity()
export class MyClass extends BaseEntity {
  @PrimaryGeneratedColumn("uuid")
  @Generated("uuid")
  id: string;

  @Column()
  name: string;

  @AfterInsert()
  doSomething() {
    console.log("this only runs on the first invocation", this);
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
592dacommented, Jan 4, 2020

hope it will help you as well, when running serverless offline with

serverless offline --skipCacheInvalidation

solves the problem locally, not sure about production yet.

0reactions
filipw01commented, Dec 9, 2020

Please note, that in new version of serverless-offline --skipCacheInvalidation does not exist use --allowCache instead

Read more comments on GitHub >

github_iconTop Results From Across the Web

BeforeInsert and AfterInsert hook not being called
According to TypeORM's Docs the listener hooks get called before or after their respective operation when using the entity manager or ...
Read more >
When to use before insert, before update, after insert and after ...
Basically before triggers are executed before the data is commited to the database . You can change the field value in before trigger...
Read more >
execution of AfterInsert caused by: System.FinalException ...
Here you are trying to update the trigge.new instance which is only possible in before trigger not in after trigger.
Read more >
Entity Listeners and Subscribers - typeorm - GitBook
You can define a method with any name in entity and mark it with @AfterInsert and TypeORM will call it after the entity...
Read more >
Trigger Overview - MariaDB Knowledge Base
a table with which it is associated ( animals ); a set of statements to run (here, just the one UPDATE statement). AFTER...
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