I work on an application link to a MySQL DB. I have different models, and i want to used a model in another model.js.
for example i have 2 models :
Server.json
{
"name": "server",
"base": "PersistedModel",
"idInjection": false,
"properties": {
"idserver": {
"type": "number",
"id": true
},
"Name": {
"type": "string"
},
"type": {
"type": "string"
},
"cpus": {
"type": "number"
},
"memory": {
"type": "number"
},
"storage": {
"type": "number"
},
"hypervisor": {
"type": "number"
},
"iddatastore": {
"type": "number"
},
"comment": {
"type": "string"
}
},
"validations": [],
"relations": {
"datastores": {
"type": "hasAndBelongsToMany",
"model": "Datastore"
}
},
"acls": [],
"methods": []
}
And Datastore.json
{
"name": "Datastore",
"plural":"Datastores",
"base": "PersistedModel",
"idInjection": false,
"properties": {
"iddatastore": {
"type": "number",
"id": true
},
"owner": {
"type": "number"
},
"size": {
"type": "number"
},
"type": {
"type": "string"
},
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"servers": {
"type": "hasAndBelongsToMany",
"model": "server"
}
},
"acls": [],
"methods": []
}
and i would like to get the data from Datastore in Server.js.
i try this way but it doesn't work well :
Server.js
var loopback = require('loopback');
var app = module.exports = loopback();
module.exports = function(Server) {
Server.on('attached',function(){
var override = Server.find;
var Datastore = Server.app.models.Datastore;
// the problem is here. It seems to return the description of the model when i look for the data
Server.findById = function(filter,callback){
var id = arguments[0];
// this isn't working
console.log(Datastore.find({where:{"idserver":id}}));
return override.apply(this, arguments);
};
});
};
I know it is possible by different way but if it is possible i prefer get the data from Datastore !
Sorry for my english if it is not clear, Do you have any idea ?
Loïc