2

sailsjs: I am trying to define a model. I would like to add a property vendorID. The type would be the monogdb objectID from the vendor collection. Something like for a store model: module.exports ={ attributes :{ vendorId : { type: <Monog ObjectId>}, <-- this would be a FK to the vendor Collection storeName: {type: 'string'} .... }

Waterline docu says:

The following attribute types are currently available:

  • string
  • text
  • integer
  • float
  • date
  • time
  • datetime
  • boolean
  • binary
  • array
  • json

So what do I pick?

Thanks

tomatom
  • 419
  • 4
  • 9

2 Answers2

4

You should look into SailsJS associations. With waterline you shouldn't need to deal directly with id types. Just create an attribute that points to another collection via the model or collection properties.

Here's a simple example from the Sails/Waterline docs.

//Pet.js - A Pet may only have a single user
module.exports = {

    attributes: {
        name:'STRING',
        color:'STRING',
        owner:{
            model:'user'
        }
    }

}

//User.js - A user may have multiple pets
module.exports = {

    attributes: {
        name:'STRING',
        age:'INTEGER',
        pets:{
            collection: 'pet',
            via: 'owner'
        }
    }

}
Jason Kulatunga
  • 5,814
  • 1
  • 26
  • 50
  • that works. I noticed the ObjectID gets converted into a string in waterline. So I can use the string as reference in other collections too. – tomatom Sep 08 '14 at 18:27
0

The _id is created automatically for you by Waterline you don't have to do this.

mdunisch
  • 3,627
  • 5
  • 25
  • 41