Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize - update record, and return result

I am using sequelize with MySQL. For example if I do:

models.People.update({OwnerId: peopleInfo.newuser},         {where: {id: peopleInfo.scenario.id}})         .then(function (result) {             response(result).code(200);          }).catch(function (err) {         request.server.log(['error'], err.stack);        ).code(200);     }); 

I am not getting information back if the people model was succesfully updated or not. Variable result is just an array with one element, 0=1

How can I know for certain that the record was updated or not.

like image 583
Wexoni Avatar asked Jul 22 '16 11:07

Wexoni


People also ask

What is update return Sequelize?

update() returns an array with two possible objects. The first is the number of rows affected by the update. It's always included. The second element is an array of the data instances from the rows themselves.

How do I update a database in Sequelize?

To update data in the database, sequelize provide update() method it will update your data on the database based on your condition. using sequelize update method we are able to write SQL update query in a function and JSON parameters.

How do I update multiple rows in Sequelize?

While Sequelize doesn't provide a bulkUpdate() method, both update() and bulkCreate() methods allow you to update multiple rows with a single method. When you need to update multiple rows with different values, you can use the bulkCreate() method.


2 Answers

Here's what I think you're looking for.

db.connections.update({   user: data.username,   chatroomID: data.chatroomID }, {   where: { socketID: socket.id },   returning: true,   plain: true }) .then(function (result) {   console.log(result);      // result = [x] or [x, y]   // [x] if you're not using Postgres   // [x, y] if you are using Postgres }); 

From Sequelize docs: The promise returns an array with one or two elements. The first element x is always the number of affected rows, while the second element y is the actual affected rows (only supported in postgres with options.returning set to true.)

Assuming you are using Postgres, you can access the updated object with result[1].dataValues.

You must set returning: true option to tell Sequelize to return the object. And plain: true is just to return the object itself and not the other messy meta data that might not be useful.

like image 117
nickang Avatar answered Sep 21 '22 18:09

nickang


You can just find the item and update its properties and then save it. The save() results in a UPDATE query to the db

const job = await Job.findOne({where: {id, ownerId: req.user.id}}); if (!job) {     throw Error(`Job not updated. id: ${id}`); }  job.name = input.name; job.payload = input.payload; await job.save(); 

On Postgres:

Executing (default): UPDATE "jobs" SET "payload"=$1,"updatedAt"=$2 WHERE "id" = $3 
like image 42
David Dehghan Avatar answered Sep 17 '22 18:09

David Dehghan