Is there any way to do relation mapping between models in Sails.js?
Here is what I would like:
Video.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:
module.exports = {
attributes: {
username: {
type: 'email',
required: true
},
password: 'STRING',
videos: // I would like to have an array of videos after querying a user
}
};
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..
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"
},
}
};
Sails.js doesn't support association yet, but they're working on it: https://github.com/balderdashy/sails/issues/124#issuecomment-21690561
Also see: How to perform SQL Joins and Relations in Sails.js and Waterline?
For now I would just reference ID's and/or use the .query() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With