Class: LoopBackApplication Static Methods

app.connector(name, connector)

Register a connector.

When a new data-source is being added via app.dataSource, the connector
name is looked up in the registered connectors first.

Connectors are required to be explicitly registered only for applications
using browserify, because browserify does not support dynamic require,
which is used by LoopBack to automatically load the connector module.

Arguments

Name

Type

Description

name

String

Name of the connector, e.g. 'mysql'.

connector

Object

Connector object as returned by require('loopback-connector-{name}').

app.dataSource(name, config)

Define a DataSource.

Arguments

Name

Type

Description

name

String

The data source name

config

Object

The data source config

app.enableAuth()

Enable app wide authentication.

app.listen([cb])

Listen for connections and update the configured port.

When there are no parameters or there is only one callback parameter,
the server will listen on app.get('host') and app.get('port').

For example, to listen on host/port configured in app config:

app.listen();

Otherwise all arguments are forwarded to http.Server.listen.

For example, to listen on the specified port and all hosts, and ignore app config.

app.listen(80);

The function also installs a listening callback that calls
app.set('port') with the value returned by server.address().port.
This way the port param contains always the real port number, even when
listen was called with port number 0.

Arguments

Name

Type

Description

[cb]

Function

If specified, the callback is added as a listener for the server's "listening" event.

Returns

Name

Type

Description

result

http.Server

A node http.Server with this application configured as the request handler.

app.model(Model, config)

Attach a model to the app. The Model will be available on the
app.models object.

Example - Attach an existing model:

var User = loopback.User;
app.model(User);

Example - Attach an existing model, alter some aspects of the model:

var User = loopback.User;
app.model(User, { dataSource: 'db' });

Arguments

Name

Type

Description

Model

Object or String

The model to attach.

config

Object

The model's configuration.

config

Name

Type

Description

dataSource

String or DataSource

The DataSource to which to attach the model.

[public]

Boolean

Whether the model should be exposed via REST API.

[relations]

Object

Relations to add/update.

Returns

Name

Type

Description

result

ModelConstructor

the model class

app.models()

Get the models exported by the app. Returns only models defined using app.model()

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

Class: loopback Static Methods

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:

Class: Registry Static Methods

addACL(acls, acl)

Class: Registry Instance Methods

registry.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.

registry.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:

PersistedModel.findOrCreate(where, data, callback)

Find one record matching the optional where filter. The same as find, but limited to one object.
Returns an object, not collection.
If not found, create the object using data provided as second argument.

persistedModel.setId(val)

Set the correct id property for the PersistedModel. Uses the setId method if the model is attached to
connector that defines it. Otherwise, uses the default lookup.
Override this method to handle complex IDs.

Arguments

Name

Type

Description

val

The id value. Will be converted to the type that the id property specifies.

persistedModel.updateAttribute(name, value, callback)

Update a single attribute.
Equivalent to updateAttributes({name: 'value'}, cb)

Whether to use production Apple Push Notification Service (APNS) servers to send push notifications. If true, uses gateway.push.apple.com:2195 and feedback.push.apple.com:2196.
If false, uses gateway.sandbox.push.apple.com:2195 and feedback.sandbox.push.apple.com:2196

Algorithm used to create cryptographic hash, used as argument to crypto.createHash. Default is sha1.

settings.ignoreErrors

Boolean

By default, when changes are rectified, an error will throw an exception. However, if this setting is true, then errors will not throw exceptions.

Class: Change Static Methods

Change.bothDeleted(a, b)

Are both changes deletes?

Arguments

Name

Type

Description

a

Change

b

Change

Change.diff(modelName, since, remoteChanges, callback)

Determine the differences for a given model since a given checkpoint.

The callback will contain an error or result.

result

{
deltas: Array,
conflicts: Array
}

deltas

An array of changes that differ from remoteChanges.

conflicts

An array of changes that conflict with remoteChanges.

Arguments

Name

Type

Description

modelName

String

since

Number

Compare changes after this checkpoint

remoteChanges

Array.<Change>

A set of changes to compare

callback

Function

Callback

Name

Type

Description

err

Error

result

Object

See above.

Change.findOrCreateChange(modelName, modelId, callback)

Find or create a change for the given model.

Arguments

Name

Type

Description

modelName

String

modelId

String

callback

Function

Callback

Name

Type

Description

err

Error

change

Change

Change.getCheckpointModel()

Get the checkpoint model.

Change.hash(str)

Create a hash of the given string with the options.hashAlgorithm.
Default: sha1

Arguments

Name

