Dealing with some difficulties regarding the Sequelize JS.
I'm attempting to incorporate the following query into a Sequelize scope.
SELECT *
FROM Projects
WHERE isDeleted IS NOT true
AND (
( importSource IS NOT null ) AND ( createdAt BETWEEN '2018-01-19' AND '2018-01-29')
OR
( importSource IS null ) AND ( createdAt > '2018-01-19' )
)
Currently trying this in my model without success.
scopes: {
active: {
where: {
isDeleted: {
[Op.not]: true
},
[Op.or]: [
{
importSource: { [Op.ne]: null },
createdAt: {
[Op.between]: [
new Date(new Date() - 30 * ( 24 * 60 * 60 * 1000)),
new Date(new Date() - 4 * (24 * 60 * 60 * 1000))
],
}
},
{
importSource: { [Op.eq]: null },
createdAt: {
[Op.gt]: new Date(new Date() - 30 * ( 24 * 60 * 60 * 1000)),
}
}
]
}
}
}
No error is shown, just the MySQL code being executed like below:
Executing (default): SELECT `id`, `url`, `title`, `company`, `importSource`, `importSourceId`, `publishedAt`, `createdAt`, `updatedAt` FROM `Projects` AS `Project` WHERE `Project`.`isDeleted` IS NOT true ORDER BY `Project`.`id` DESC LIMIT 5;
Here is the controller method calling the model and scope.
exports.index = (req, res) => {
const MAX = 50;
var numberProjects = ((req.query.number) ? req.query.number : MAX);
numberProjects = ((numberProjects > MAX) ? MAX : numberProjects);
Project.scope('active').findAll({
order: [
['id', 'DESC']
],
limit: +numberProjects
})
.then( projects => {
res.send( projects );
})
};
Any assistance would be greatly appreciated.