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

Improve docs for Operation hooks #1173

Closed
bajtos opened this issue Mar 5, 2015 · 19 comments
Closed

Improve docs for Operation hooks #1173

bajtos opened this issue Mar 5, 2015 · 19 comments
Assignees
Labels

Comments

@bajtos
Copy link
Member

bajtos commented Mar 5, 2015

Judging by the recent discussions in GitHub issues, the current minimal documentation does not make it clear how to correctly use the hooks and why they are designed in a seemingly unintuitive way.

The goal of this task is to write down all knowledge that's in Miroslav's head at the moment, add information from comments in the issues linked below, and beef up the doc page Operation hooks.

A list of pain-points can be found here:

Related issues:

@bajtos bajtos self-assigned this Mar 5, 2015
@chandadharap
Copy link

I'd rather that Rand sit with you and get the info to update and improve docs, rather than you write this.

@chandadharap chandadharap added #plan and removed #tob labels Mar 5, 2015
@crandmck
Copy link
Contributor

crandmck commented Mar 5, 2015

Miroslav, I will go through the above issues and try to extract the pertinent info. I will likely have questions as a result.... Then let's meet and you can give me a brain dump, as Chanda suggests. You could also jot down some notes if you have some specific things you know you'd like to add to or change in the docs.

@crandmck
Copy link
Contributor

crandmck commented Mar 9, 2015

This may not be completed this sprint, since the feature is being significantly reworked.

Miroslav said:

there are still pull requests in progress that will improve certain areas, so this docs work may be a longer term affair.

@bajtos
Copy link
Member Author

bajtos commented Mar 9, 2015

@crandmck first few things that are ready to be documented.

Document "hookState" - see loopbackio/loopback-datasource-juggler#485 and loopbackio/loopback-datasource-juggler#484

Capture the information from loopbackio/loopback-datasource-juggler#483 (comment). The first part explains why there is "instance" object instead of providing always a "data" object. The second part explains different use cases of hooks.

Explain why the "before delete" and "after delete" hooks don't receive a list of deleted ids, see the discussion in loopbackio/loopback-datasource-juggler#474. Explain that a naive implementation of hooks creates a race condition (loopbackio/loopback-datasource-juggler#474 (comment)) and describe how it can be avoided (loopbackio/loopback-datasource-juggler#474 (comment)).

NOTE: loopbackio/loopback-datasource-juggler#488 is adding ctx.instance to both "delete" hooks when they are deleting a single model, we will need to describe that once the PR is landed. Because batch delete is not exposed via REST API by default (IIRC), in most cases ctx.instance should be a good solution for getting data of the deleted model (including the id).

loopbackio/loopback-datasource-juggler#481 contains useful information too, e.g. the first part of loopbackio/loopback-datasource-juggler#481 (comment) explains why "updateAttributes" does not have ctx.instance.

NOTE: loopbackio/loopback-datasource-juggler#488 is adding ctx.currentInstance to "before save" hook triggered by "updateAttributes", this should somewhat alleviate the problem.

@bajtos
Copy link
Member Author

bajtos commented Mar 9, 2015

Some material for starting a FAQ section: loopbackio/loopback-datasource-juggler#482 (comment)

@bajtos
Copy link
Member Author

bajtos commented Mar 9, 2015

Re-estimating to 1 story point. I'll only gather more pointers to relevant comments and pull requests, so that @crandmck can write the content himself.

@crandmck
Copy link
Contributor

@bajtos Added a note about ctx.options that Fabien added. However, we really need an example. See http://docs.strongloop.com/display/LB/Operation+hooks#Operationhooks-Operationhookcontextobject.

@bajtos
Copy link
Member Author

bajtos commented Apr 17, 2015

@crandmck Can we get an example of how ctx.options might be used?

Here is a code snippet based on loopbackio/loopback-datasource-juggler#549 (comment).

var FILTERED_PROPERTIES = ['immutable', 'birthday'];
MyModel.observe('before save', function filterProperties(ctx, next) {
  if (ctx.options && ctx.options.skipPropertyFilter) return next();
  if (ctx.instance) {
    FILTERED_PROPERTIES.forEach(function(p) { ctx.instance.unsetAttribute(p); });
  } else {
    FILTERED_PROPERTIES.forEach(function(p) { delete ctx.data[p]; });
  }
  next();
});

