Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!