Reputation: 177
I have a persisted model in my loopback app and I want to automatically add createdAt
and updatedAt
attributes for this model instances accordingly. What is the easy way to achieve this?
Upvotes: 1
Views: 1485
Reputation: 177
In loopback 3 I have figured a great way to insert createdAt
and updatedAt
for a model using a mixin. The mixin name is loopback-ds-timestamp-mixin
and you can install its module using the command
npm i loopback-ds-timestamp-mixin --save
Then you can add the mixin in the mixins
property found in server/model-config.json
as the following
"mixins": [
"loopback/common/mixins",
"../node_modules/loopback-ds-timestamp-mixin",
"../common/mixins"
]
Finally, you can insert createdAt and updatedAt for a model by specifying
"Timestamp": true
in the mixins
property of the model json file.
You can also get more details about this mixin here.
Upvotes: 7