Search code examples
node.jsloopbackjs

node loopback updateAll update property with another property value


I'm trying to call a node loopback updateAll method where one property receives the value of another property. How I do this?

Model.updateAll(
     {
       Status: 'Cancel',
      },
      {
        Qty: 0
        QtyCancelled: Qty   <------------- How I should write this line?
      },
     function(err, info) {
      console.log('result',info);
     }
  );

}

thanks in advance,


Solution

  • LoopBack does not supports updates where one property receives a value read from the database and/or other properties in the update command.

    If you are using an SQL database, then you can run an SQL query directly.

    Model.dataSource.connector.execute(
      'UPDATE Model SET Qty=?, QtyCancelled=Qty WHERE Status = "Cancel"',
      [0],
      function (err, info) {
        // ...
      });
    

    See https://loopback.io/doc/en/lb3/Executing-native-SQL.html

    Many of no-SQL connectors support execute API too, please check the documentation of the connector you are using.