This module is designed for the Strongloop Loopback framework. It automatically adds
updatedAt attributes to any Model.
createdAt will be set to the current Date the by using the default property of the attribute.
updatedAt will be set for every update of an object through bulk
updateAll or instance
npm i loopback-ds-timestamp-mixin --save
With version 2.33.2 of this module the upsert validation was turned off. This may create issues for your project if upsert validation is required. If you require upsert validation, set the
validateUpsert option to true, however most upserts will fail unless you supply the
updatedAt fields or set
required option to false.
mixins property to your
To use with your Models add the
mixins attribute to the definition object of your model config.
The attribute names
updatedAt are configurable. To use different values for the default attribute names add the following parameters to the mixin options.
You can also configure whether
updatedAt are required or not. This can be useful when applying this mixin to existing data where the
required constraint would fail by default.
By setting the
validateUpsert option to true you will prevent this mixin from overriding the default Model settings. With validation turned on most upsert operations will fail with validation errors about missing the required fields like
This mixin uses console logs to warn you whenever something might need your attention. If you would prefer not to receive these warnings, you can disable them by setting the option
false on a per model basis.
In this example we change
updatedOn, respectively. We also change the default
false and set
validateUpsert to true. We also disable console warnings with
NOTE for database MySQL and Postgres options
When you use database options for MySQL and the like beware that you may have to use the
columnName value configured for the database instead of the loopback configured name.
In the following example for the
Widget object your
createdAt field should equal the
columnName which would be
Thus the configuration looks like this for the above example.
Please see issue #19 for more information on database options.
By passing in additional options to an update or save operation you can control when this mixin updates the
updatedAt field. The passing true to the option
skipUpdatedAt will skip updating the
In this example we assume a book object with the id of 2 already exists. Normally running this operation would change the
updatedAt field to a new value.
Source files are located in the
es6 directory. Edit the source files to make changes while running
For error checking and to help maintain style this package uses
eslint as a pretest. All test are run against the transformed versions of files, not the es6 versions.
Run the tests in the
Run with debugging output on:
DEBUG='loopback:mixins:time-stamp' npm test