11

Using Waterline ORM from SailsJS, my defaults for autoCreatedAt and autoUpdatedAt are set to false, but I still need to implement to the functionality just using different field names (DBA request). Is there a way to either:

  1. specify different column names for the automatically generated field, or
  2. manually emulate the same behave in an attribute definition, or
  3. can I just leave custom fields in the schema like created_ts and updated_ts to be updated with triggers in the DB schema itself (but I still need Waterline to read them)?

Thanks in advance.

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Andrew Eddie
  • 988
  • 6
  • 15
  • 2
    possible duplicate of [Change field name for CreatedAt / UpdateAt Attributes](http://stackoverflow.com/questions/24560942/change-field-name-for-createdat-updateat-attributes) – sgress454 Jul 28 '14 at 04:06
  • Actually, the linked answer was slightly different question. Here, we wish to know whether the DB column names can be altered, not the domain object attribute names (which was asked in the linked answer). It might or might not be the same answer, that is what I now wish to know... – arcseldon Sep 23 '14 at 23:49
  • 1
    The name of the auto-generated fields is hardcoded in the source, so they cannot be changed. – Andrew Eddie Sep 24 '14 at 03:53
  • Alright, I'm a sails/waterline user and I think it should have been done a while ago, so I'm going to open a pull-request for this. I will post an answer when I'm done! – Tristan Foureur Apr 11 '15 at 12:33

1 Answers1

18

I just opened two pull requests to implement this feature;

  • One to waterline-schema so that the schema builder takes this into account
  • One to waterline so that the timestamp behaviour works as expected.

You can also follow this issue.

With this merged, in your model, instead of having for example :

autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
    creationDate: {
        columnName: 'created_ts',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    },
    updateDate: {
        columnName: 'updated_ts',
        type: 'datetime',
        defaultsTo: function() {return new Date();}
    }
},
beforeUpdate:function(values,next) {
    values.updateDate = new Date();
    next();
}

You can just do :

autoCreatedAt: 'created_ts',
autoUpdatedAt: 'updated_ts'
baao
  • 71,625
  • 17
  • 143
  • 203
Tristan Foureur
  • 1,647
  • 10
  • 23