Important:Before running this generator, you must create an application using the application generator.Then you must run the command from the root directory of the application.

Synopsis

Adds a new LifeCycleObserver class to a LoopBack application.

  1. lb4 observer [--group <group>] [<name>]

Arguments and options

<name> - Required name of the observer to create as an argument to thecommand. If provided, the tool will use that as the default when it prompts forthe name.

—group <group> - Optional name of the observer group to sort the execution ofobservers by group.

Interactive Prompts

The tool will prompt you for:

  • Name of the observer.(observerName) If the name had been supplied fromthe command line, the prompt is skipped.

  • Group of the observer.(groupName) If the group had been supplied fromthe command line, the prompt is skipped.

Output

Once all the prompts have been answered, the CLI will do the following:

  • Create a LifeCycleObserver class as follows:/src/observers/${observerName}.observer.ts
  • Update /src/observers/index.ts to export the newly created LifeCycleObserverclass.The generated class looks like:
  1. import {
  2. /* inject, Application, CoreBindings, */
  3. lifeCycleObserver, // The decorator
  4. CoreTags,
  5. LifeCycleObserver, // The interface
  6. } from '@loopback/core';
  7. /**
  8. * This class will be bound to the application as a `LifeCycleObserver` during
  9. * `boot`
  10. */
  11. @lifeCycleObserver('observer-group-name')
  12. export class HelloObserver implements LifeCycleObserver {
  13. /*
  14. constructor(
  15. @inject(CoreBindings.APPLICATION_INSTANCE) private app: Application,
  16. ) {}
  17. */
  18. /**
  19. * This method will be invoked when the application starts
  20. */
  21. async start(): Promise<void> {
  22. // Add your logic for start
  23. }
  24. /**
  25. * This method will be invoked when the application stops
  26. */
  27. async stop(): Promise<void> {
  28. // Add your logic for start
  29. }
  30. }