Node.js Sequelize getter方法中的SQL查询

Node.js Sequelize getter方法中的SQL查询,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我正在使用Postgres扩展名“earthdistance”进行横向/纵向距离计算 我还使用Sequelize访问数据库,我想定义一个getter 根据与一组坐标的距离进行计算和排序的方法 以下查询工作正常: SELECT name, earth_distance(ll_to_earth( 51.5241182, -0.0758046 ), ll_to_earth(latitude, longitude)) as distance_from_current_lo

我正在使用Postgres扩展名“earthdistance”进行横向/纵向距离计算

我还使用Sequelize访问数据库,我想定义一个getter 根据与一组坐标的距离进行计算和排序的方法

以下查询工作正常:

SELECT name, 
       earth_distance(ll_to_earth( 51.5241182, -0.0758046 ), 
       ll_to_earth(latitude, longitude)) as distance_from_current_location 
FROM "Branches" 
ORDER BY distance_from_current_location ASC;
我可以使用sequelize.query()来使用它,但我希望将所有模型查询都保留为模型的一部分

如何在模型定义中指定getter方法中的条件在何处


谢谢

最好的办法可能是将查询包装在存储过程中,并在where子句中传递要使用的参数。在编译存储过程时,这将比动态SQL执行得更好,动态SQL会动态生成where子句

根据需要向存储的进程添加任何参数和类型,结果如下所示:

CREATE FUNCTION GetEarthDistance (v_Foo bigint) RETURNS type AS $$
DECLARE 
   v_Name varchar(256);
BEGIN
SELECT name INTO v_Name, 
   earth_distance(ll_to_earth( 51.5241182, -0.0758046 ), 
   ll_to_earth(latitude, longitude)) as distance_from_current_location 
FROM Branches 
WHERE somecol > v_foo
ORDER BY distance_from_current_location ASC;

RETURN v_Name; 
END;
$$ LANGUAGE 'plpgsql';

我想在postgres中使用sequelize做同样的事情。你最后做了什么?