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

194
Vistas
How to extend sequelize class methods?

Is it possible to extend my sequelize model to have methods other than findAll or findOne.

I want to have something like:

class Transaction extends Model {
  
  static associate(models) {
    this.belongsTo(models.StripePayment, {
      foreignKey: 'stripePaymentId',
      as: 'stripePayment',
    });
  }
    
  async failedTransactions({
    billId
  }) {
    return this.findAll({
      where: {
        billId
      },
      include: [
        {
          model: StripePayment,
          required: false,
        },
      ],
    });
  },
  
  // ...
}

Where I can then call

const failedTransactions = await Transaction.failedTransaction({ billId }) // currently throws Transaction.failedTransaction is not a function

Is seems to recognise it as a function if I define the method as static inside the model

static async failedTransactions({
    billId
  }) {
    ...
  }

However, the associations don't seem to work inside a static method. I am guessing this is because the method gets defined before my model links it's associations.*

  • associations work when I call the findAll query in my controller (i.e. not in the model)
about 4 years ago · Juan Pablo Isaza
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