Page Contents

Extending models using JSON

When you create a model with the model generator, you choose a base model, that is, the model that your model will “extend” and from which it will inherit methods and properties. The tool will set the  base property in the model definition JSON file accordingly. For example, for a model that extends PersistedModel:


  "name": "Order",
  "base": "PersistedModel",

To change the base model, simply edit the JSON file and change the base property.

In general, use PersistedModel as the base model when you want to store your data in a database using a connector such as MySQL or MongoDB. Use Model as the base for models that don’t have CRUD semantics, for example, using connectors such as SOAP and REST.

See Customizing models for general information on how to create a model that extends (or “inherits from”) another model.

See LoopBack types for information on data types supported.

Extending a model in JavaScript

You can also extend models using JavaScript file in the model JavaScript file, /common/models/_modelName_.js (where modelName is the name of the model); for example:


var properties = {
  firstName: {
    type: String,
    required: true

var options = {
  relations: {
    accessTokens: {
      model: accessToken,
      type: hasMany,
      foreignKey: userId
    account: {
      model: account,
      type: belongsTo
    transactions: {
      model: transaction,
      type: hasMany
  acls: [{
    permission: ALLOW,
    principalType: ROLE,
    principalId: $everyone,
    property: myMethod

var user = loopback.Model.extend('user', properties, options);

See LoopBack types for information on data types supported.

Mixing in model definitions

You may want to create models that share a common set of properties and logic. LoopBack enables you to “mix-in” one or more other models into a single model. This is a special case of the general ability to mix in model properties and functions. See Defining mixins for more information.

For example:


var TimeStamp = modelBuilder.define('TimeStamp', {
  created: Date,
  modified: Date
var Group = modelBuilder.define('Group', {
  groups: [String]
User.mixin(Group, TimeStamp);

Setting up a custom model

You may want to perform additional setup for an custom model, such as adding remote methods of another model. To do so, implement a setup() method on the new model. The loopback.Model.extend() function calls setup() so code you put in setup() will automatically get executed when the model is created.

For example: 


MyModel = Model.extend('MyModel');

MyModel.on('myEvent', function() {
  console.log('meep meep!');

MyExtendedModel = MyModel.extend('MyExtendedModel');

MyModel.emit('myEvent'); // nothing happens (no event listener)

// this is where `setup()` becomes handy

MyModel.setup = function() {
  var MyModel = this;
  // since setup is called for every extended model
  // the extended model will also have the event listener
  MyModel.on('myEvent', function() {