Page Contents

The @loopback/model-api-builder package provides types and helpers for packages contributing Model API builders. From a model class, this module can be used to define a contract to create a corresponding repository and controller. An example Model API builder is the CrudRestApiBuilder which is used to create a CRUD REST repository and controller. You can see more details in Creating CRUD REST APIs.

@loopback/model-api-builder helps create a configuration for the repository and controllers and @loopback/boot processes this configuration and registers appropriate repositories and controllers with the app.

To create your own API builder, you need to install the @loopback/model-api-builder dependency:

npm install --save @loopback/model-api-builder

Then you can define class to define your builder. For example:


import {ModelApiBuilder} from '@loopback/model-api-builder';

export class SampleApiBuilder implements ModelApiBuilder {}

Then bind the builder with @asModelApiBuilder so it can be used as an API builder option.

import {injectable} from '@loopback/core';
import {asModelApiBulder, ModelApiBuilder} from '@loopback/model-api-builder';

export class SampleApiBuilder implements ModelApiBuilder {}

Since the builder implements ModelApiBuilder, the build function must be defined:

import {injectable} from '@loopback/core';
import {
} from '@loopback/model-api-builder';
import {ApplicationWithRepositories} from '@loopback/repository';
import {Model} from '@loopback/rest';

export class SampleApiBuilder implements ModelApiBuilder {
  readonly pattern: string = 'Sample'; // put the name of your builder here

    application: ApplicationWithRepositories,
    modelClass: typeof Model & {prototype: Model},
    cfg: ModelApiConfig,
  ): Promise<void> {
    // here you can define how the repository and controller classes are built

Additionally, you can add configuration options to be used:

// imports

export interface SampleApiConfig extends ModelApiConfig {
  // add configuration options here

export class SampleApiBuilder implements ModelApiBuilder {
  // other code here

After the builder is created, it must be exported as a component in order for it to be selected by ModelApiBooter and to then be used in an application. Create a class for the component to export the builder:


import {Component, createBindingFromClass} from '@loopback/core';
import {SampleApiBuilder} from './sample.api-builder';

export class SampleComponent implements Component {
  bindings = [createBindingFromClass(SampleApiBuilder)];

To use the component, add it to your application class:


// other imports

// add an import for your component
import {SampleComponent} from '../sample.component';

export class ExampleApplication extends BootMixin(
) {
  constructor(options: ApplicationConfig = {}) {
    // other code

    // add the following line