title: Ways to do a dynamic query in node.js
body: <p>I'm trying to figure out how to do dynamic queries in node.js with node-mysql. Which table and fields to insert and update is based on users' request from the <code>data</code> object. I thought of creating a module to store different queries like the following but no luck. It gave me this error:</p>
  
  <pre><code>TypeError: Object function (){
     var likes = 'INSERT IGNORE INTO item (id,like_type) VALUES(?,?)';
     return likes;
   } has no method 'replace'
  </code></pre>
  
  <p>app.js</p>
  
  <pre><code> var queries = require('./queries');
   .....
  
   socket.on("like",function(data){ 
    var table_file = data[0].table;
    var d = data[1];  
    connection.query(queries[table_file]
      ,[d.id,d.like_type],function(err,rows,fields){ 
       if(err){ throw err;}
       console.log(rows);
      });
  </code></pre>
  
  <p>queries.js:</p>
  
  <pre><code>module.exports = {
   item_like:function(){
     var likes = 'INSERT IGNORE INTO item (id,like_type) VALUES(?,?)';
     return likes;
   },
   people:function(){
     var likes = 'INSERT IGNORE INTO people (id,like_type) VALUES(?,?)';
     return likes;
   }
  }
  </code></pre>
  
  <p>Here's the data object sent to socket:</p>
  
  <pre><code>data object: {table:item_like},{id:1,like_type:1};
  </code></pre>