Change field name for CreatedAt / UpdateAt Attributes

JCKortlang picture JCKortlang · Jul 3, 2014 · Viewed 8.4k times · Source

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
    }
    ...
}

Answer

sgress454 picture sgress454 · Jul 3, 2014

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.