on "after save, is there anyway to get data before the change?

770 views
Skip to first unread message

Benjamin Boudreau

unread,
Feb 13, 2015, 12:50:58 PM2/13/15
to loopb...@googlegroups.com
Is there a way to know what changed on an after save? This would be to trigger async jobs and refresh denormalized data.

Miroslav Bajtoš

unread,
Feb 14, 2015, 1:16:56 PM2/14/15
to loopb...@googlegroups.com
On Friday, February 13, 2015 at 9:50:58 AM UTC-8, Benjamin Boudreau wrote:
Is there a way to know what changed on an after save? This would be to trigger async jobs and refresh denormalized data.

Unless you are calling the `updateAll` method, the context object passed to "after save" handler contains an id of the object that was saved.

LoopBack does not track changes on the property level, you have to refresh denormalized data on per-model granularity.

Miroslav

Benjamin Boudreau

unread,
Feb 15, 2015, 12:06:38 PM2/15/15
to
Thanks, this make sense. Now lets say I have the current model as it is in the DB, so that I am able to know what will be changing onSave in the "before save" hook.

Do you have any suggestion on passing that data to the 'after save' hook? Would it be possible to keep part of the ctx of the new observe hooks between the before and the after? This would allow me to perform actions based on the initial state of my model.

before save: ctx.fieldWillChange = true

Right now, we end up using the currentContext at too many places to keep various states between before and after. But it makes for fragile code when we end up using scripts / jobs that are not remote and do not create a currentContext.

Benjamin

Miroslav Bajtoš

unread,
Feb 17, 2015, 8:47:29 AM2/17/15
to Benjamin Boudreau, loopb...@googlegroups.com
On Sun, Feb 15, 2015 at 9:06 AM, Benjamin Boudreau <bbou...@foko.co> wrote:
Thanks, this make sense. Not lets say I have the current model as it is in the DB, so that I am able to know what will be changing onSave in the "before save" hook.


Do you have any suggestion on passing that data to the 'after save' hook? Would it be possible to keep part of the ctx of the new observe hooks between the before and the after? This would allow me to perform actions based on the initial state of my model.

before save: ctx.fieldWillChange = true

We don't have a good solution for that now. While I was implementing the new operations hooks, we have discussed the idea of preserving the ctx object across before/after hooks. I decided to not implement it, because the context objects is not guaranteed to contain the same properties in all hooks. (Consider for example "query" and "after save".)

A possible solution is to define a new ctx property that will be preserved across all hooks invoked as part of the same operation:

before save: ctx.custom.fieldWillChange = true
after save: if (ctx.custom.fieldWillChange) //...

Can you contribute this feature yourself? Perhaps use a better property name than "custom".
 
Right now, we end up using the currentContext at too many places to keep various states between before and after. But it makes for fragile code when we end up using scripts / jobs that are not remote and do not create a currentContext.

This is a bug of the current implementation. It should be possible to run methods using currentContext locally, without the remoting layer.

The idea is to extract the code setting up the context from server/middleware/token.js, so that it can be run by user code.

Something along the following lines:

loopback.runInContext = function(fn) {
  // setup context and run fn()
};

// context middleware
return function(req, res, next) {
  // TODO: prevent double-invocation, etc.
  loopback.runInContext(next);
}

Miroslav

Bunker

unread,
Feb 4, 2016, 6:09:32 AM2/4/16
to LoopbackJS, bbou...@foko.co, miro...@strongloop.com
Hi Miroslav,

Has there already been an implementation of this feature?

Benjamin Boudreau

unread,
Feb 4, 2016, 6:39:45 AM2/4/16
to Bunker, LoopbackJS, miro...@strongloop.com
yes ctx.hookState

Bunker

unread,
Feb 4, 2016, 7:59:35 AM2/4/16
to LoopbackJS, jo...@iambnb.nl, miro...@strongloop.com
Thanks, hookState seems to be an empty object. Do I need to add the old data in a 'before save' observer myself to hookState? There's not so much documentation on hookState unfortunately.

Benjamin Boudreau

unread,
Feb 4, 2016, 8:29:10 AM2/4/16
to LoopbackJS, Bunker, miro...@strongloop.com
yes it is kept between before save and after save - Look at dao.js in datasource-juggler for it.

--
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/RRHbqNI1GTY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/531af808-7b5d-4282-8742-dd5d8f75ac62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages