Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
Typeorm listeners with parameters

Is it possible to use TypeORM listeners with parameters?

Example:

@BeforeInsert()
public async doSomething(myImportantParam) {
    // using myImportantParam here
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

As in @BeforeInsert documentation, it does not allow you to add any parameters to the listener.

However, I believe you can create a constructor for your entity that takes myImportantParam as an argument and you can use this.myImportantParam to access the value within the listener.

Below here is an example code (assume myImportantParam is a string).

Entity class:

@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 ...*/
}

Service Class:

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 ...*/
}

Hope this helps. Cheers 🍻 !!!

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda