Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change field name for CreatedAt / UpdateAt Attributes

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
    }
    ...
}
like image 430
JCKortlang Avatar asked Jul 03 '14 18:07

JCKortlang


3 Answers

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.

like image 117
sgress454 Avatar answered Nov 12 '22 21:11

sgress454


You could remap the following way:

attributes: {
    createdAt: {
      type: 'datetime',
      columnName: 'cre_dt'
    },
    updatedAt: {
      type: 'datetime',
      columnName: 'upd_dt'
    },
    // your other columns
  }
like image 33
Askar Avatar answered Nov 12 '22 20:11

Askar


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.

like image 1
Jesse Hoobergs Avatar answered Nov 12 '22 20:11

Jesse Hoobergs