strongloop:隐藏 PersistedModel 上的默认方法

strongloop: hide default method on PersistedModel

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

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

到目前为止,我无法同时满足这两个要求:如果我将模型设置为 public,它会附带映射到 标准[=23] 的整套内置方法=] 路由,如果我将其设置为非 public,甚至我的自定义遥控器也会被隐藏。

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

我终于得出了这个片段:

在我的模型文件中

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);
};

我回家它帮助别人

您要查找的函数是:

MyModel.disableRemoteMethod(name, [isStatic])

这是API docs

不幸的是,您需要单独禁用每个方法...

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

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

面向 public 的模型将是基础 class Model 而非 public 模型仍然是 PersistedModel none 暴露其端点。您可以通过在 MyPublicModel.js 文件中调用 MyPublicModel.app.models.MyPrivateModel 来访问 "Private" 模型的功能。

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