在风帆水线 orm 中填充多个表

Populating multiple tables in sails waterline orm

我正在开发一个包含多个(>2)tables 的 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
    }
}

ProductDeatils.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
    }
}

在填充时,我能够填充每个类别的类别和 sub-category。

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

如何一次性填充以上所有内容table,以便在最终查询后我们一次性获得以上所有详细信息json。

我们得到以上所有的连接tables

即拥有所有sub-categories、sub-category所有产品和所有产品的产品详细信息在一个json

你问了一个很好的问题。人们 大量 对将嵌套填充功能放入 sails 感兴趣,实际上有数十个问题请求和 PR 等

在这里看看一些历史:

[FEATURE REQUEST] Recursively populate #308 - 我迟到了,在 2014 年 10 月 29 日提出请求,您将在历史记录中看到。

据我所知,大多数对话最终汇聚于此(经过几年的 Sails 用户请求该功能):

Deep populate #1052(问题仍然 未解决 截至撰写 2016 年 1 月 14 日

从我们所处的那个问题的状态来看还不清楚。这两个链接的历史确实表明其他人使用过的替代解决方法。

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

当我使用 SailsJS 的水线模型关联时,我所做的是使用像 async.js - use something like waterfall to explicitly populate the child relationships programmatically. You can combine doing this with overriding the default toJSON() 这样的包,你调用的模型将它们的关系(你已经以编程方式填充)添加到 JSON回复。您同样可以选择使用 built-in 承诺来实现相同的目的。

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

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