node.js - Node-Postgres SELECT WHERE IN dynamic query optimization -


we're working on node/express web app postgres database, using node-postgres package. followed instructions in this question, , have our query working written way:

exports.getbyfilenameandcolname = function query(data, cb) {    const values = data.columns.map(function map(item, index) {     return '$' + (index + 2);   });    const params = [];   params.push(data.filename);   data.columns.foreach(function iterate(element) {     params.push(element);   });    db.query('select * columns ' +     'inner join files on columns.files_id = files.fid ' +     'where files.file_name = $1 , columns.col_name in (' + values.join(', ') + ')',     params, cb   );  }; 

data object containing string filename , array of column names columns. want query extract information our 'columns' , 'files' tables dynamic number of columns. db.query takes parameters (query, args, cb), query sql query, args array of parameters pass query, , cb callback function executed database results.

so code written in way returns correct data, (we think) it's ugly. we've tried different ways of passing parameters query, format has returned data.

is there cleaner/simpler way pass in our parameters? (e.g. way pass parameters in way node-postgres accept without having create additional array array + non-array elements.)

asking because:

  1. perhaps there's better way use node-postgres package/we're using incorrectly, ,
  2. if correct way solve type of issue, code supplements answer in question referenced above.

hello tried translate "but (we think) it's ugly" believe response answers question. in same question reference find this response

in user takes pg-promise special-case variable formatting

in case may using shared connection in example recommend using plain db.query im using shared connection show how extended "ugly":

exports.getbyfilenameandcolname = function query(data,cb) {   var sco;    const params = [];   params.push(data.filename);   data.columns.foreach(function iterate(element) {     params.push(element);   });   db.connect()   .then(function(obj){     sco=obj;     return sco.query('select * columns ' +       'inner join files on columns.files_id = files.fid ' +       'where files.file_name = $1 , columns.col_name in ($2^)',     pgp.as.csv(params)));   },function(reason){     console.log(reason);   })   .done(function(){     if(sco){         sco.done();         cb();     }   });  }; 

now again i'm not sure meant ugly in use case return format this:

{   column:[          {           id: data,           data: data,           col_name: data,           files_id: data,           fid: data,           files_name: data          },...    ] } 

and in case wanted this:

{       column:[               {               id: data,               data: data,               col_name: data,               files_id: data,               },...       ],       file:[             {              fid: data,              files_name: data             },...       ]      } 

so in order took same shared connection , added variable manage results. may not answer question or might on suggest looking pg-promises helpful advance queries , formatting.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -