Im currently working on a budget tracking web app that has the corresponding database setup
So basically i have multiple transactions reladed to a single account, as well as multiple accounts related to a single user
i want to come up with a sequelize query that allows me to view every transaction done by any account corresponding to a single user
Assuming that you have associations look like this:
// these definitions are simplified for demonstration purposes
User.hasMany(Account);
Account.hasMany(Transaction);
Account.belongsTo(User);
Transaction.belongsTo(Account);
We can get all user's transactions like this:
const transactions = await Transaction.findAll({
include: [{
model: Account,
required: true,
include: [{
model: User,
required: true,
where: {
id: userId // here is the condition on a certain user id
}
}]
}]
})