Documentation on loopback Doc (https://loopback.io/doc/en/lb3/Defining-models.html)
-
with model generator :
lb model MyModel
-
manuel via documentation (https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html)
- Edit file
server/model-config.json
and add
"MyModel": {
"dataSource": "[myDataSource]"
},
for create or update BDD schema with this new model, you can add you model on this file server/component-config.json
"loopback-component-auto-migrate": {
...
"models": [
...
"MyModel"
]
}
You must expose via REST with edit file server/model-config.json
"MyModel": {
"public": true
},
You can visualize new REST endpoint on Swagger
see documentation
- many-to-one => belongsTo
// road.json
"relations": {
"city": {
"type": "belongsTo",
"model": "City",
"foreignKey": "cityCode"
}
},
- one-to-many => hasMany
// city.json
"relations": {
"roads": {
"type": "hasMany",
"model": "Road",
"foreignKey": "cityCode"
}
},
Mixin is like 'hook' for add logic on Listener Models. see documentation (https://loopback.io/doc/en/lb3/Operation-hooks.html)
- Example of common models mixin
common/mixins.integrity-checks.js
, this mixin verify constraint on relations.
You can active for a model with add this lines on your my-model.json
...
"mixins": {
"IntegrityCheck": {}
},
...
- Example of custom model mixin
common/models.city.js
module.exports = function (City) {
City.observe('before save', (ctx, next) => {
if (ctx.instance) {
ctx.instance.nameMin = ctx.instance.name.toLowerCase();
} else {
ctx.data.nameMin = ctx.data.name.toLowerCase();
}
next();
});
};
- edit file
common/models/my-model.json
- add elements on section ACL (see doc)
- Sample
"acls": [
{ // All action Deny for All
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{ // Read action Allow for 'agent' role
"accessType": "READ",
"principalType": "ROLE",
"principalId": "agent",
"permission": "ALLOW"
},
{ // Read/Write action Allow for 'admin' role
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
}
...
],