11

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"

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
Jahid Shohel
  • 1,395
  • 4
  • 18
  • 34

5 Answers5

15

You can use the "$now" attribute

"logTime":{
  "type": "date",
  "required": true,
  "default": "$now"
}
Emmanuel P.
  • 896
  • 2
  • 9
  • 16
5

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

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

That will do.

aslamdoctor
  • 3,753
  • 11
  • 53
  • 95
1

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

superkhau
  • 2,781
  • 18
  • 9
  • Hi, thanks, I am aware abiut model hooks. But I want to do that in db schema level, as I mentioned above like CURRENT_TIMESTAMP – Jahid Shohel Jan 23 '15 at 05:06
  • 1
    Did you try `"logTime": {"type": "date", "dataType": "timestamp" }`? – superkhau Jan 23 '15 at 07:21
  • I tried that before starting this thread, but if you see above declaration, you declaring data and its type, but there is no instruction about "by default use current time", which is done by "DEFAULT CURRENT_TIMESTAMP". Since strongloop is wrapping the ORM (object relation mapping), so there must be a way to pass this to DB layer. I know there are many ways to do this in upper layer using JS, but I want that to be done by DB. – Jahid Shohel Jan 23 '15 at 09:13
  • Which connector are you using? MySQL? – superkhau Jan 23 '15 at 18:50
  • 1
    I asked one of my colleagues and he is saying you can have db specific defaults. See this PR https://github.com/strongloop/loopback-connector-postgresql/pull/54 – superkhau Jan 23 '15 at 22:26
  • Yes, I am using mysql connector – Jahid Shohel Jan 23 '15 at 22:34
  • I tried this, but didnt work - "logTime":{ "type" : "date", "mysql": { "dataType": "timestamp", "dbDefault": "NOW()" } } – Jahid Shohel Jan 23 '15 at 22:42
0

Loopback have some neat default functions to meet exactly your need. In your Model.json file you can implement it. One of your "model's property properties" are the defaultFn, which you can set with the "now" attribute to solve your problem (see code sample).

As said in documentation:

"now": use the current date and time as returned by new Date()

Other cool and useful default functions are used to setup guids and uuids creation right on model definition file.

Code sample

...
"properties": {
   ...
    "logTime":{
      "type" : "date",
      "defaultFn": "now"
    }
}, 
...

With this setup, the default value for logTime will be set always to the current timestamp as required.

Reference

Loopback documentation are sometimes wry, but really pay off read the main parts and investigate further on the many examples they offer on github. For now, if you want to know more about defaultFn feature, this is the url you need:

http://loopback.io/doc/en/lb2/Model-definition-JSON-file.html#general-property-properties

luizv
  • 618
  • 1
  • 12
  • 22
0

If you take a look on the Loopback Documentation, you'll see if you specify date it's going to create a Javascript Date Object, no matter what. So if you want an Unix TimeStamp you can create a type number(or even String) on your model and then if you create using new Date() it's going to save as a timestamp. Here is an example:

Your model:

"logTime":{
  "type" : "number"
}

Your logic:

yourModel.logTime = new Date();

Your result:

{
  .
  "logTime": 1480437102036
  .
}
Marco Barcellos
  • 196
  • 2
  • 4