Sails 10.x waterline:用于Mongo objectID的属性类型

时间:2014-09-05 20:08:35

标签: sails.js waterline sails-mongo

sailsjs:我正在尝试定义一个模型。我想添加一个属性vendorID。类型将是vendor集合中的monogdb objectID。 像商店模型的东西: module.exports ={ attributes :{ vendorId : { type: <Monog ObjectId>}, <-- this would be a FK to the vendor Collection storeName: {type: 'string'} .... }

水线文件说:

目前提供以下属性类型:

  • 字符串
  • 文本
  • 整数
  • 日期
  • 时间
  • 日期时间
  • 布尔
  • 二进制
  • 阵列
  • JS​​ON

那我该选哪个?

由于

2 个答案:

答案 0 :(得分:4)

您应该查看SailsJS associations。使用水线,您不需要直接处理id类型。只需通过modelcollection属性创建指向其他集合的属性。

这是Sails / Waterline文档中的一个简单示例。

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

}

答案 1 :(得分:0)

水线为您自动创建_id,您无需这样做。