tomatom
tomatom

Reputation: 469

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:

So what do I pick?

Thanks

Upvotes: 2

Views: 3178

Answers (2)

Jason Kulatunga
Jason Kulatunga

Reputation: 5894

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

}

Upvotes: 4

mdunisch
mdunisch

Reputation: 3697

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

Upvotes: 0

Related Questions