Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js - One to Many mapping

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

  }

};
like image 939
Samuel Poirier Avatar asked Aug 22 '13 21:08

Samuel Poirier


2 Answers

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"
    },

  }

};
like image 102
Luja Shrestha Avatar answered Nov 15 '22 15:11

Luja Shrestha


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.

like image 35
RoryKoehein Avatar answered Nov 15 '22 15:11

RoryKoehein