本文介绍了是否可以在sails.js/waterline中重命名`createdAt`和`updatedAt`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SailsJS 的 Waterline ORM,autoCreatedAtautoUpdatedAt 的默认设置为 false,但我仍然需要使用不同的字段名称(DBA要求).有没有办法:

Using Waterline ORM from SailsJS, my defaults for autoCreatedAt and autoUpdatedAt are set to false, but I still need to implement to the functionality just using different field names (DBA request). Is there a way to either:

  1. 为自动生成的字段指定不同的列名,或
  2. 在属性定义中手动模拟相同的行为,或
  3. 我可以在架构中保留自定义字段,例如 created_tsupdated_ts 以使用数据库架构本身中的触发器进行更新(但我仍然需要 Waterline 来读取它们)?
  1. specify different column names for the automatically generated field, or
  2. manually emulate the same behave in an attribute definition, or
  3. can I just leave custom fields in the schema like created_ts and updated_ts to be updated with triggers in the DB schema itself (but I still need Waterline to read them)?

提前致谢.

推荐答案

我刚刚开了两个 pull request 来实现这个功能;

I just opened two pull requests to implement this feature;

  • One to waterline-schema 以便架构构建器考虑到这一点
  • 一到水线,以便时间戳行为按预期工作.
  • One to waterline-schema so that the schema builder takes this into account
  • One to waterline so that the timestamp behaviour works as expected.

您也可以关注本期.

合并后,在您的模型中,而不是例如:

With this merged, in your model, instead of having for example :

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'

这篇关于是否可以在sails.js/waterline中重命名`createdAt`和`updatedAt`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:17