Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update the field 'updated_at' of a sequelize model without modifiyng any attributes

I'm using sequelize 2.0.0 with PostgreSQL. I would like to know if it's possible to update the field 'updated_at' of a model with one method. If yes, how can I achieve that?

For instance, in others frameworks, like Laravel, you have model.touch() to automatically update the 'updated_at' of a model.

I've already tried to use model.save() but as the sequelize doc says, calling this method without attributes does nothing. Also, in the doc, I didn't find anything that allow me to do what I need to do simply.

Thanks in advance for help.

Edit: To give an example of what I'm trying to achieved, I've already had an instance of my model: Model.findById(1).then(function(myInstance){ [...] myInstance.update() //here, I didn't change any attributes of myInstance and I would like to update the field udpated_at without doing another query. [...] }): The question is : How can I update the field updated_at of my previous instance with one method?

like image 570
naike Avatar asked Sep 27 '22 13:09

naike


1 Answers

To update an instance value, you can use instance.set(key, value, [options]).

myInstance.set('updatedAt', new Date());
myInstance.save().then(function() {
  // my nice callback stuff
});

The sequelize docs have a good example of doing an update query if you don't have an instance:

Post.update({
  updatedAt: null,
}, {
  where: {
    deletedAt: {
      $ne: null
    }
  }
});
like image 191
Evan Siroky Avatar answered Oct 11 '22 15:10

Evan Siroky