Type

Description

str

String

The string to be hashed

Change.idForModel(modelName, modelId)

Get an identifier for a given model.

Arguments

Name

Type

Description

modelName

String

modelId

String

Change.rectifyAll(cb)

Correct all change list entries.

Arguments

Name

Type

Description

cb

Function

Change.rectifyModelChanges(modelName, modelIds, callback)

Track the recent change of the given modelIds.

Arguments

Name

Type

Description

modelName

String

modelIds

Array

callback

Function

Callback

Name

Type

Description

err

Error

changes

Array

Changes that were tracked

Change.revisionForInst(inst)

Get the revision string for the given object

Arguments

Name

Type

Description

inst

Object

The data to get the revision string for

Class: Change Instance Methods

change.conflictsWith(change)

Does this change conflict with the given change.

Arguments

Name

Type

Description

change

Change

change.currentRevision(callback)

Get a change's current revision based on current data.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

rev

String

The current revision

change.equals(change)

Compare two changes.

Arguments

Name

Type

Description

change

Change

change.getModelCtor()

Get the Model class for change.modelName.

change.isBasedOn(change)

Determine if the change is based on the given change.

Arguments

Name

Type

Description

change

Change

change.rectify(callback)

Update (or create) the change with the current revision.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

change

Change

change.type()

Get a change's type. Returns one of:

Change.UPDATE

Change.CREATE

Change.DELETE

Change.UNKNOWN

Class: Change.Conflict

Change.Conflict

When two changes conflict a conflict is created.

Note: call conflict.fetch() to get the target and source models.

Arguments

Name

Type

Description

modelId

SourceModel

PersistedModel

TargetModel

PersistedModel

Class Properties

Name

Type

Description

source

ModelClass

The source model instance

target

ModelClass

The target model instance

Class: Change.Conflict Instance Methods

conflict.changes(callback)

Get the conflicting changes.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

sourceChange

Change

targetChange

Change

conflict.models(callback)

Fetch the conflicting models.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

source

PersistedModel

target

PersistedModel

conflict.resolve(callback)

Resolve the conflict.

Set the source change's previous revision to the current revision of the
(conflicting) target change. Since the changes are no longer conflicting
and appear as if the source change was based on the target, they will be
replicated normally as part of the next replicate() call.

This is effectively resolving the conflict using the source version.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

conflict.resolveManually(data, callback)

Resolve the conflict using the supplied instance data.

Arguments

Name

Type

Description

data

Object

The set of changes to apply on the model instance. Use null value to delete the source instance instead.

callback

Function

Callback

Name

Type

Description

err

Error

conflict.resolveUsingSource(callback)

Resolve the conflict using the instance data in the source model.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

conflict.resolveUsingTarget(callback)

Resolve the conflict using the instance data in the target model.

Arguments

Name

Type

Description

callback

Function

Callback

Name

Type

Description

err

Error

conflict.swapParties()

Return a new Conflict instance with swapped Source and Target models.

This is useful when resolving a conflict in one-way
replication, where the source data must not be changed:

Default time to live (in seconds) for the AccessToken created by User.login() / user.createAccessToken(). Default is 1209600 (2 weeks)

settings.maxTTL

Number

The max value a user can request a token to be alive / valid for. Default is 31556926 (1 year)

settings.realmRequired

Boolean

Require a realm when logging in a user.

settings.realmDelimiter

String

When set a realm is required.

settings.resetPasswordTokenTTL

Number

Time to live for password reset AccessToken. Default is 900 (15 minutes).

settings.saltWorkFactor

Number

The bcrypt salt work factor. Default is 10.

Class: User Static Methods

User.confirm(userId, token, redirect, callback)

Confirm the user's identity.

Arguments

Name

Type

Description

userId

Any

token

String

The validation token

redirect

String

URL to redirect the user to once confirmed

callback

Function

Callback

Name

Type

Description

err

Error

User.generateVerificationToken(user, cb)

A default verification token generator which accepts the user the token is
being generated for and a callback function to indicate completion.
This one uses the crypto library and 64 random bytes (converted to hex)
for the token. When used in combination with the user.verify() method this
function will be called with the user object as it's context (this).

Name of template that displays verification page, for example, `'verify.ejs'.

redirect

String

Page to which user will be redirected after they verify their email, for example '/' for root URI.

generateVerificationToken

Function

A function to be used to generate the verification token. It must accept the user object and a
callback function. This function should NOT add the token to the user
object, instead simply execute the callback with the token! User saving
and email sending will be handled in the verify() method.