node.js - Sails js,我们如何使用 populate 编写内连接

标签 node.js sails.js sails-postgresql

Sails 的填充工作方式与左连接相同。我想做内连接,

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

最佳答案

例如,如果您有两个模型用户、联系人 一对多关系 你尝试写一些类似的东西:-

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

用户.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'
}]
}

关于node.js - Sails js,我们如何使用 populate 编写内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37790797/

相关文章:

javascript - 在策略中过滤通知

node.js - Sails 拒绝与 sails-postgresql 模块的 PostgreSQL 连接

node.js - 在 Express Node.js 中获取 cookie connect.sid 键的值

node.js - 应如何设置高需求应用程序的 node.js 堆栈?

node.js - 在 Node js中使用私钥连接ssh服务器

orm - 在 Sails Controller 中的操作之前

javascript - 在 Sails.js 中从 PostgreSQL 获取 BYTEA

javascript - Node.js,Express : Cannot set headers after they are sent to the client

node.js - Sequelize finder 排除了属性但想要所有属性