Once you've created a model with the model generator (slc loopback:model
), you can start customizing it. You can customize it using slc, by editing the Model definition JSON file, and by adding JavaScript code.
Customizing a model with slc
You can use slc
to customize a model after you initially create it; specifically, you can:
- Use
slc loopback:property
to add a property to the model. See Property generator for more information. - Use
slc loopback:relation
to add add relations beteween models. See Relation generator for more information. - Use slc loopback:acl to add access control to the model. See ACL generator for more information.
Customizing a model using JSON
You can customize a number of aspects of a model by simply editing the model definition JSON file in /common/models
(for example, customer.json
), which by default looks like this:
{ "name": "myModel", "base": "PersistedModel", "properties": { // Properties listed here depend on your responses to the CLI }, "validations": [], "relations": {}, "acls": [], "methods": [] }
Extending another model
You can make a model extend or "inherit from" an existing model, either one of the built-in models such as User, or a custom model you've defined in your application.
base
property
Edit the model JSON file and set the "base" property to the name of the model you want to extend: either one of the built-in models, or one of the custom models you've defined in the application.
For example, here is an excerpt from the customer.json
file from loopback-example-app that extends the built-in User model to define a new Customer model:
{ "name": "Customer", "base": "User", "idInjection": false, ...
MyModel
that extends from a custom model you defined called mMyBaseModel
, create MyModel using slc loopback:model
, then edit the JSON file common/models/MyModel.json
as follows:
{ "name": "Example", "base": "MyBaseModel", }
You can add new properties when you extend a model, for example:
{ "name": "Customer", "base": "User", "properties": { "favoriteMovie": { "type": "string" } } }
Customzing other model settings
Here are some of the most important settings you can customize:
- plural - set to a custom string value to use, instead of the default standard plural form.
- strict - set to true to make the model accept instances that have the predefined set of properties. False by default
- idInjection - Whether to automatically add an id property to the model. True by default.
- http.path - customized HTTP path of REST endpoints.
See Model definition JSON file for more information.
Customizing a model with JavaScript code
The basic way to extend a model programmatically is to edit the model's JavaScript file in the common/models/
directory. For example, a "customer" model will have a /common/models/customer.js
file (if you create the model using slc loopback:model
, the Model generator). The script is executed immediately after the model is defined. Treat the script as part of the model definition; use it for model configuration and registration. You could also add model relationships, complex validations, or default functions for certain properties: Basically, anything you cannot do in JSON. However, note that at this point the script doesn't have access to the app instance.
You can also extend a model by adding a remote method or a model hook.
If you don't want to expose the method over REST, then just omit the remoteMethod()
call.
See 添加应用逻辑 for more information on customizing a model using JavaScript.
Change the implementation of built-in methods
When you attach a model to a persistent data source, it becomes a persisted model that extends PersistedModel, and LoopBack automatically adds a set of built-in methods for CRUD operations. In some cases, you might want to change the implementation; use a JavaScript file in the /server/boot
directory to do this. For example, the following code shows how to reimplement Note.find()
to override the built-in find() method.
module.exports = function(app) { var Note = app.models.Note; var find = Note.find; var cache = {}; Note.find = function(filter, cb) { var key = ''; if(filter) { key = JSON.stringify(filter); } var cachedResults = cache[key]; if(cachedResults) { console.log('serving from cache'); process.nextTick(function() { cb(null, cachedResults); }); } else { console.log('serving from db'); find.call(Note, function(err, results) { if(!err) { cache[key] = results; } cb(err, results); });; } } }
- extending models
- overview
- what is model inheritance/extending a model
- depends if you're talking about built-in vs user-defined
- modifying model properties (built-ins and user-defined)
- extending built in models
- why/use case
- because you cannot modify built-in model properties
- solution is to extend the model and add in your custom properties
- can you override built in properties?
- if so describe the process
- caveats
- how to/example
- why/use case
- extending user-defined models
- extending models created from slc loopback:model
- example
- extending models created via discovery
- example
- extending models create via introspection
- example
- extending models created from slc loopback:model
- overview