I've 3 models: A, B, and its pivot, AsBs. I'm trying to get all A/B entities (querying its respective repositories) where pivot's boolToCheck is true. I've been able to filter through each other model's fields (ex: A where B.bField is whatever), but not through a pivot field.
How could I do it? Thanks!
Model A
module.exports = (sequelize, DataTypes) => {
const A = sequelize.define(
"a",
{
aField: {
allowNull: false,
type: DataTypes.STRING,
},
},
{}
);
A.associate = function (models) {
A.belongsToMany(models.B, {
through: "asBs",
onDelete: "RESTRICT",
onUpdate: "CASCADE",
});
};
return A;
};
Model B
module.exports = (sequelize, DataTypes) => {
const B = sequelize.define(
"b",
{
bField: {
allowNull: false,
type: DataTypes.STRING,
},
},
{}
);
B.associate = function (models) {
B.belongsToMany(models.A, {
through: "asBs",
onDelete: "RESTRICT",
onUpdate: "CASCADE",
});
};
return B;
};
PIVOT AB
module.exports = (sequelize, DataTypes) => {
const AsBs = sequelize.define(
"asBs",
{
aId: {
allowNull: false,
type: DataTypes.NUMBER,
},
bId: {
allowNull: false,
type: DataTypes.NUMBER,
},
boolToCheck: {
allowNull: false,
type: DataTypes.NUMBER,
},
},
{}
);
AsBs.associate = function (models) {
AsBs.belongsTo(models.A, {
onDelete: "RESTRICT",
onUpdate: "CASCADE",
});
AsBs.belongsTo(models.B, {
onDelete: "RESTRICT",
onUpdate: "CASCADE",
});
};
return AsBs;
};