Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback "params" for running native SQL queries

Anybody know what "params" is/are for the Loopback docs here:

https://docs.strongloop.com/display/public/LB/Executing+native+SQL

it says:

Executing native SQL

To execute SQL directly against your data-connected model, use the following:

dataSource.connector.execute(sql, params, cb); 

or

dataSource.connector.query(sql, params, cb); // For 1.x connectors

Where: sql - The SQL string. params - parameters to the SQL statement. cb - callback function

like image 783
Alexander Mills Avatar asked Dec 25 '22 00:12

Alexander Mills


1 Answers

That is an array of values of your SQL string params. For example if you have postgresql database and parametrized query like this:

select * from table where id = $1 or name = $2

then you have to provide parameter values to your function, so you will do something like this:

var query = "select * from table where id = $1 or name = $2";
var params = [82, "My name"];
ds.connector.execute(query, params, function(err, data){
  if(err){
    console.log( err);
  }else{
    console.log(data);
  }
});
like image 124
A.Z. Avatar answered Jan 13 '23 01:01

A.Z.