// immutable is not updated
MyModel.updateOrCreate({ id: 1, immutable: 'new value' }, cb);

// immutable is changed
MyModel.updateOrCreate({ id: 2, immutable: 'new value' }, { skipPropertyFilter: true }, cb);

@crandmck
Copy link
Contributor

Added the example code..thanks.

We still need to add all the items noted in the comment above.

@bajtos Were you planning to do this, or did you want me to do it?

@bajtos
Copy link
Member Author

bajtos commented Apr 20, 2015

Were you planning to do this, or did you want me to do it?

Please do it yourself. My plan was only to collect a list of pointers for you.

@bajtos
Copy link
Member Author

bajtos commented Apr 20, 2015

Actually there are no more features to add to the list in the comment above, I am reassigning this issue to you, @crandmck.

@bajtos bajtos assigned crandmck and unassigned bajtos Apr 20, 2015
@chandadharap chandadharap assigned DeniseLee and unassigned crandmck May 5, 2015
@chandadharap chandadharap added #plan and removed #tob labels May 5, 2015
@crandmck crandmck self-assigned this May 29, 2015
@crandmck crandmck added #plan and removed #tob labels Jun 10, 2015
@chandadharap chandadharap changed the title Improve docs for Operation hooks 73 Improve docs for Operation hooks Jun 16, 2015
@chandadharap chandadharap changed the title 73 Improve docs for Operation hooks Improve docs for Operation hooks Jun 16, 2015
@crandmck
Copy link
Contributor

OK, here is what I have for this:

I'm going to need a bit more time to sort all this out, because it's fairly complex and there are an abundance of PRs and discussions to sort through.

@crandmck crandmck added #wip and removed #sprint73 labels Jun 29, 2015
@bajtos
Copy link
Member Author

bajtos commented Jun 30, 2015

OK, here is what I have for this:
(...)

Sounds good!

FYI, there were two more hooks added recently, see loopbackio/loopback-datasource-juggler#599 and loopbackio/loopback-datasource-juggler#586. I am afraid the task of (fully) documenting operation hooks will be a long one.

@crandmck
Copy link
Contributor

crandmck commented Jul 1, 2015

@bajtos It was difficult for me to sort out from the very lengthy discussions across multiple PRs what was actually done and needs to be documented. I gave it my best shot, but apologies if I missed something. I admit to still being somewhat confused.

Please review the updates referenced in the comment above and let me know what more needs to be added or changed. Some illustrative examples would help.

Re the two new hooks, see http://docs.strongloop.com/display/LB/Operation+hooks#Operationhooks-newhooks. Note this information is "hidden" until the new features are released to production.

@crandmck crandmck added #review and removed #wip labels Jul 1, 2015
@crandmck crandmck assigned bajtos and unassigned crandmck Jul 1, 2015
@bajtos
Copy link
Member Author

bajtos commented Jul 8, 2015

@crandmck thank you for updating the doc page. I am afraid I don't have enough time to review the changes now, I have created a new sprint story so that we can plan this task: https://github.com/strongloop-internal/scrum-loopback/issues/376 Sorry for the delay :(

@crandmck
Copy link
Contributor

crandmck commented Jul 8, 2015

OK, np. It took me quite a while to get around to working on this myself.

@crandmck crandmck assigned raymondfeng and unassigned bajtos Jul 9, 2015
@raymondfeng raymondfeng assigned bajtos and unassigned raymondfeng Aug 11, 2015
@crandmck
Copy link
Contributor

@mike-aungsan
Copy link

Is it possible to terminate the invocation from Operation hooks?
I cannot find in doc. I try ctx.end() and it raise err.

@bajtos
Copy link
Member Author

bajtos commented Feb 17, 2016

@mike-aungsan

Is it possible to terminate the invocation from Operation hooks?
I cannot find in doc. I try ctx.end() and it raise err.

You can terminate the invocation by raising an error via next(err).

In the future, please post questions to our mailing list and/or community forums, see https://github.com/strongloop/loopback/wiki/Questions.

@strongloop strongloop locked and limited conversation to collaborators Feb 17, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants