0

I am using loopback 4 and I want to create an object instance when the server starts and share it in the methods of all controllers. How can I achieve that? Thanks

Juan Pablo Fernandez
  • 2,408
  • 3
  • 18
  • 32

1 Answers1

1

Loopback 4 provides functionality to bind key-value pairs which is stored in memory and can be injected anywhere.

See the below example:

In your application.ts, you can bind key-value pairs that is maintained by loopback and can be injected into your controller.

export class User {
  name: string,
  age: number
}

let user = new User()
user.name = "Name"
user.age = 20


this.bind("user").to(user)

And then in your controller, you can simply inject the bound value.

export class CustomerDetailsController {
  constructor(
    @inject("user") private user: User
  ) { }
Ashutosh Chamoli
  • 987
  • 1
  • 11
  • 27