【问题标题】:Sails.js - One to Many mappingSails.js - 一对多映射
【发布时间】:2013-08-25 19:48:04
【问题描述】:

有没有办法在 Sails.js 中做模型之间的关系映射?

这是我想要的:

视频.js:

module.exports = {

  attributes: {

    filename: 'STRING',
    length: 'INTEGER',
    watchCount: 'INTEGER',
    extension: 'STRING'
    user: // I wan to reference to my User.js model
  }

};

在我的 User.js 中:

module.exports = {

  attributes: {

    username: {
        type: 'email',
        required: true
    },
    password: 'STRING',
    videos: // I would like to have an array of videos after querying a user

  }

};

【问题讨论】:

    标签: javascript node.js mapping one-to-many sails.js


    【解决方案1】:

    您现在可以通过 v0.10 分支在sailsJs 中使用关联 https://stackoverflow.com/a/21822843/1585332
    映射将是这样的......

    视频.js

    module.exports = {
    
      attributes: {
    
        filename: 'STRING',
        length: 'INTEGER',
        watchCount: 'INTEGER',
        extension: 'STRING'
        user:{
          model: "user"
        }
      }
    
    };
    

    用户.js

    module.exports = {
    
      attributes: {
    
        username: {
            type: 'email',
            required: true
        },
        password: 'STRING',
        videos:{
          collection: "video",
          via: "user"
        },
    
      }
    
    };
    

    【讨论】:

      【解决方案2】:

      Sails.js 还不支持关联,但他们正在努力: https://github.com/balderdashy/sails/issues/124#issuecomment-21690561

      另见:How to perform SQL Joins and Relations in Sails.js and Waterline?

      现在我只想引用 ID 和/或使用 .query() 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多