LoopBack 4 Todo Application Tutorial - Adding legacy juggler
Page Contents

Adding Legacy Juggler

The Legacy Juggler is a “bridge” between the existing loopback-datasource-juggler and the new LoopBack 4 architecture. It provides the capabilities required to access persistence layers/APIs, and perform CRUD operations on those sources of data.

It also provides many of the functions and interfaces we’ll require for setting up our new LoopBack application, which is why we’re starting here.

Now that you have your setup, it’s time to modify it to add in @loopback/repository. Install this dependency by running npm i --save @loopback/repository.

Next, modify src/application.ts to change the base class of your app to use the RepositoryMixin:

src/application.ts

import {ApplicationConfig} from '@loopback/core';
import {RestApplication, RestServer} from '@loopback/rest';
import {MySequence} from './sequence';

/* tslint:disable:no-unused-variable */
// Binding and Booter imports are required to infer types for BootMixin!
import {BootMixin, Booter, Binding} from '@loopback/boot';

// juggler and DataSourceConstructor imports are required to infer types for RepositoryMixin!
import {
  Class,
  Repository,
  RepositoryMixin,
  juggler,
  DataSourceConstructor,
} from '@loopback/repository';
/* tslint:enable:no-unused-variable */

export class TodoListApplication extends BootMixin(
  RepositoryMixin(RestApplication),
) {
  constructor(options?: ApplicationConfig) {
    super(options);

    // Set up the custom sequence
    this.sequence(MySequence);

    this.projectRoot = __dirname;
    // Customize @loopback/boot Booter Conventions here
    this.bootOptions = {
      controllers: {
        // Customize ControllerBooter Conventions here
        dirs: ['controllers'],
        extensions: ['.controller.js'],
        nested: true,
      },
    };
  }

  async start() {
    await super.start();

    const server = await this.getServer(RestServer);
    const port = await server.get<number>('rest.port');
    console.log(`Server is running at http://127.0.0.1:${port}`);
    console.log(`Try http://127.0.0.1:${port}/ping`);
  }
}

Once you’re ready, we’ll move on to the Add your Todo model section.

For more information on the Legacy Juggler, check out the @loopback/repository package or see the Repositories section of our docs.

Previous step: Scaffolding your application

Next step: Add your Todo model