I would like to know if there is a better way to obtain the following result: filtering a list of rows with values from table that's connected through a many-to-many relationship. In my example case, it's songs that feature certain instruments.
Here is my songs model:
module.exports = function(sequelize, DataTypes) {
var Song = sequelize.define('songs', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING(50),
allowNull: false
},
}, {
tableName: 'songs',
timestamps: false
});
Song.associate = function (models) {
Song.belongsToMany(models.instruments, {through: 'songs_instruments', foreignKey: 'song_id'})
};
return Song
};
Here is my instruments model:
module.exports = function(sequelize, DataTypes) {
var Instrument = sequelize.define('instruments', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(50),
allowNull: false
}
}, {
tableName: 'instruments',
timestamps: false,
});
Instrument.associate = function (models) {
Instrument.belongsToMany(models.songs, {through: 'songs_instruments', foreignKey: 'instrument_id'})
};
return Instrument
};
Here is the model for the join table:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('songs_instruments', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
song_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
model: 'songs',
key: 'id'
}
},
instrument_id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
model: 'instruments',
key: 'id'
}
}
}, {
tableName: 'songs_instruments',
timestamps: false
});
};
And here is the way I am currently doing my fetching (args['instruments']
contains a list of the instruments I want to filter the songs for):
const instruments = await db.instruments.findAll({
where: {name: args['instruments']}
})
const songs_instruments = await db.songs_instruments.findAll({
where: {instrument_id: instruments.map(instrument => instrument.id)}
})
const songs = await db.songs.findAll({
where: {id: songs_instruments.map(song => song.song_id)}
})
return songs
While this works correctly, and in the end I get the results that I expect, I can't help but wonder if there is a more efficient way of querying the database, as doing it like this causes a total of three queries to be executed:
Executing (default): SELECT `id`, `name` FROM `instruments` AS `instruments` WHERE `instruments`.`name` IN ('Vocals', 'Trumpet', 'Guitar');
Executing (default): SELECT `id`, `song_id`, `instrument_id` FROM `songs_instruments` AS `songs_instruments` WHERE `songs_instruments`.`instrument_id` IN (1, 3, 7);
Executing (default): SELECT `id`, `title` FROM `songs` AS `songs` WHERE `songs`.`id` IN (1, 1, 2, 2, 3, 3);
I am quite new to Sequelize, hence my probably incorrect/suboptimal way of achieving my goal.
Oh i totally missed that.. it was late.. yes you can do that.. add the as like below to alias this connection, and do the same in songs with as: 'instruments'... you don't need to fetch the join records..
Instrument.associate = function (models) {
Instrument.belongsToMany(models.songs, {through: 'songs_instruments', as: 'songs', foreignKey: 'instrument_id', otherKey: 'song_id'})
};
const instruments = await db.instruments.findAll({
where: {name: args['instruments']},
{
include: [{
model: db.songs,
as: 'songs',
through: db.songs_instruments,
}]
}
});
OR Something along the lines of the include below...
const songs = await db.songs.findAll({
include: [{
model: db.instruments,
as: 'instruments',
through: db.songs_instruments,
where: {name: args['instruments']}
}],
});