Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated_at timestamps #98

Open
roth-mike opened this issue Aug 30, 2017 · 6 comments
Open

updated_at timestamps #98

roth-mike opened this issue Aug 30, 2017 · 6 comments

Comments

@roth-mike
Copy link

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
Copy link

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

@roth-mike
Copy link
Author

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

@Css-IanM
Copy link

Css-IanM commented Aug 30, 2017

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
Copy link
Author

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
Copy link

Css-IanM commented Aug 30, 2017

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants