关于node.js:Sequelize-使用MariaDB在where子句中进行地理位置定位

Squelize - geolocation in where clauses using MariaDB

我有一个使用MariaDB的node.js应用程序。
到目前为止,我所有的SQL都在存储过程中。

我正在考虑Sequelize,我在那里唯一没有发现的-我需要的-在where子句中使用函数。

我目前的查询中有这样的东西:

1
2
Select * from places p
where ST_WITHIN(p.geolocation, ST_BUFFER(GeomFromText(in_geolocation), radius)) = 1

(in_gelocation和radius是SP参数)。

无论如何,在Sequelize或另一个ORM中是否可以做到这一点?

谢谢


尝试使用Sequelize.fn

Creates a object representing a database function. This can be used in
search queries, both in where and order parts, and as default values
in column definitions.

http://docs.sequelizejs.com/en/latest/api/sequelize/#fn-fn-args-sequelizefn


这是一个Sequelize地理位置示例,供您参考。 它利用SQL函数并说明了where属性的包含。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
if(!query.includes(',') && lat && lng) {
    attributes.push([
        DataTypes.fn('concat',
        DataTypes.col('city'),
        DataTypes.col('state')
    ),'city_state'])
    attributes.push([
        DataTypes.fn('ST_Distance',
        DataTypes.col('location'),
        DataTypes.literal(`ST_GeomFromText('POINT(${lat} ${lng})', 4326)`)
    ), 'distance'])
}
if(!query.includes(',')) {
    attributes.push([DataTypes.literal(`CASE
        WHEN
            name = '` + query + `' THEN 4
        WHEN
            city LIKE '%` + query + `%' THEN 3
        WHEN
            name LIKE '%` + query + `%' THEN 2
        WHEN
            name LIKE '` + query + `%' THEN 1
    END`), 'exact_like_match'])
}
var whereAnd = []
whereAnd.push(
    DataTypes.literal('MATCH(name, city, state, zip) AGAINST(:search_query)')
)
whereAnd.push({
    menu_enabled: 'Y'
})
var search_params = {  
    where: whereAnd,
    replacements: {
        search_query: '+' + query + '*',
        type: DataTypes.QueryTypes.SELECT
    },
    attributes: attributes,
    offset: parseInt(req.query.start),
    limit: req.query.limit ? parseInt(req.query.limit) : 12
}
search_params['order'] = [
    DataTypes.literal('exact_like_match DESC')
]
if(!query.includes(',')) {
    // console.log('comma detected!')
    search_params['order'] = [
        DataTypes.literal('exact_like_match DESC, distance ASC')
    ]
}
dispensary.findAndCountAll(search_params).then(disp_rx => {

})