Chinese Documentation : Creating models


Overview

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

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 (for example) the models is passed into the top-level function, so the model object is available directly; for example:

/common/models/model.js
module.exports = function(Customer) {
  Customer.create( ... );  // Customer object is available 
  ...
REVIEW COMMENT from Rand
Can you get a reference to any other models? How?

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