【问题标题】:Populating multiple tables in sails waterline orm在sails waterline orm中填充多个表
【发布时间】:2016-04-18 15:21:42
【问题描述】:

我正在开发一个包含多个 (>2) 表的 Sails 应用程序,我需要借助 populate 方法 加入这些表 例如

Category.js 模型

attributes: {
    CategoryID:{
        type:"integer",
        required:true,
        primaryKey:true,
        autoIncrement:true
    },
    SubCategories:{                  //REFERING TO SUB-CATEGORY TABLE
        collection:'SubCategory',
        via:'CategoryID'
    },
    CategoryName:{
        type:"string",
        required:true,
        maxLength:50
    }
  }

这是 SubCategory.js 模型

attributes: {
    id:{
        type:'integer',
        required:true,
        primaryKey:true,
        autoIncrement:true,
        maxLength:10,
        columnName:'SubCategoryID'
    },
    CategoryID:{
        model:'Category'                 //REFERING TO CATEGORY TABLE
    },
    ProductsOfCategory:{                  //REFERING TO PRODUCT TABLE
        collection:'Product',
        via:'SubCategoryID'
    },
    SubCategory:{
        type:"string",
        required:true,
        maxLength:50
    }
}

Product.js 模型

attributes: {
    id: {
        type: 'integer',
        primaryKey: true,
        autoIncrement: true,
        maxLength: 10,
        columnName:'ProductID'
    },
    SubCategoryID: {
        model:'SubCategory'
    },
    ProductDetails:{
        collection:'ProductDetails',
        via:'ProductID'
    },
    ProductName: {
        type: "string",
        required: true,
        maxLength: 50
    }
}

ProductDeutils.js 模型

attributes: {
    id: {
        type: 'integer',
        primaryKey: true,
        autoIncrement: true,
        maxLength: 10,
        columnName:'ProductDetailID'
    },
    ProductID:{
        model:'Product'
    },
    Size:{
        type:"string",
        required:true,
        maxLength:10
    },
    Color:{
        type:"string",
        required:true,
        maxLength:10
    }
}

在填充时,我可以填充每个类别的类别和子类别。

Category.find()
        .populate('SubCategories')
        .exec(function(err, categories){
            if (err) {
                console.log(err);
                return res.json(err);
            }
            console.log(categories);
            res.json(categories);
        })

如何一次性填充上述所有表格,以便在最终查询后我们在一个 json 中获得上述所有详细信息。

我们得到以上所有表格的连接

即具有所有子类别的类别,具有所有产品的子类别和所有产品在一个 json 中具有产品详细信息

【问题讨论】:

  • ashishkumar - 我的回答对您有帮助吗?如果您觉得有帮助,请您标记为正确答案。还链接到一个(有点过时但非常相似的)SOF 问题。

标签: mysql sails.js waterline


【解决方案1】:

你问了一个很好的问题。 大量对将嵌套填充功能引入到风帆中,实际上是数十个问题请求和 PR 等。

看看这里的一些历史:

[FEATURE REQUEST] Recursively populate #308 - 我迟到了,在 2014 年 10 月 29 日提出请求,正如你将在历史中看到的那样。

据我所知,大多数对话最终都集中在这里(经过几年的 Sails 用户请求该功能):

Deep populate #1052(截至写作 2016 年 1 月 14 日,问题仍然开放

从该问题的状态来看,我们目前所处的位置尚不清楚。这两个链接的历史确实表明了其他人使用的替代解决方法。

我的预感是不支持开箱即用的递归填充。

当使用 SailsJS 的水线模型关联时,我所做的是使用像 async.js 这样的包 - 使用类似瀑布的东西以编程方式显式填充子关系。您可以将此与覆盖您调用的模型的默认 toJSON() 相结合,以将它们的关系(您以编程方式填充)添加到 JSON 响应中。您同样可以选择使用内置的 Promise 来实现相同的目标。

找到这个(日期,2014 年)SOF Question,它提供了更多信息。

如果我在最近的 Sails 或 Waterline 版本中错过了此功能添加,请在此处纠正我 - 在任何一个项目的发行说明中都找不到任何说明此功能受支持的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2017-03-22
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多