Module: loopback

Class: loopback

loopback

LoopBack core module. It provides static properties and methods to create models and data sources. The module itself is a function that creates loopback app. For example:

var loopback = require('loopback');
var app = loopback();
Class Properties
Name Type Description
version String

Version of LoopBack framework. Static read-only property.

mime String
isBrowser Boolean

True if running in a browser environment; false otherwise. Static read-only property.

isServer Boolean

True if running in a server environment; false otherwise. Static read-only property.

registry Registry

The global Registry object.

faviconFile String

Path to a default favicon shipped with LoopBack. Use as follows: app.use(require('serve-favicon')(loopback.faviconFile));

loopback.autoAttach()

Attach any model that does not have a dataSource to the default dataSource for the type the Model requests

loopback.configureModel(ModelCtor, config)

Alter an existing Model class.

Arguments
Name Type Description
ModelCtor Model

The model constructor to alter.

config Object

Additional configuration to apply

config
Name Type Description
dataSource DataSource

Attach the model to a dataSource.

[relations] Object

Model relations to add/update.

loopback.createDataSource(name, options)

Create a data source with passing the provided options to the connector.

Arguments
Name Type Description
name String

Optional name.

options Object

Data Source options

options
Name Type Description
connector Object

LoopBack connector.

[*]

Other connector properties. See the relevant connector documentation.

loopback.createModel

Create a named vanilla JavaScript class constructor with an attached set of properties and options.

This function comes with two variants:

  • loopback.createModel(name, properties, options)
  • loopback.createModel(config)

In the second variant, the parameters name, properties and options are provided in the config object. Any additional config entries are interpreted as options, i.e. the following two configs are identical:

{ name: 'Customer', base: 'User' }
{ name: 'Customer', options: { base: 'User' } }

Example

Create an Author model using the three-parameter variant:

loopback.createModel(
  'Author',
  {
    firstName: 'string',
    lastName: 'string'
  },
  {
    relations: {
      books: {
        model: 'Book',
        type: 'hasAndBelongsToMany'
      }
    }
  }
);

Create the same model using a config object:

loopback.createModel({
  name: 'Author',
  properties: {
    firstName: 'string',
    lastName: 'string'
  },
  relations: {
    books: {
      model: 'Book',
      type: 'hasAndBelongsToMany'
    }
  }
});
Arguments
Name Type Description
name String

Unique name.

properties Object
options Object

(optional)

loopback.findModel(modelName)

Look up a model class by name from all models created by loopback.createModel()

Arguments
Name Type Description
modelName String

The model name

Returns
Name Type Description
result Model

The model class

loopback.getDefaultDataSourceForType(type)

Get the default dataSource for a given type.

Arguments
Name Type Description
type String

The datasource type.

Returns
Name Type Description
result DataSource

The data source instance

loopback.getModel(modelName)

Look up a model class by name from all models created by loopback.createModel(). Throw an error when no such model exists.

Arguments
Name Type Description
modelName String

The model name

Returns
Name Type Description
result Model

The model class

loopback.getModelByType(modelType)

Look up a model class by the base model class. The method can be used by LoopBack to find configured models in models.json over the base model.

Arguments
Name Type Description
modelType Model

The base model class

Returns
Name Type Description
result Model

The subclass if found or the base class

if()

Expose path to the default favicon file

only in node

loopback.memory([name])

Get an in-memory data source. Use one if it already exists.

Arguments
Name Type Description
[name] String

The name of the data source. If not provided, the 'default' is used.

loopback.remoteMethod(fn, options)

Add a remote method to a model.

Arguments
Name Type Description
fn Function
options Object

(optional)

loopback.setDefaultDataSourceForType(type, dataSource)

Set the default dataSource for a given type.

Arguments
Name Type Description
type String

The datasource type.

dataSource Object or DataSource

The data source settings or instance

Returns
Name Type Description
result DataSource

The data source instance.

loopback.template(path)

Create a template helper.

var render = loopback.template('foo.ejs');
var html = render({foo: 'bar'});
Arguments
Name Type Description
path String

Path to the template file.

Returns
Name Type Description
result Function