在风帆水线 orm 中填充多个表 [英] Populating multiple tables in sails waterline orm

查看:58
本文介绍了在风帆水线 orm 中填充多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on a sails applications which contains multiple(>2) tables which I need to join with the help of populate method e.g.

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

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

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

On Populating, I am able to populate the category and sub-category of each category.

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 中包含产品详细信息

推荐答案

你提出了一个很好的问题.大量兴趣将嵌套填充功能放入帆中,实际上有数十个问题请求和 PR 等.

You ask a great question. There has been massive interest in getting nested populate feature into sails, literally tens of issue requests and PRs etc.

看看这里的一些历史:

[功能请求] 递归填充 #308 - 我参加聚会迟到了,您将在历史记录中看到的 2014 年 10 月 29 日提出请求.

[FEATURE REQUEST] Recursively populate #308 - i was late to the party, making the request on October 29th 2014 as you'll see in the history.

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

As far as I know, most conversations eventually converged here (after a couple of years of Sails users requesting the feature):

Deep populate #1052(问题仍然开放 撰写时 2016 年 1 月 14 日)

Deep populate #1052 (the issue remains open as of writing 14 Jan 2016)

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

It is unclear from the state of that Issue where we are. The history of both links does suggest alternative workarounds others have used.

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

My hunch is that recursive populate is not supported out of the box.

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

What I did when using waterline model associations with SailsJS, was work with a package like async.js - use something like waterfall to explicitly populate the child relationships programmatically. You can combine doing this with overriding the default toJSON() of the models you invoke to add their relationships (which you have programmatically populated) to the JSON response. You could equally choose to use the built-in promises to achieve the same thing.

发现此(日期为 2014 年)SOF 问题,其中提供了更多信息.

Found this (dated, 2014) SOF Question which offers more information.

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

Someone, do please correct me here if I have missed this feature addition in a recent Sails or Waterline version - couldn't find anything in the release notes for either project to say this was supported.

这篇关于在风帆水线 orm 中填充多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