Open roth-mike opened 7 years ago
@mikeroth7 You could do a document @Transform
to update that every time.
and the toDB
transform would only get called during any insert/update?
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
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.
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.
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
How would you suggest setting updated_at timestamps automatically? I saw the
onSaving
hook but according to the docs that only gets called when theinstance.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.