You can create models with the model generator, by "discovery" from existing an existing database schema, and by instance introspection for non-relational data sources.
Page Contents

Overview

You can create LoopBack models in various ways, depending on what kind of data source the model is based on. You can create models:

All three of these methods create a  Model definition JSON file that defines your model in LoopBack, by convention in a LoopBack project’s common/models directory; for example, common/models/account.json.

You can also create and customize models programmatically using the  LoopBack API, or by manually editing the  model definition JSON file. In most cases, you shouldn’t need to use those techniques to create models, but you generally will use them to modify and customize models.

Getting a reference to a model in JavaScript

The way that you get a reference (or “handle”) to a model in JavaScript code depends on where the code is.

In model JavaScript file

In the model JavaScript file, the model is passed into the top-level function, so the model object is available directly; for example for a “customer” model:

/common/models/customer.js

module.exports = function(Customer) {
  // Customer object is available 
  //...
}

In a boot script

In a boot script, use the app.models object to get a reference to any model; for example:

/server/boot/script.js

module.exports = function(app) {
  var User = app.models.user;
  var Role = app.models.Role;
  var RoleMapping = app.models.RoleMapping;
  var Team = app.models.Team;
  //...
}
Tags: models