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"

5 Answers 5

15

You can use the "$now" attribute

"logTime":{
  "type": "date",
  "required": true,
  "default": "$now"
}
1
  • 6
    What is the difference between the $now and now?
    – Radmation
    Jun 28, 2018 at 17:44
5

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

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

That will do.

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

7
  • 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 Jan 23, 2015 at 5:06
  • 1
    Did you try "logTime": {"type": "date", "dataType": "timestamp" }?
    – superkhau
    Jan 23, 2015 at 7: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. Jan 23, 2015 at 9:13
  • Which connector are you using? MySQL?
    – superkhau
    Jan 23, 2015 at 18:50
  • 1
    I asked one of my colleagues and he is saying you can have db specific defaults. See this PR github.com/strongloop/loopback-connector-postgresql/pull/54
    – superkhau
    Jan 23, 2015 at 22:26
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

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
  .
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.