Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda