Answer the question
In order to leave comments, you need to log in
How to make the right request on Sequelize?
Tell me how to make such a request on Sequelize:
(in brackets the names of models)
There is a Users (Users)
model There is a Colors
model There is a Statuses model
There is a Reviews model.
Each review can have multiple statuses, the relationship is models.Reviews.belongsToMany(models.Statuses, {through: 'reviews_statuses'});
There is a ColorsUserReviews model that stores data when one user marks a review with any of the colors
models[ColorsUserReviews].belongsTo(models.Users, {onDelete: 'cascade'});
models[ColorsUserReviews].belongsTo(models.Reviews, {onDelete: 'cascade'});
models[ColorsUserReviews].belongsTo(models.Colors, {onDelete: 'cascade'
You need to get a list of reviews by filter.
There is data for the filter:
Statuses: [1,3,5] - statuses with identifiers 1, 3 and 5
Description: "text" - text contained in Reviews.description
Colors: [2,4] - colors with identifiers 2 and 4,
User: 1 - user identifier
The result of the selection is all reviews that have at least one of the listed statuses,
then the found reviews are filtered by the content of Reviews.description (must contain the text passed)
then these reviews should be filtered by the listed colors. The user and the list of colors are taken, based on the data in ColorsUserReviews, we filter the received reviews
Need another pick. The data is the same, search for reviews by the listed statuses PLUS all reviews that do not have
DB statuses at all - mysql
the size of the mysql query string should not depend on the number of records in the DB
Answer the question
In order to leave comments, you need to log in
If I understand correctly, then something like this:
/*
Есть модель Пользователи (Users)
Есть модель Цвета (Colors)
Есть модель Статусы (Statuses)
Есть модель Отзывы (Reviews).
Каждый отзыв может иметь несколько статусов, связь - models.Reviews.belongsToMany(models.Statuses, {through: 'reviews_statuses'});
*/
var statuses = [1,3,5]; // - статусы с идентификатором 1, 3 и 5
var description = "text"; // - текст, которые содержится в Reviews.description
var colors = [2,4]; // - цвета с идентификаторами 2 и 4,
var user = 1; // - идентификатор пользователя
Reviews.findAll({
where: {
description: {
$like: description
}
},
include: [{
model: Statuses,
attributes: ['id'],
where: {
reviews_statuses: {
$in: statuses
}
}
},
{
model: Colors,
attributes: ['id'],
where: {
color_id: {
$in: colors
}
}
},
{
model: Users,
attributes: ['id'],
where: {
id: user
}
}]
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question