Search code examples
sails.jswaterlinesails-mongo

Sails 10.x waterline: What attribute type to use for Mongo objectID


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


Solution

  • 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'
            }
        }
    
    }