Skip to content

Migration help: Can Interceptors work with mouted lb3 application? #6099

Closed

Description

@dzhiwei

LoopBack 3 use case

I have one LB3 project with operation hook function used: .afterRemote and .observe('before save'.

LoopBack 4 new solution

I am referring to this toturial: https://loopback.io/doc/en/lb4/migration-mounting-lb3app.html for migration now.
The good news is that Swagger API is working after the migration.
Unfortunally, I find the operation hook will not work anymore after I mount LB3 project as a component in LB4.
And there's interceptors will take the role of operation hooks, But it seems inteceptors is only working with LB4 Controllers and handler functions for REST routes.

Is that possible LB4 interceptors can work with mouted LB3 application? Or am I misunderstanding the LB4 interceptors use case?
Not sure if we can configure my global interceptor to app in the index.ts. I did't find a good example to explain the

app.interceptor(CachingInterceptorProvider, {
  global: true,
  group: 'caching',
  key: 'caching-interceptor',
});

Thank you so much.

Activity

  1. dhmlau commented on Aug 13, 2020

    @dhmlau
    Member

    @dzhiwei, I believe the closest migration path for the operation hooks is to override the definePersistedModel method in DefaultCrudRepository in the model’s repository. You can find more details in: https://loopback.io/doc/en/lb4/migration-models-operation-hooks.html.

  2. self-assigned this
    on Aug 13, 2020
  3. dzhiwei commented on Aug 13, 2020

    @dzhiwei
    Author

    @dzhiwei, I believe the closest migration path for the operation hooks is to override the definePersistedModel method in DefaultCrudRepository in the model’s repository. You can find more details in: https://loopback.io/doc/en/lb4/migration-models-operation-hooks.html.

    @dhmlau Thank you so much for your reply.
    Currently I only have loopback 3 models, does that mean I need to rewrite the loopback 4 repository for loopback 3 models to make the operation hooks function work?
    The perferred option is that no more code for rewriting the loopback 3 models and only the remoting/operation hooks will be implemente with Loopback 4 manner. Is that feasible? I would not like to rewrite it, because It will take too much effort to rewrite the whole thing if I need to use LB4 repository instead of LB3 models(in lb3app).

  4. dhmlau commented on Aug 17, 2020

    @dhmlau
    Member

    @hacksparrow @nabdelgadir, do you think you can help with the question since you both were working on the mounting LB3 app guides? Thanks.

  5. nabdelgadir commented on Aug 23, 2020

    @nabdelgadir
    Contributor

    Hi, sorry for the delayed response. If you want to use a global interceptor, try using the lb4 interceptor command e.g.

    $ lb4 interceptor
    ? Interceptor name: global
    ? Is it a global interceptor? Yes
    
    Global interceptors are sorted by the order of an array of group names bound to ContextBindings.GLOBAL_INTERCEPTOR_ORDERED_GROUPS. See https://loopback.io/doc/en/lb4/Interceptors.html#order-of-invocation-for-interceptors.
    
    ? Group name for the global interceptor: ('') 
       create src/interceptors/global.interceptor.ts
       update src/interceptors/index.ts
    
    Interceptor Global was/were created in src/interceptors
    

    In the created src/interceptors/global.interceptor.ts (or whatever you name it) file, you can add your logic there.

    Let us know if this works for you. Thanks!

  6. dhmlau commented on Sep 11, 2020

    @dhmlau
    Member

    @dzhiwei, we believe we've answered your question. Closing this issue. Thanks.

  7. Quent1Pr commented on Nov 25, 2020

    @Quent1Pr
    Contributor

    I try a global interceptor but it only intercepts lb4 queries and not lb3 queries.
    I found a workaround with a lb4 middleware : https://loopback.io/doc/en/lb4/Middleware.html#loopback-style-middleware

  8. nabdelgadir commented on Nov 25, 2020

    @nabdelgadir
    Contributor

    Thanks @QPierre1! I think we can document the middleware workaround you're using for now and then we'll have to see if we can get lb4 interceptors to also work for lb3 routes. Would you be interested in doing the documentation portion?

  9. locked and limited conversation to collaborators on Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions