javascript Sails.js - 一对多映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18391309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:44:03  来源:igfitidea点击:

Sails.js - One to Many mapping

javascriptnode.jsmappingone-to-manysails.js

提问by Samuel Ptheitroadier

Is there any way to do relation mapping between models in Sails.js?

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

Here is what I would like:

这是我想要的:

Video.js:

视频.js:

module.exports = {

  attributes: {

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

};

And in my User.js:

在我的 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

  }

};

回答by Luja Shrestha

You can use associations in sailsJs now by using the v0.10 branch https://stackoverflow.com/a/21822843/1585332
The mapping would be something like this..

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

Video.js

视频.js

module.exports = {

  attributes: {

    filename: 'STRING',
    length: 'INTEGER',
    watchCount: 'INTEGER',
    extension: 'STRING'
    user:{
      model: "user"
    }
  }

};

User.js

用户.js

module.exports = {

  attributes: {

    username: {
        type: 'email',
        required: true
    },
    password: 'STRING',
    videos:{
      collection: "video",
      via: "user"
    },

  }

};

回答by RoryKoehein

Sails.js doesn't support association yet, but they're working on it: https://github.com/balderdashy/sails/issues/124#issuecomment-21690561

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

Also see: How to perform SQL Joins and Relations in Sails.js and Waterline?

另请参阅:如何在 Sails.js 和 Waterline 中执行 SQL 联接和关系?

For now I would just reference ID's and/or use the .query() method.

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