【问题标题】:strongloop: hide default method on PersistedModelstrongloop:隐藏 PersistedModel 上的默认方法
【发布时间】:2016-08-09 03:06:37
【问题描述】:

我有一个模型定义为PersistedModel,因此与 mongodb 集合有关。我想实现以下目标:

  1. 默认的crud方法被隐藏
  2. 公开并映射到路由GET /(默认为myModel.find())的自定义远程方法

到目前为止,我无法满足这两个要求:如果我将模型设置为 public,它会附带映射在 standard 路由上的整套内置方法,如果我将其设置为非-public 甚至我的自定义遥控器都被隐藏了。

【问题讨论】:

    标签: javascript mongodb strongloop


    【解决方案1】:

    你要找的功能是:

    MyModel.disableRemoteMethod(name, [isStatic])
    

    这里是API docs

    很遗憾,您需要单独禁用每个方法...

    另一种解决方案是创建两个不同的模型,一个是公共的,一个不是(您可以在 server/model-config.json 文件中将 Public 布尔值更改为 false。您不需要将“公共”模型附加到数据源.

    "MyPublicModel": {
      "dataSource": null,
      "public": true
    },
    "MyPrivateModel": {
      "datasource": "db",
      "public": false
    }
    

    面向公众的模型将是基类 Model,而非公共模型仍将是 PersistedModel,没有任何端点暴露。您可以通过在MyPublicModel.js 文件中调用MyPublicModel.app.models.MyPrivateModel 来访问“私有”模型的​​功能。

    (如果您需要任何澄清,请发表评论)。

    【讨论】:

    • 感谢分享。通过定义 disableAllMethodsButRemotes 函数,我个人对以下选项感到满意
    • 哦,太好了,没想到是你找到了解决方法!
    【解决方案2】:

    我通过这个 ol'dirty 解决方法解决了。我在这个地址找到了它:https://github.com/strongloop/loopback/issues/651

    我终于拿出了这个sn-p:

    在我的模型文件中

    module.exports = function(myModel) {
        utils.disableAllMethodsButRemotes(myModel);
    };
    

    在我的utils.js 库文件中:

    exports.disableAllMethodsBut = function(model, methodsToExpose) {
        if (model && model.sharedClass) {
            methodsToExpose = methodsToExpose || [];
    
            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');
            }
        }
    };
    
    exports.disableAllMethodsButRemotes = function disableAllMethodsBut(model) {
        var remotes = Object.keys(model.definition.settings.methods || {});
        return exports.disableAllMethodsBut(model, remotes);
    };
    

    我家它可以帮助某人

    【讨论】:

      猜你喜欢
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多