{"id":7888,"date":"2015-11-05T07:53:23","date_gmt":"2015-11-05T07:53:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/05\/sql-query-in-sequelize-getter-method-open-source-projects-sequelize-sequelize\/"},"modified":"2015-11-05T07:53:23","modified_gmt":"2015-11-05T07:53:23","slug":"sql-query-in-sequelize-getter-method-open-source-projects-sequelize-sequelize","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/05\/sql-query-in-sequelize-getter-method-open-source-projects-sequelize-sequelize\/","title":{"rendered":"SQL Query in Sequelize getter method-open source projects sequelize\/sequelize"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/Mnunr.jpg?s=128&amp;g=1\" \/> <strong>Naqamel<\/strong><\/p>\n<p>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.<\/p>\n<p>Add whatever parameters and types to your stored proc as you need, and the result will look something like this:<\/p>\n<pre><code>CREATE FUNCTION GetEarthDistance (v_Foo bigint) RETURNS type AS $$\nDECLARE \n   v_Name varchar(256);\nBEGIN\nSELECT name INTO v_Name, \n   earth_distance(ll_to_earth( 51.5241182, -0.0758046 ), \n   ll_to_earth(latitude, longitude)) as distance_from_current_location \nFROM Branches \nWHERE somecol &gt; v_foo\nORDER BY distance_from_current_location ASC;\n\nRETURN v_Name; \nEND;\n$$ LANGUAGE 'plpgsql';\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7888","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7888"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7888\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}