Class: Model

.exports~Model(data)

new Model(data)

The base class for **all models**. **Inheriting from `Model`** ```js var properties = {...}; var options = {...}; var MyModel = loopback.Model.extend('MyModel', properties, options); ``` **Options** - `trackChanges` - If true, changes to the model will be tracked. **Required for replication.** **Events** #### Event: `changed` Emitted after a model has been successfully created, saved, or updated. Argument: `inst`, model instance, object ```js MyModel.on('changed', function(inst) { console.log('model with id %s has been changed', inst.id); // => model with id 1 has been changed }); ``` #### Event: `deleted` Emitted after an individual model has been deleted. Argument: `id`, model ID (number). ```js MyModel.on('deleted', function(id) { console.log('model with id %s has been deleted', id); // => model with id 1 has been deleted }); ``` #### Event: `deletedAll` Emitted after all models have been deleted. Argument: `where` (optional), where filter, JSON object. ```js MyModel.on('deletedAll', function(where) { if (where) { console.log('all models where ', where, ' have been deleted'); // => all models where // => {price: {gt: 100}} // => have been deleted } }); ``` #### Event: `attached` Emitted after a `Model` has been attached to an `app`. #### Event: `dataSourceAttached` Emitted after a `Model` has been attached to a `DataSource`. #### Event: set Emitted when model property is set. Argument: `inst`, model instance, object ```js MyModel.on('set', function(inst) { console.log('model with id %s has been changed', inst.id); // => model with id 1 has been changed }); ```
Parameters:
Name Type Description
data Object
Properties:
Name Type Description
Model.modelName String The name of the model. Static property.
Model.dataSource DataSource Data source to which the model is connected, if any. Static property.
Model.sharedMethod SharedClass The `strong-remoting` [SharedClass](http://apidocs.strongloop.com/strong-remoting/#sharedclass) that contains remoting (and http) metadata. Static property.
settings Object Contains additional model settings.
Properties
Name Type Description
http.path string Base URL of the model HTTP route.
acls Array.<Object> Array of ACLs for the model.
Source:

Methods

(static) createOptionsFromRemotingContext(ctx) → {Object}

Create "options" value to use when invoking model methods via strong-remoting (e.g. REST). Example ```js MyModel.myMethod = function(options, cb) { // by default, options contains only one property "accessToken" var accessToken = options && options.accessToken; var userId = accessToken && accessToken.userId; var message = 'Hello ' + (userId ? 'user #' + userId : 'anonymous'); cb(null, message); }); MyModel.remoteMethod('myMethod', { accepts: { arg: 'options', type: 'object', // "optionsFromRequest" is a loopback-specific HTTP mapping that // calls Model's createOptionsFromRemotingContext // to build the argument value http: 'optionsFromRequest' }, returns: { arg: 'message', type: 'string' } }); ```
Parameters:
Name Type Description
ctx Object A strong-remoting Context instance
Source:
Returns:
The value to pass to "options" argument.
Type
Object

(static) disableRemoteMethod(name, isStatic)

Disable remote invocation for the method with the given name.
Parameters:
Name Type Description
name String The name of the method.
isStatic Boolean Is the method static (eg. `MyModel.myMethod`)? Pass `false` if the method defined on the prototype (eg. `MyModel.prototype.myMethod`).
Source:

(static) disableRemoteMethodByName(name)

Disable remote invocation for the method with the given name.
Parameters:
Name Type Description
name String The name of the method (include "prototype." if the method is defined on the prototype).
Source:

(static) remoteMethod(name, options)

Enable remote invocation for the specified method. See [Remote methods](http://loopback.io/doc/en/lb2/Remote-methods.html) for more information. Static method example: ```js Model.myMethod(); Model.remoteMethod('myMethod'); ```
Parameters:
Name Type Description
name String The name of the method.
options Object The remoting options. See [Remote methods - Options](http://loopback.io/doc/en/lb2/Remote-methods.html#options).
Source:

(static) setup()

The `loopback.Model.extend()` method calls this when you create a model that extends another model. Add any setup or configuration code you want executed when the model is created. See [Setting up a custom model](http://loopback.io/doc/en/lb2/Extending-built-in-models.html#setting-up-a-custom-model).
Source: