问题
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:
- specify different column names for the automatically generated field, or
- manually emulate the same behave in an attribute definition, or
- can I just leave custom fields in the schema like
created_ts
andupdated_ts
to be updated with triggers in the DB schema itself (but I still need Waterline to read them)?
Thanks in advance.
回答1:
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'
来源:https://stackoverflow.com/questions/24987551/is-it-possible-to-rename-createdat-and-updatedat-in-sails-js-waterline