2013-08-22 69 views
10

有没有什么办法可以在Sails.js中的模型之间进行关系映射?Sails.js - 一对多映射

这里是我想什么:

的Video.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 

    } 

}; 

回答

22

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

的Video.js

module.exports = { 

    attributes: { 

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

}; 

user.js的

module.exports = { 

    attributes: { 

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

    } 

};