-
-
Save ilumin/695d0fee2a98a7b13024edb16e3edb73 to your computer and use it in GitHub Desktop.
LoopbackJs disable all remote methods and enable only selected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Based on @ericprieto code https://github.com/strongloop/loopback/issues/651#issuecomment-140879983 | |
* place this file into common/mixins/disableAllMethods.js | |
* | |
**/ | |
module.exports = function(Model, options) { | |
if(Model && Model.sharedClass) { | |
var methodsToExpose = options.expose || []; | |
var modelName = Model.sharedClass.name; | |
var methods = Model.sharedClass.methods(); | |
var relationMethods = []; | |
var hiddenMethods = []; | |
try { | |
Object.keys(Model.definition.settings.relations).forEach(function(relation) { | |
relationMethods.push({ name: '__findById__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__destroyById__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__updateById__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__exists__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__link__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__get__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__create__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__update__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__destroy__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__unlink__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__count__' + relation, isStatic: false }); | |
relationMethods.push({ name: '__delete__' + relation, isStatic: false }); | |
}); | |
} catch(err) {} | |
methods.concat(relationMethods).forEach(function(method) { | |
var methodName = method.name; | |
if(methodsToExpose.indexOf(methodName) < 0) { | |
hiddenMethods.push(methodName); | |
Model.disableRemoteMethod(methodName, method.isStatic); | |
} | |
}); | |
// if(hiddenMethods.length > 0) { | |
// console.log('\nRemote mehtods hidden for', modelName, ':', hiddenMethods.join(', '), '\n'); | |
// } | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Model", | |
"base": "PersistedModel", | |
"idInjection": true, | |
"description":"", | |
"properties": { | |
"title": { | |
"type": "String", | |
"required": true, | |
"length": 1023, | |
"precision": null, | |
"scale": null, | |
"_selectable": false | |
} | |
}, | |
"validations": [], | |
"relations": {}, | |
"acls": [], | |
"methods": {}, | |
"mixins":{ | |
"DisableAllMethods":{ | |
"expose":[ | |
"findById" | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment