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
    }
    ...
}
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
JCKortlang
  • 227
  • 2
  • 9

3 Answers3

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.

A T
  • 13,008
  • 21
  • 97
  • 158
sgress454
  • 24,870
  • 4
  • 74
  • 92
  • 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? – Andrew Eddie Jul 30 '14 at 01:18
  • 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) – Naor Biton Sep 09 '14 at 04:04
  • This should have been `beforeUpdate`--surprised nobody caught that before now! – sgress454 Sep 09 '14 at 05:18
  • @sgress454 - 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? – arcseldon Sep 23 '14 at 23:52
  • 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. – arcseldon Sep 23 '14 at 23:53
  • 2
    @sgress454 the columnName can now be overridden without disabling default support in v0.11.0 of Waterline (0.12.0 of Sails) – mikermcneil Feb 05 '16 at 20:16
18

You could remap the following way:

attributes: {
    createdAt: {
      type: 'datetime',
      columnName: 'cre_dt'
    },
    updatedAt: {
      type: 'datetime',
      columnName: 'upd_dt'
    },
    // your other columns
  }
Askar
  • 5,784
  • 10
  • 53
  • 96
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.