Change field name for CreatedAt / UpdateAt Attributes

陌路散爱 提交于 2019-11-29 06:45:21

问题


I am attempting to model an existing MSSQL table in SailsJS. Unsurprisingly the tables in the existing database have a createdAt and updatedAt column similar to what is generated by the SailsJS framework.

Is there a way to assign the value of the property generated by the SailsJS framework to an attribute that I have defined? For example:

attributes: {
    ...
    creationDate:{
        columnName:'cre_dt',
        type:'datetime',
        defaultsTo: this.createdAt
    }
    ...
}

回答1:


No, but you can turn off the auto-generated property entirely and use your own:

autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
    creationDate: {
        columnName: 'cre_dt',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    },
    updateDate: {
        columnName: 'upd_dt',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    }
},
//Resonsible for actually updating the 'updateDate' property.
beforeValidate:function(values,next) {
    values.updateDate= new Date();
    next();
}

See the Waterline options doc.




回答2:


You could remap the following way:

attributes: {
    createdAt: {
      type: 'datetime',
      columnName: 'cre_dt'
    },
    updatedAt: {
      type: 'datetime',
      columnName: 'upd_dt'
    },
    // your other columns
  }



回答3:


Now it's possible to use the autoCreatedAt and autoUpdatedAt to set a custom name for these fields as described at http://sailsjs.com/documentation/concepts/models-and-orm/model-settings#?autocreatedat

If set to a string, that string will be used as the custom field/column name for the createdAt attribute.



来源:https://stackoverflow.com/questions/24560942/change-field-name-for-createdat-updateat-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!