Model Hooks vs Operation Hook

251 views
Skip to first unread message

Raj Lalwani

unread,
Feb 25, 2015, 3:30:23 AM2/25/15
to loopb...@googlegroups.com
Hi, 

We are using operation "before save" for a model. We have a requirement where we want to delete previous record for an id. With Model hooks such as beforeCreate and beforeUpdate it was easier to delete the records only when creating i.e. destroyAll in beforeCreate but not beforeUpdate. It has become difficult with operation hooks. 

Could you please suggest a better way to achieve that using Operation Hooks? I have an option to use Model Hooks though. 

Thanks for your help in advance,
Raj

Miroslav Bajtoš

unread,
Feb 25, 2015, 1:21:35 PM2/25/15
to loopb...@googlegroups.com
Hi Raj,

On Wednesday, February 25, 2015 at 9:30:23 AM UTC+1, Raj Lalwani wrote:
We are using operation "before save" for a model. We have a requirement where we want to delete previous record for an id. With Model hooks such as beforeCreate and beforeUpdate it was easier to delete the records only when creating i.e. destroyAll in beforeCreate but not beforeUpdate. It has become difficult with operation hooks. 

Could you please suggest a better way to achieve that using Operation Hooks? I have an option to use Model Hooks though. 

The reason why "before save" does not distinguish between Create and Update is because the atomic version of "updateOrCreate" (a.k.a "upsert") cannot tell in advance whether the operation will end up as an Update or a Create.

Here are some options how to address your needs:

1) Let "before save" hook fill a "ctx.created" property, this property will be undefined in updateOrCreate. There is a similar proposal for "after save" hooks: https://github.com/strongloop/loopback/issues/838#issuecomment-72808583.

2) Implement per-method hooks and use these hooks for update and create:
Then you can either not install any hook for updateOrCreate, or you can install a hook that throws an error.

Miroslav

Líus Fontenelle Carneiro

unread,
Feb 25, 2015, 3:31:49 PM2/25/15
to loopb...@googlegroups.com
Hi Raj,

I think this example code can help:


Model.observe('before save', function (ctx, next) {
if(ctx.instance) {
ctx.instance.createdAt = ctx.instance.updatedAt = new Date();
} else {
ctx.data.updatedAt = new Date();
}

next();
});



Best Regards,

Líus

Miroslav Bajtoš

unread,
Feb 26, 2015, 2:18:55 AM2/26/15
to Líus Fontenelle Carneiro, loopb...@googlegroups.com
A word of warning: In general, I would strongly recommend against using `if(ctx.instance)` for detecting Create vs. Update.

For example, "Model.prototype.save()" calls "before save" hook with "ctx.instance" set too. If you ever happen to call this method from your code, the hook described below will incorrectly override the "createdAt" field. In the future, we may add more Update-like methods calling "before save" with "ctx.instance" set.

However, if you never call ".save()" and use the default REST API only, then the hook described below should work fine with the current version of loopback-datasource-juggler. 

Miroslav

Líus Fontenelle Carneiro

unread,
Feb 27, 2015, 6:40:27 AM2/27/15
to loopb...@googlegroups.com, liu...@gmail.com
Bajtos,

Thanks for the advice. If I understand well, when I'm using all Node API available for PersistedModel, there is no predictable, direct way (like old beforeCreate model hook) to ensure that a code will run only when creating, via remote API, because of usage of upsert method internally by LB, right?

Thanks,

Líus
Reply all
Reply to author
Forward
0 new messages