From 71c75734f3d01405487d6e9b01ed4326a6b27861 Mon Sep 17 00:00:00 2001 From: Shahar Hadas Date: Thu, 11 Feb 2016 00:45:11 -0500 Subject: [PATCH 1/4] Possible fix for https://github.com/clarkbw/loopback-ds-timestamp-mixin/pull/10 --- README.md | 3 ++- package.json | 2 +- time-stamp.js | 30 +++++++++++++++++++++++++----- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 580fd41..a79112c 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ In this example we change `createdAt` and `updatedAt` to `createdOn` and `update "TimeStamp" : { "createdAt" : "createdOn", "updatedAt" : "updatedOn", - "required" : false + "required" : false, + "disableAllValidateUpsert" : true } } } diff --git a/package.json b/package.json index 752bbbe..079659d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-ds-timestamp-mixin", - "version": "3.2.2", + "version": "3.2.3", "description": "A mixin to automatically generate created and updated Date attributes for loopback Models", "main": "index.js", "scripts": { diff --git a/time-stamp.js b/time-stamp.js index 12edc88..133e307 100644 --- a/time-stamp.js +++ b/time-stamp.js @@ -19,15 +19,27 @@ exports['default'] = function (Model) { debug('TimeStamp mixin for Model %s', Model.modelName); - options = _extends({ createdAt: 'createdAt', updatedAt: 'updatedAt', required: true }, options); + options = _extends({ createdAt: 'createdAt', updatedAt: 'updatedAt', required: true, disableAllValidateUpsert: false }, options); debug('options', options); - debug('Model.settings.validateUpsert', Model.settings.validateUpsert); - if (Model.settings.validateUpsert && options.required) { - console.warn('TimeStamp mixin requires validateUpsert be false. See @clarkbw/loopback-ds-timestamp-mixin#10'); + if (options.disableAllValidateUpsert) { + console.warn('%s.settings.validateUpsert was overriden to false', Model.pluralModelName); + Model.settings.validateUpsert = false; + } else { + + // Check for PersistedModel static method + try { + Model.exists({id: null}, function (err, exists) { + // Continue normally + }); + } + catch(err) { + if (Model.settings.validateUpsert && options.required) { + console.warn('TimeStamp mixin requires validateUpsert be false in models not based on PersistedModel, override with disableAllValidateUpsert. See @clarkbw/loopback-ds-timestamp-mixin#10'); + } + } } - Model.settings.validateUpsert = false; Model.defineProperty(options.createdAt, { type: Date, required: options.required, defaultFn: 'now' }); Model.defineProperty(options.updatedAt, { type: Date, required: options.required }); @@ -42,6 +54,14 @@ exports['default'] = function (Model) { ctx.instance[options.updatedAt] = new Date(); } else { debug('%s.%s before update matching %j', ctx.Model.pluralModelName, options.updatedAt, ctx.where); + + if (ctx.currentInstance && ctx.currentInstance[options.createdAt]) { + debug('currentInstance.%s timestamp reused', options.createdAt); + ctx.data[options.createdAt] = ctx.currentInstance[options.createdAt]; + } else { + ctx.data[options.createdAt] = new Date(); + } + ctx.data[options.updatedAt] = new Date(); } next(); From 6f9d0b0c1cded81b56b1281915ab229905423782 Mon Sep 17 00:00:00 2001 From: Shahar Hadas Date: Tue, 16 Feb 2016 17:37:02 -0500 Subject: [PATCH 2/4] Some speed optimization by 1st trying Model.settings.base to try and identify PersistedModel --- time-stamp.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/time-stamp.js b/time-stamp.js index 133e307..a1acb18 100644 --- a/time-stamp.js +++ b/time-stamp.js @@ -25,19 +25,22 @@ exports['default'] = function (Model) { if (options.disableAllValidateUpsert) { console.warn('%s.settings.validateUpsert was overriden to false', Model.pluralModelName); - Model.settings.validateUpsert = false; + Model.settings.validateUpsert = false; } else { - // Check for PersistedModel static method - try { - Model.exists({id: null}, function (err, exists) { - // Continue normally + // Check base model + if (Model.settings.base != "PersistedModel") { + // Check for PersistedModel static method + try { + Model.exists({ id: null }, function (err, exists) { + // Continue normally }); - } - catch(err) { + } + catch (err) { if (Model.settings.validateUpsert && options.required) { - console.warn('TimeStamp mixin requires validateUpsert be false in models not based on PersistedModel, override with disableAllValidateUpsert. See @clarkbw/loopback-ds-timestamp-mixin#10'); + console.warn('TimeStamp mixin requires validateUpsert be false in models not based on PersistedModel, override with disableAllValidateUpsert. See @clarkbw/loopback-ds-timestamp-mixin#10'); } + } } } @@ -54,14 +57,14 @@ exports['default'] = function (Model) { ctx.instance[options.updatedAt] = new Date(); } else { debug('%s.%s before update matching %j', ctx.Model.pluralModelName, options.updatedAt, ctx.where); - + if (ctx.currentInstance && ctx.currentInstance[options.createdAt]) { debug('currentInstance.%s timestamp reused', options.createdAt); ctx.data[options.createdAt] = ctx.currentInstance[options.createdAt]; } else { ctx.data[options.createdAt] = new Date(); } - + ctx.data[options.updatedAt] = new Date(); } next(); From f5557fc5b311941ac29e28495c0606a2b9a32f99 Mon Sep 17 00:00:00 2001 From: Shahar Hadas Date: Tue, 16 Feb 2016 17:54:05 -0500 Subject: [PATCH 3/4] Clear some jshint warnings. --- time-stamp.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/time-stamp.js b/time-stamp.js index a1acb18..8690d94 100644 --- a/time-stamp.js +++ b/time-stamp.js @@ -4,7 +4,17 @@ Object.defineProperty(exports, '__esModule', { value: true }); -var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; +var _extends = Object.assign || function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + if (Object.prototype.hasOwnProperty.call(source, key)) { + target[key] = source[key]; + } + } + } + return target; +}; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } @@ -19,7 +29,11 @@ exports['default'] = function (Model) { debug('TimeStamp mixin for Model %s', Model.modelName); - options = _extends({ createdAt: 'createdAt', updatedAt: 'updatedAt', required: true, disableAllValidateUpsert: false }, options); + options = _extends({ + createdAt: 'createdAt', + updatedAt: 'updatedAt', + required: true, + disableAllValidateUpsert: false }, options); debug('options', options); @@ -29,7 +43,7 @@ exports['default'] = function (Model) { } else { // Check base model - if (Model.settings.base != "PersistedModel") { + if (Model.settings.base != 'PersistedModel') { // Check for PersistedModel static method try { Model.exists({ id: null }, function (err, exists) { @@ -38,7 +52,8 @@ exports['default'] = function (Model) { } catch (err) { if (Model.settings.validateUpsert && options.required) { - console.warn('TimeStamp mixin requires validateUpsert be false in models not based on PersistedModel, override with disableAllValidateUpsert. See @clarkbw/loopback-ds-timestamp-mixin#10'); + console.warn('TimeStamp mixin requires validateUpsert be false in models not based on PersistedModel.' + + 'Override with disableAllValidateUpsert. See @clarkbw/loopback-ds-timestamp-mixin#10'); } } } From aac80ea869e283f05e08242eadeee2faba28ab59 Mon Sep 17 00:00:00 2001 From: Shahar Hadas Date: Mon, 29 Feb 2016 22:24:48 -0500 Subject: [PATCH 4/4] Modified to allow disabling createdAt and updatedAt by setting them to null. --- time-stamp.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/time-stamp.js b/time-stamp.js index 8690d94..0386ece 100644 --- a/time-stamp.js +++ b/time-stamp.js @@ -29,10 +29,10 @@ exports['default'] = function (Model) { debug('TimeStamp mixin for Model %s', Model.modelName); - options = _extends({ - createdAt: 'createdAt', - updatedAt: 'updatedAt', - required: true, + options = _extends({ + createdAt: 'createdAt', + updatedAt: 'updatedAt', + required: true, disableAllValidateUpsert: false }, options); debug('options', options); @@ -41,7 +41,7 @@ exports['default'] = function (Model) { console.warn('%s.settings.validateUpsert was overriden to false', Model.pluralModelName); Model.settings.validateUpsert = false; } else { - + // Check base model if (Model.settings.base != 'PersistedModel') { // Check for PersistedModel static method @@ -59,8 +59,12 @@ exports['default'] = function (Model) { } } - Model.defineProperty(options.createdAt, { type: Date, required: options.required, defaultFn: 'now' }); - Model.defineProperty(options.updatedAt, { type: Date, required: options.required }); + if (typeof options.createdAt !== 'undefined' && options.createdAt !== null) { + Model.defineProperty(options.createdAt, { type: Date, required: options.required, defaultFn: 'now' }); + } + if (typeof options.updatedAt !== 'undefined' && options.updatedAt !== null) { + Model.defineProperty(options.updatedAt, { type: Date, required: options.required }); + } Model.observe('before save', function (ctx, next) { debug('ctx.options', ctx.options); @@ -69,22 +73,30 @@ exports['default'] = function (Model) { } if (ctx.instance) { debug('%s.%s before save: %s', ctx.Model.modelName, options.updatedAt, ctx.instance.id); - ctx.instance[options.updatedAt] = new Date(); + if (typeof options.updatedAt !== 'undefined' && options.updatedAt !== null) { + ctx.instance[options.updatedAt] = new Date(); + } } else { debug('%s.%s before update matching %j', ctx.Model.pluralModelName, options.updatedAt, ctx.where); - if (ctx.currentInstance && ctx.currentInstance[options.createdAt]) { - debug('currentInstance.%s timestamp reused', options.createdAt); - ctx.data[options.createdAt] = ctx.currentInstance[options.createdAt]; - } else { - ctx.data[options.createdAt] = new Date(); + if (typeof options.createdAt !== 'undefined' && options.createdAt !== null) { + if (ctx.currentInstance && ctx.currentInstance[options.createdAt]) { + debug('currentInstance.%s timestamp reused', options.createdAt); + ctx.data[options.createdAt] = ctx.currentInstance[options.createdAt]; + } else { + ctx.data[options.createdAt] = new Date(); + } } - ctx.data[options.updatedAt] = new Date(); + if (typeof options.updatedAt !== 'undefined' && options.updatedAt !== null) { + ctx.data[options.updatedAt] = new Date(); + } } next(); }); + }; + module.exports = exports['default']; -//# sourceMappingURL=time-stamp.js.map \ No newline at end of file +//# sourceMappingURL=time-stamp.js.map