Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback model CURRENT_TIMESTAMP

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"

like image 722
Jahid Shohel Avatar asked Jan 22 '15 23:01

Jahid Shohel


3 Answers

You can use the "$now" attribute

"logTime":{
  "type": "date",
  "required": true,
  "default": "$now"
}
like image 121
Emmanuel P. Avatar answered Nov 07 '22 21:11

Emmanuel P.


Update your model JSON file and change the logTime field to this

"logTime": {
    "type": "date",
    "dataType": "timestamp",
    "defaultFn": "now"
}

That will do.

like image 25
aslamdoctor Avatar answered Nov 07 '22 23:11

aslamdoctor


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

like image 1
superkhau Avatar answered Nov 07 '22 23:11

superkhau