Last active
October 12, 2018 09:52
-
-
Save ratik/5252e4c168a8c29329c0 to your computer and use it in GitHub Desktop.
LoopbackJs disable all remote methods and enable only selected
This file contains 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 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": "Some", | |
"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
Please, give the proper credits. I posted most of this code here
strongloop/loopback#651 (comment)