From 4233568e50ef09fa928501050f078a7e86e569ef Mon Sep 17 00:00:00 2001 From: Shahar Hadas Date: Thu, 11 Feb 2016 00:45:11 -0500 Subject: [PATCH] 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 8548003..7ff3181 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 69913f0..74c1d42 100644 --- a/time-stamp.js +++ b/time-stamp.js @@ -20,15 +20,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 }); @@ -43,6 +55,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();