Loopback model CURRENT_TIMESTAMP
Asked Answered
N

5

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"

Narcolepsy answered 22/1, 2015 at 23:14 Comment(0)
R
15

You can use the "$now" attribute

"logTime":{
  "type": "date",
  "required": true,
  "default": "$now"
}
Roose answered 3/6, 2016 at 1:19 Comment(1)
What is the difference between the $now and now?Realistic
R
5

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

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

That will do.

Rafaelrafaela answered 11/12, 2017 at 4:51 Comment(0)
A
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

Abijah answered 23/1, 2015 at 0:51 Comment(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_TIMESTAMPNarcolepsy
Did you try "logTime": {"type": "date", "dataType": "timestamp" }?Abijah
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.Narcolepsy
Which connector are you using? MySQL?Abijah
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/54Abijah
Yes, I am using mysql connectorNarcolepsy
I tried this, but didnt work - "logTime":{ "type" : "date", "mysql": { "dataType": "timestamp", "dbDefault": "NOW()" } }Narcolepsy
D
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

Downhill answered 14/11, 2016 at 14:57 Comment(0)
C
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
  .
}
Capapie answered 29/11, 2016 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.