-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunsql.js
31 lines (27 loc) · 1018 Bytes
/
runsql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* gifting this antipattern to the future
use this to bypass the loopback2 orm allowing one to run things like distinct and group queries
pass in any model that's backed by an sql datasource and a query
use this like:
const foo = await lb2-runsql.run( <your_lb_model_here>, "select 1" ) .catch (err =>{ debug("runsql", err )}) ;
will return an array of rows with cooked keys eg [ {"colname": "colvalue"}....}
XXXX this is a terrible idea; don't use it
*/
exports.run = function (model, sqlquery ) {
return new Promise ( async function ( resolve, reject) {
//experience the calm rational sytanx of async
try {
const connec = model.dataSource.connector;
connec.execute ( sqlquery, [], (err, res) => {
if ( err) {
console.error(err, err.stack, sqlquery);
reject( res );
}
else {
resolve( res );
}
});
} catch (err) {
console.error(err, err.stack, sqlquery);
}
});
};