title: Upsert in Postgres using node.js
body: <p>I'm trying to do an insert or update in a postgres database using node.js with pg extension (version 0.5.4).</p>
  
  <p>So far I have this code:
  (...)</p>
  
  <pre><code>client.query({
              text: "update users set is_active = 0, ip = $1 where id=$2",
              values: [ip,id]
          }, function(u_err, u_result){
              debug(socket_id,"update query result: ",u_result);
                  debug(socket_id,"update query error: ",u_err);
  
                      date_now = new Date();
              var month = date_now.getMonth() + 1;
  
              if(!u_err){
  
                  client.query({
                      text: 'insert into users (id,first_name,last_name,is_active,ip,date_joined) values' +
                      '($1,$2,$3,$4,$5,$6)',
                      values: [
                              result.id, 
                              result.first_name,
                              result.last_name,
                              1,
                              ip,
                              date_now.getFullYear() + "-" + month + "-" + date_now.getDate() + " " + date_now.getHours() + ":" + date_now.getMinutes() + ":" + date_now.getSeconds()
                              ]
                  }, function(i_err, i_result){
                      debug(socket_id,"insert query result: ",i_result);
                      debug(socket_id,"insert query error: ",i_err);
                  });
              }
          });
  </code></pre>
  
  <p>The problem is that, although both queries work the problem is always running both instead of only running the insert function if the update fails.</p>
  
  <p>The debug functions in code output something like:</p>
  
  <p><strong>UPDATE</strong></p>
  
  <pre><code>Object { type="update query result: ", debug_value={...}}
  home (linha 56)
  Object { type="update query error: ", debug_value=null}
  home (linha 56)
  Object { type="insert query result: "}
  home (linha 56)
  Object { type="insert query error: ", debug_value={...}}
  </code></pre>
  
  <p><strong>Insert</strong></p>
  
  <pre><code>Object { type="update query result: ", debug_value={...}}
  home (linha 56)
  Object { type="update query error: ", debug_value=null}
  home (linha 56)
  Object { type="insert query result: ", debug_value={...}}
  home (linha 56)
  Object { type="insert query error: ", debug_value=null}
  </code></pre>
  
  <p>** EDIT **</p>
  
  <p>ANSWER FROM node-postgres developer:</p>
  
  <blockquote>
    <p>It's possible to retrieve number of rows affected by an insert and
    update.  It's not fully implemented in the native bindings, but does
    work in the pure javascript version.  I'll work on this within the
    next week or two.  In the mean time use pure javascript version and
    have a look here:</p>
    
    <p><a href="https://github.com/brianc/node-postgres/blob/master/test/integration/client/result-metadata-tests.js" rel="noreferrer">https://github.com/brianc/node-postgres/blob/master/test/integration/client/result-metadata-tests.js</a></p>
  </blockquote>
  
  <p>** END EDIT **</p>
  
  <p>Can anyone help?</p>