insert or update(upsert) in loopback application
<p>I developed an API using Loopback framework, in that i have to <code>insert or update</code> to a table.</p>

<p>My table looks like below:</p>

<pre><code>userid  bbid saved  id(PK)
  1      5    1000   1
</code></pre>

<p>So when next time if(bbid = 5) it should update the above row, if bbid =5 is not there it should insert into the above table.</p>

<p>I tried the following:</p>

<pre><code>app.models.modelname.upsert({
  bbid: bb.id,
  saved: Saved,
  userid: 1
}, function(err, res) {}); 
</code></pre>

<p>EDIT for COMPOSITE ID:</p>

<pre><code>app.models.modelname.upsert({
  bbid: vvvv.bbid,
  userid: 1,
  saved: amountSaved
}, function(err, res) {});   
</code></pre>

<p>Also gone through Loopback doc it says </p>

<pre><code>    upsert - checks if the instance (record) exists, based on the designated 
ID property, which must have a unique value; if the instance already exists, 
the method updates that instance.  Otherwise, it inserts a new instance.
</code></pre>

<p>But in my case it should check for <code>bbid not id</code></p>

<p>But its inserting everytime. Please share your ideas. Thanks in advance</p>

<p>EDIT FOR UPSERT:</p>

<p>My process is as follows:</p>

<pre><code>Fetch from table1 and loop that response and calculate the saved and upsert it into another table(table2 (this is where upsert should happen)). Also the fetching from table1 will happen frequently so suppose consider if is happens 2nd time it should update the already present bbid..
</code></pre>