See also:
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:
- With the LoopBack model generator.
- From an existing relational database using model discovery. Then you can keep your model synchronized with the database using LoopBack’s schema / model synchronization API.
- By instance introspection for free-form data in NoSQL databases or REST APIs.
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
Warning: In model JavaScript files (for example, for a “foo” model, common/models/foo.js
) you cannot access model relations since models are are not yet loaded.
You must perform relation operations in boot scripts.
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:
module.exports = function(Customer) {
// Customer object is available
//...
}
Promises
LoopBack also supports Promises in addition to callbacks for CRUD methods of a model and its related models.
In a boot script
In a boot script, use the app.models
object to get a reference to any model; for example:
module.exports = function(app) {
var User = app.models.user;
var Role = app.models.Role;
var RoleMapping = app.models.RoleMapping;
var Team = app.models.Team;
//...
}