如何在sails.js和waterline中执行嵌套连接?

时间:2014-08-24 09:44:07

标签: node.js sails.js waterline

sails.js中我有以下模型:

网站

module.exports = {
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    active: {
        type: 'boolean',
        defaultTo: false
    },
    pages: {
        collection: 'page',
        via 'site'
    }
  }
};

网页

module.exports = {
  attributes: {
    name: {
        type: 'string',
        required: true
    },
    site: {
        model: 'site',
        required: true
    },
    modules: {
        collection: 'module',
        via 'page'
    }
  }
};

模块

module.exports = {
  attributes: {
    module: {
        type: 'string',
        required: true
    },
    page: {
        model: 'page',
        required: true
    }
  }
};

当我致电 GET / Site / 1 时,我得到以下内容:

{
    "pages": [
        {
            "name": "First page",
            "site": 1,
            "createdAt": "2014-08-23T17:57:41.562Z",
            "updatedAt": "2014-08-23T17:57:41.562Z",
            "id": 1
        }
    ],
    "name": "First site",
    "createdAt": "2014-08-23T17:56:57.143Z",
    "updatedAt": "2014-08-23T17:56:57.143Z",
    "id": 1
}

我使用MongoDB,这很容易作为嵌套文档进行建模..不幸的是,我不认为Waterline支持它,因此关联/加入。

我可以看到它成功输出了与网站相关的每个页面,我该如何制作它以便它还输出与每个页面相关的模块列表?

1 个答案:

答案 0 :(得分:3)

This thread可能有所帮助。从本质上讲,还没有直接的方法来获取嵌套关联。

相关问题