SierraSoftworks / Iridium

A high performance MongoDB ORM for Node.js
http://sierrasoftworks.github.io/Iridium/
Other
570 stars 25 forks source link

updated_at timestamps #98

Open roth-mike opened 7 years ago

roth-mike commented 7 years ago

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.

Css-IanM commented 7 years ago

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

roth-mike commented 7 years ago

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

Css-IanM commented 7 years ago

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

roth-mike commented 7 years ago

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.

Css-IanM commented 7 years ago

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.

notheotherben commented 7 years ago

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