Change field name for CreatedAt / UpdateAt Attributes

前端 未结 3 1291
小蘑菇
小蘑菇 2020-12-30 05:59

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 gene

相关标签:
3条回答
  • 2020-12-30 06:15

    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.

    0 讨论(0)
  • 2020-12-30 06:21

    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.

    0 讨论(0)
  • 2020-12-30 06:32

    You could remap the following way:

    attributes: {
        createdAt: {
          type: 'datetime',
          columnName: 'cre_dt'
        },
        updatedAt: {
          type: 'datetime',
          columnName: 'upd_dt'
        },
        // your other columns
      }
    
    0 讨论(0)
提交回复
热议问题