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();
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 |
faviconFile |
String
|
Path to a default favicon shipped with LoopBack. Use as follows: |
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.
Name | Type | Description |
---|---|---|
ModelCtor |
Model
|
The model constructor to alter. |
config |
Object
|
Additional configuration to apply |
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.
Name | Type | Description |
---|---|---|
name |
String
|
Optional name. |
options |
Object
|
Data Source 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'
}
}
});
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()
Name | Type | Description |
---|---|---|
modelName |
String
|
The model name |
Name | Type | Description |
---|---|---|
result |
Model
|
The model class |
loopback.getDefaultDataSourceForType(type)
Get the default dataSource
for a given type
.
Name | Type | Description |
---|---|---|
type |
String
|
The datasource type. |
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.
Name | Type | Description |
---|---|---|
modelName |
String
|
The model name |
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.
Name | Type | Description |
---|---|---|
modelType |
Model
|
The base model class |
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.
Name | Type | Description |
---|---|---|
[name] |
String
|
The name of the data source. If not provided, the |
loopback.remoteMethod(fn, options)
Add a remote method to a model.
Name | Type | Description |
---|---|---|
fn |
Function
|
|
options |
Object
|
(optional) |
loopback.setDefaultDataSourceForType(type, dataSource)
Set the default dataSource
for a given type
.
Name | Type | Description |
---|---|---|
type |
String
|
The datasource type. |
dataSource |
Object or DataSource
|
The data source settings or instance |
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'});
Name | Type | Description |
---|---|---|
path |
String
|
Path to the template file. |
Name | Type | Description |
---|---|---|
result |
Function
|