是否可以在sails.js / waterline中重命名`createdAt`和`updatedAt`

时间:2014-07-28 02:00:14

标签: node.js sails.js waterline

使用SailsJS中的Waterline ORM,autoCreatedAtautoUpdatedAt的默认值设置为false,但我仍然需要使用不同的字段名称(DBA请求)实现功能。有没有办法:

  1. 为自动生成的字段指定不同的列名称,或
  2. 在属性定义中手动模拟相同的行为,或
  3. 我可以在created_tsupdated_ts这样的架构中保留自定义字段,以便在数据库架构本身中使用触发器进行更新(但我仍然需要Waterline来阅读它们)?
  4. 提前致谢。

1 个答案:

答案 0 :(得分:18)

我刚开了两个拉请求来实现这个功能;

您也可以关注this issue

在你的模型中合并它,而不是例如:

autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
    creationDate: {
        columnName: 'created_ts',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    },
    updateDate: {
        columnName: 'updated_ts',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    }
},
beforeUpdate:function(values,next) {
    values.updateDate = new Date();
    next();
}

你可以这样做:

autoCreatedAt: 'created_ts',
autoUpdatedAt: 'updated_ts'