loopback-component-package
Version:
Package/Pricing management for loopback API server
59 lines (53 loc) • 1.79 kB
JavaScript
// model.js easly create a loopback3 model programatically.
var loopback = require('loopback');
var DataModel = loopback.PersistedModel || loopback.DataModel;
/**
* load a model from json
*
* @param {json} jsonFile - JSON spec of the model
* @return {object} returns DataModel instance
*/
var loadModel = function(jsonFile){
var modelDefinition = require(jsonFile);
return DataModel.extend(modelDefinition.name, modelDefinition.properties);
};
/**
* initalize a model loaded model
* registers it as a loopback model and attach it to a datasource
* @param {object} loopbackApp - loopback instance
* @param {object} js - js file of the model
* @param {json} json - JSON spec of the model
* @param {string} datasource - datasource for the model
* @param {bool} isPublic - true if the model is publicly accessable false otherwise
* @return {object} loopback model
*/
var initModel = function(loopbackApp,js,json,datasource,isPublic){
isPublic=(isPublic==null?true:isPublic);
var m;
if(js){
m= require(js)(loadModel(json));
}else{
m= loadModel(json);
}
loopbackApp.model(m,{dataSource:datasource,public:isPublic});
return m;
};
/**
* disable specific remote methods so that its not publicly accessable
* @param {object} model - loopback model
* @param {object} methodsToDisable - array of methods to disable
*/
var disableRemoteMethods = function(model,methodsToDisable){
if(model && model.sharedClass)
{
methodsToDisable = methodsToDisable||[];
for(var i=0;i<methodsToDisable.length;i++){
model.disableRemoteMethodByName(methodsToDisable[i]);
}
}
};
module.exports={
loadModel:loadModel,
initModel:initModel,
disableRemoteMethods:disableRemoteMethods
};