Iridium icon indicating copy to clipboard operation
Iridium copied to clipboard

updated_at timestamps

Open roth-mike opened this issue 8 years ago • 6 comments

How would you suggest setting updated_at timestamps automatically? I saw the onSaving hook but according to the docs that only gets called when the instance.save() function is used. I also saw the Iridium-timestamps repo but wasn't quite sure how to apply that to how we have Iridium setup with Typescript and decorators to create the schemas.

roth-mike avatar Aug 30 '17 18:08 roth-mike

@mikeroth7 You could do a document @Transform to update that every time.

Css-IanM avatar Aug 30 '17 18:08 Css-IanM

and the toDB transform would only get called during any insert/update?

roth-mike avatar Aug 30 '17 18:08 roth-mike

yes, as long as you are re-assigning the field value, depending on how complex the object is. see here for more detailed explanation. https://github.com/SierraSoftworks/Iridium#transform-gotchas

Css-IanM avatar Aug 30 '17 19:08 Css-IanM

But do document level transforms get called when doing something like Model.update()? Because my breakpoint in the @Transform isn't being hit unless an instance.save() function is called.

roth-mike avatar Aug 30 '17 19:08 roth-mike

is your updated_at a top level property? have you tried the transform on the property itself? another point. update refreshes your model from the db, so i dont see toDB() not being called an issue, as there is nothing going into the db. and save would be adding to the db therefore toDB() would be hit.

EDIT: my mistake i was thinking of Instance update method.

Css-IanM avatar Aug 30 '17 20:08 Css-IanM

Hi @mikeroth7 - sorry for the late reply, unfortunately the Model.update() method doesn't currently support any kind of transforms (figuring out a transform with a complex query is really difficult to get right for everyone, all the time).

My best advice would be to either avoid using Model.update() and instead use Instance.save() (potentially with the 2nd parameter providing your change object), in which case instance level hooks will get triggered - enabling you to use something like iridium-timestamps, or alternatively manually set updated_at as part of all your calls to Model.update().

I'm sorry there isn't a better solution yet, but I also can't see much room for improving it without creating a snow-flake update hook.

If you can expand on your specific use case and why you're required to use Model.update() (there are a bunch of valid reasons) I'll keep that use case in mind as I continue to evolve the project's functionality and try to cater to it as soon as I'm able.

Regards, Benjamin

notheotherben avatar Aug 31 '17 07:08 notheotherben