Change field name for CreatedAt / UpdateAt Attributes
Asked Answered
B

3

14

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
    }
    ...
}
Bost answered 3/7, 2014 at 18:30 Comment(0)
J
22

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.

Jubilee answered 3/7, 2014 at 20:1 Comment(6)
Scott, if you are using the schema to enforce the behaviour, for example created_ts timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, would you have to use an afterUpdate handler to load the resource back out of the DB and bind the data back to the resource?Fouquiertinville
The 'afterUpdate' hook appears to occur after the update query has already been executed, so your code doesn't update the updateDate. The way I made this work is to use the 'afterValidate' hook. It looks to me like the most suitable hook to place this code in. (This is written while using Sails 0.10)Mourning
This should have been beforeUpdate--surprised nobody caught that before now!Jubilee
@Jubilee - Is it possible to just supply a different columnName to be used in the DB (date_created, last_updated), while leaving the domain entity attribute names the same (createdAt, updatedAt). Or do we have to go through the above boilerplate for every domain object in our model just to override the column name?Diploid
Model inheritance in Sails 0.10.5 using the recommended approach of lodash and merge / deepClone is broken due to the introduction of associations. So I would be copy / pasting the above across 20+ domain objects which is really nasty.Diploid
@Jubilee the columnName can now be overridden without disabling default support in v0.11.0 of Waterline (0.12.0 of Sails)Sauls
T
18

You could remap the following way:

attributes: {
    createdAt: {
      type: 'datetime',
      columnName: 'cre_dt'
    },
    updatedAt: {
      type: 'datetime',
      columnName: 'upd_dt'
    },
    // your other columns
  }
Tripe answered 25/9, 2015 at 7:21 Comment(0)
M
1

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.

Morbidity answered 14/8, 2017 at 8:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.