0

My Sails application helps monitor the state of our system by monitoring a mix of artifacts (log files, exports, etc) and real-time changes (events, web-hooks, etc).

On various schedules the application reads the artifacts and uses .findOrCreate() and .update() to update the Sails application state. Sails then sets the instances createdAt and updatedAt properties to new Date() at the time of saving; however I'd like these times to match their source artifact's time-stamp.

How can I override the values for their createdAt and updatedAt properties of a Sails.js model? And would there be any obvious gotcha's as a result of setting them to a specific timestamp?

STW
  • 44,917
  • 17
  • 105
  • 161
  • so far I've stored the data in seperate properties, but I'd really prefer to override the `createdAt` and `updatedAt` values. Also, I'd like to understand the influence of these fields on the system behavior. – STW Jan 12 '16 at 21:24
  • have you seen my answer? If you have any questions, please do ask - be happy to clarify if needed. – arcseldon Jan 13 '16 at 06:23

1 Answers1

1

In your model, you can turn off the defaults.

autoCreatedAt: false,
autoUpdatedAt: false,

You are now free to use your own values. You can use the beforeUpdate lifecycle callback (if needed) to add any extra behaviour just prior to updating the model.

For example:

beforeUpdate:function(values, next) {
    // do whatever you need here if required.
    // you could just override the default createdAt, updatedAt behaviour
    next();
}

See similar question answered by a core maintainer here.

If this still doesn't quite address your needs, then leave me a comment below and I will try to update the answer further.

Community
  • 1
  • 1
arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • I've had the chance to play around with this and think it will work. Setting `autoUpdatedAt: false` removes the property from the model, so then it needs to be added to `attributes` as `updatedAt: 'datetime'`. – STW Jan 14 '16 at 18:31