简体   繁体   中英

Sequelize where condition as raw

I have a model defined. I would like to do an advanced select query in postgres on that model.

The raw query would look like this:

SELECT
    6371 * acos(
        cos(
            radians("+CustomerLat+")
        ) * cos(radians(sto.lat)) * cos(
            radians(sto.lng) - radians("+CustomerLng+")
        ) + sin(
            radians("+CustomerLat+")
        ) * sin(radians(sto.lat))
    ) AS distance
FROM
    stores sto
ORDER BY
    distance ASC
LIMIT 1

Could I do that with sequelize directly on the model without doing this in raw?

Stores
.findAll()
.then(function(res){ //****// })

This question is old but, There are two things I would suggest... Firstly why don't you use PostGIS a postgresql extension which is superb and fast when it comes to GIS data. Secondly.. No! you can't do that... But for advanced where clause conditions I would suggest mixing things up with Knex.js A raw query builder. You can use Knex.js to build your where clause and mix things up with PostGIS to get the best results.

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