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.*
findAll query in my controller (i.e. not in the model)