Skip to content

Access model settings in Loopback 4 #6417

Closed

Description

@condratbogdan

In Loopback 3 i could access the models and their settings by using app.models['Logs'].settings.
How can i do this in Loopback 4?

Activity

  1. raymondfeng commented on Sep 21, 2020

    @raymondfeng
    Contributor

    LB4 model classes are bound the Application context. You can use dependency injection or service locator to get a model class by name:

    const settings = app.getSync('models.MyModel#.definition.settings');
    
    Or
    
    @inject('models.MyModel#definition.settings')
    private settings: object;
  2. achrinza commented on Sep 23, 2020

    @achrinza
    Member

    const settings = app.getSync('models.MyModel#.definition.settings');

    Would app.get() be preferred since it's non-blocking? AFAIK, app.getSync() exists only for use in a constructor, which can't be async-ed.

  3. raymondfeng commented on Sep 23, 2020

    @raymondfeng
    Contributor

    Would app.get() be preferred since it's non-blocking? AFAIK, app.getSync() exists only for use in a constructor, which can't be async-ed.

    Not exactly. The difference between getSync and get is whether the binding can be resolved synchronously or asynchronously. If the binding resolution returns a promise by itself or its dependencies, getSync will throw an error. Please await ctx.get(...) works for both cases.

    From performance perspective, getSync is more efficient than get. If the binding produces a value synchronously, using get() is the same as getSync() except the returned value is wrapped in a Promise.

  4. 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

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions