What is the best / correct way to call a postgres function or stored procedure from nodejs

I am using the "pg" module to handle postgresql db, how to call a function using pg I doubt

I call the function using the request method,

client.query("SELECT * FROM SQSP_IsUserNameExists($1)",[userName], function(err, result) { // some code. }); 

which works fine, but this is the correct way to call postgresql functions.

+6
source share
2 answers

Your code looks correct.

But if you need a stronger syntax, in the same example using pg-promise :

 db.func('SQSP_IsUserNameExists', userName) .then(data => { // data as returned from the function }) .catch(error => { // error }); 
+4
source

On the PostgrSQL side, if a function returns a result set than yes, the SQL syntax is correct. Regarding the syntax of calling Node, I am not familiar with this structure. But if its results return, I say that the task is completed.

0
source

Source: https://habr.com/ru/post/985347/


All Articles