node.js – Sails JS水线连接多个模型

嗨我正在尝试使用populate方法连接多个表,我用Google搜索并找不到
这样做的有效方法,我不想多次查询db来构建结果,是否有可能用sails版本“~0.10.0-rc7”来解决它我正在用超过一百个表构建退出大项目.

var co = {
    adapter: 'someMysqLServer',migrate:'safe',autocreatedAt: false,autoUpdatedAt: false,autopK:false,attributes:{
        id:{
            type:"int",primaryKey: true,autoIncrement: true
        },code:"string",priority :"int",co_group_c_id :"int",timezone_c_id :"int",lang_c_id :"int",currency_c_id :"int",name_used :"string",name_official :"string",co_tax_no :"int",co_vat_no :"int",co_vat_reg :"int",co_reg_record :"string",co_representative :"string",co_addresses:{
            collection: "co_address",via: "co_user_id"
        },}
};
module.exports = co;

var co_address = {
    adapter: 'someMysqLServer',attributes: {
        id:{
            type:"int",autoIncrement: true,},address_c_id:"int",address_street_first: "string",address_street_second: "int",address_street_third: "int",address_postalcode: "string",address_city: "string",co_user_id: {
            model: 'co_user'
        },co_id: {
            model: 'co'
        },co_address_opening_hours:{
            collection: "co_address_opening_hours",via: "co_address_id"
        },}
};
module.exports = co_address;

var co_address_opening_hours = {
    adapter: 'someMysqLServer',day_c_id: "int",time_from: "datetime",time_to :"datetime",co_address_id: {
            model: 'co_address'
        }
    }
};
module.exports = co_address_opening_hours;

//controller
    get_co:function(req,res){
        co.find()
            .populate("co_addresses")
            .populate("co_address_opening_hours")
            .exec(function(e,company) {
                if(e) console.log(e);
                console.log(company);
                res.json(company);
    })

解决方法

在SailsJS 0.10中,您可以使用模型关联来执行数据库连接.您可以在这里阅读更多相关信息: http://sailsjs.com/documentation/concepts/models-and-orm/associations

基本上,您首先在模型中定义关联;

var someModel = {
  attributes: {
    name: {
      type: 'text'
    }
  }
};

var someOtherModel = {
  attributes: {
    name: {
      type: 'text'
    },associationProp: {
      model: 'someModel'
    }
  }
};

在上面的代码中,someOtherModel包含someModel的关联(关系).要执行连接查询,可以使用.populate()方法.例如,检索所有someOtherModel实体并填充关联属性;

someOtherModel.find().populate('associationProp').exec(...);

对于MysqL和Psql适配器,还有.query()方法,您可以编写一些手写的SQL查询来执行(这也适用于sails< 0.10);

Model.query(<sql query>,<optional data>,callback);

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...