SQL Query in Sequelize getter method-open source projects sequelize/sequelize

Naqamel

Your best bet is probably to wrap the query in a stored procedure and pass in the arguments you want to use in the where clause. As stored procedures are compiled, this will perform better than a Dynamic SQL where you generate the WHERE clause on the fly.

Add whatever parameters and types to your stored proc as you need, and the result will look something like this:

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';