嵌入式文档&有没有办法使用"键入"关键字作为模型字段名称

时间:2014-07-24 14:14:45

标签: sails.js waterline

这个问题看起来有点荒谬。但如果有办法,这对于字段名称标准化来说是很好的。

  • 我可以使用“type”关键字作为字段名吗?

我已将字段名称更改为“方法”(另一种选择是“种类”)。但它可能是地址的子文档字段。例如:

address: { 
     type: { 
              type: 'string'
           }
         }

我的模型如下;

payment: {
    type: {
        type: 'string'
    },
    tally_system: {
        installment_count: {
            type: 'integer'
        }
    },
    gift_card: {
        type: 'string'
    },
    total_amount: {
        type: 'integer'
    },
    discount_ratio: {
        type: 'integer'
    },
    total_amount_after_discount: {
        type: 'float'
    }
}

修改

我可以在Sails中查询嵌入文档,如下所示。我认为,极有可能我可以在没有Waterline的情况下手动插入嵌入式文档并且很痛苦。 希望新的,Waterline提供嵌入式使用。

Bid.native(function(err, collection) {     
  collection
  .find({'_id' : req.param('id') })
  .nextObject(function (err, bid) {                        
     console.log(bid);
  });
});

1 个答案:

答案 0 :(得分:2)

Sails不支持嵌入式文档的模式,因此您无法执行以下操作:

tally_system: {
    installment_count: {
        type: 'integer'
    }
}

并期望它们以您想要的方式工作。你能做的最好的是:

tally_system: {
    type: "json"
}

将声明它是一个“json”字段,您可以将任意Javascript对象放入:

MyModel.create({ tally_system: [1,2,{abc:123}] })

话虽如此,你可以拥有一个名为“type”的字段而不会出现问题。