帆js,我们如何使用populate编写内连接

时间:2016-06-13 13:13:49

标签: node.js sails.js sails-postgresql

Sails的填充作为左连接。我想做内连接,

任何人都帮助我如何使用populate编写内部联接而无需编写原始查询。我正在使用MYSQL适配器。

1 个答案:

答案 0 :(得分:1)

如果您有两个模型用户,联系人 关系一对多  你试着写下这样的东西: -

第一个模型会喜欢这个: -

user.js

module.exports = {
  schema: true,
  tableName: 'userdata',
  attributes: {
    anyField: {
      type: 'integer',
      required: false,
      size: 56,
    },
    contacts: {
      collection: 'Contact',
      via: 'user'
    }

};

contact.js: -

module.exports = {

  attributes: {
    name: {
      type: 'string'
    },
        mobile_number: {
      type: 'string'
    },

    user: {
      model: 'User'
    }
};

你可以得到这样的填充结果: -

User.find(user.id).populate('contacts').exec(function (err, user) {
  // this contains  user object  and populated by contacts 
     console.log(user[0].contacts);
});

你得到的结果如下: -

{
name:'tets',
contacts:[{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
},
{
 'name':'test',
'mobile_number':'00000'
}]
}