简体   繁体   中英

Sequelize query broke down

I revisited a query I wrote a couple days ago and, as it should, it miraculously broke down.
It mostly consists of this:

...
        let filter = req.body.filter;
        let filter_value = req.body.filter_value;       
        let filter_from = req.body.filter_from;
        let filter_to = req.body.filter_to;

        let filterSystem = (filter === "createdAt") ?
                {[filter]:{
                    [Op.or]:[
                        filter_value,
                        {[Op.between]:[filter_from,filter_to]},
                        {[Op.between]:[filter_from,filter_value]},
                        {[Op.between]:[filter_value,filter_to]}
                    ]
                }} 
            : (filter === "favorites") ? 
                {[filter]:filter_value} 
            : {};

        let results = await Promise.all([ 
            await models.bookmark.findAll({
                attributes:[
                    'guid',
                    'link',
                    'createdAt',
                    'description',
                    'favorites'
                ],
                ...
                where:filterSystem
            })              
        ])

        res.json({ 
            length: results[0].length,
            data: results[0]
        })
    ...

Using Postman to input data to the body, I can see the query go through in the console:

If I input "filter_from" and "filter_value" dates and leave out everything but the [filter_from,filter_value] block -

Executing (default): SELECT `guid`, `link`, `createdAt`, `description`, `favorites` FROM `bookmarks` AS `bookmark` WHERE `bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND '2020-04-30T15:37:00.170Z' ORDER BY `bookmark`.`createdAt` ASC LIMIT 0, 50;

All it returns is an empty array.

If all of the search options are present -

SELECT `guid`, `link`, `createdAt`, `description`, `favorites` FROM `bookmarks` AS `bookmark` WHERE (`bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND NULL OR `bookmark`.`createdAt` BETWEEN '2020-04-30T15:34:48.877Z' AND '2020-04-30T15:37:00.170Z' OR `bookmark`.`createdAt` BETWEEN '2020-04-30T15:37:00.170Z' AND NULL) ORDER BY `bookmark`.`createdAt` ASC LIMIT 0, 50;

It only returns the result matching filter_value, though there should be around 6.

Searching without any filters returns all the 8 results, using "favorites" and "true" for the "filter" and "filter_value" respectively works just fine.

When making the query, I was following this Query by Date Range for a column field in Sequelize
What could go wrong - ISO 8601 got deprecated overnight? But that's the format the base stores all the dates in. Getting no warnings/errors in the console either.

After some tinkering, I dropped [Op.between] altogether and switched to

        where:{
            [filter]:{
                [Op.and]:[
                    {[Op.gt]:filter_from},
                    {[Op.lt]:filter_to}
                ]
            }
        }

And now it's working.
Just leaving this here, in case someone gets a similar issue.
In the end, I needed objects for comparison, and it seems Op.between doesn't have support for them - but Op.and/or sure do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM