I have a model like this -
{
  "name": "MakeCallTestConfiguration",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "id": {
      "type": "number",
      "id": true,
      "generated": true
    },
    "destination": {
      "type": "string",
      "required": true
    },
    "clientId": {
      "type": "number",
      "required": true
    },
    "logTime":{
      "type" : "date",
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}
For "logTime", how can I have auto generated time stamp? I mean something like
"TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"
You can use the "$now" attribute
"logTime":{
  "type": "date",
  "required": true,
  "default": "$now"
}
Update your model JSON file and change the logTime field to this
"logTime": {
    "type": "date",
    "dataType": "timestamp",
    "defaultFn": "now"
}
That will do.
Use a model hook (ie. beforeCreate) and set the date there. Here is an example using a remote hook (because I don't have an example using a model hook) - https://github.com/strongloop/loopback-getting-started-intermediate/blob/master/common/models/review.js#L4
See the model hooks doc for more info: http://docs.strongloop.com/display/LB/Model+hooks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With