¿Es posible usar oyentes TypeORM con parámetros?
Ejemplo:
@BeforeInsert() public async doSomething(myImportantParam) { // using myImportantParam here }Como en la documentación de @BeforeInsert , no le permite agregar ningún parámetro al oyente.
Sin embargo, creo que puede crear un constructor para su entidad que tome myImportantParam como argumento y puede usar this.myImportantParam para acceder al valor dentro del oyente.
A continuación se muestra un código de ejemplo (supongamos que myImportantParam es una cadena).
@Entity() export class Post { constructor(myImportantParam: string) { this.myImportantParam = myImportantParam; } myImportantParam: string; @BeforeInsert() updateDates() { // Now you can use `this.myImportantParam` to access your value foo(this.myImportantParam); } /*... more code here ...*/ } export class PostService { async addNewPost() { const myImportantParam = 'This is what I need!'; const post = new Post(myImportantParam); // Add any other properties the post might have /*... more code here ...*/ // Insert the updated post await getRepository(Post).insert(post); } /*... more code here ...*/ }Espero que esto ayude. Salud 🍻 !!!