how to use the attribute type like 'array' and 'json' in sails

user2590223 picture user2590223 · Dec 21, 2013 · Viewed 7.4k times · Source

Sails have support very convenient model througth Waterline, and I have used the 'array' attribute type in the way storing many string, but now I want to store more complex object, Although I can store the raw data in mongo by 'array' type, I don't know if it is safe and I want define the object type in the array, like mongoose's style. for example, I need a model "Products" and I want define it as a 'array' but the object stored in the array only model "Book", what I can do like this, but I don't think it works.

module.exports = {
  products : {
    type : 'array',
    Book : {
      name : 'string',
      price : 'integer'
    }
  }
}

So, any suggestion about use of the 'array' and 'json' is very appreciated, thanks so much!

Answer

sfb picture sfb · Jan 2, 2014

I don't think that the array type is going to work the way you want it to. What you're looking for is associations which will be available in SailsJS 0.10 (which you can get via git right now). The array attribute type for MySQL and PostgreSQL it will only stringify the array and store it as text in the database then it will parse the field when returning the value.

source