首页 文章

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

提问于
浏览 35 次
2

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

水线文件说:

目前提供以下属性类型:

  • 字符串

  • 文字

  • 整数

  • 浮动

  • 日期

  • 时间

  • datetime

  • 布尔值

  • 二进制

  • 数组

  • json

那我该怎么选?

谢谢

2 回答

  • 3

    你应该看看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'
            }
        }
    
    }
    
  • 0

    水线为您自动创建_id,您不必这样做 .

相关问题