From 03daaca9de47f81e571e8aabd2dbaf89481384c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 26 Feb 2015 11:15:31 +0100 Subject: [PATCH] Deprecate Model hooks List of deprecated hooks: - beforeValidate - afterValidate - beforeCreate - afterCreate - beforeSave - afterSave - beforeUpdate - afterUpdate - beforeDestroy - afterDestroy Also add a lightweight browser version of "depd", because the "depd" does not support browser and it is not trivial to fix that. This commits adds a lightweight implementation of depd's "deprecate" function. --- Makefile | 2 +- lib/browser.depd.js | 16 ++++++++++++++++ lib/hooks.js | 22 ++++++++++++++++++++++ package.json | 4 ++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/browser.depd.js diff --git a/Makefile b/Makefile index 0ab5736b..91f7f91d 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ OPTS = --growl TESTS = test/*.test.js test: - $(TESTER) $(OPTS) $(TESTS) + NO_DEPRECATION=loopback-datasource-juggler $(TESTER) $(OPTS) $(TESTS) test-verbose: $(TESTER) $(OPTS) --reporter spec $(TESTS) testing: diff --git a/lib/browser.depd.js b/lib/browser.depd.js new file mode 100644 index 00000000..32c074dd --- /dev/null +++ b/lib/browser.depd.js @@ -0,0 +1,16 @@ +// A lightweight alternative to "depd" that works in the browser +module.exports = function depd(namespace) { + var warned = {}; + return function deprecate(message) { + if (warned[message]) return; + warned[message] = true; + + if (process.noDeprecation) { + return; + } else if (process.traceDeprecation) { + console.trace(namespace, 'deprecated', message); + } else { + console.warn(namespace, 'deprecated', message); + } + }; +}; diff --git a/lib/hooks.js b/lib/hooks.js index acf69e4a..358c9df6 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -1,3 +1,5 @@ +var deprecated = require('depd')('loopback-datasource-juggler'); + /*! * Module exports */ @@ -39,6 +41,13 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback) } var inst = this; + if (actionName !== 'initialize') { + if (beforeHook) + deprecateHook(inst.constructor, ['before', 'pre'], capitalizedName); + if (afterHook) + deprecateHook(inst.constructor, ['after', 'post'], capitalizedName); + } + // we only call "before" hook when we have actual action (work) to perform if (work) { if (beforeHook) { @@ -71,3 +80,16 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback) function capitalize(string) { return string.charAt(0).toUpperCase() + string.slice(1); } + +function deprecateHook(ctor, prefixes, capitalizedName) { + var candidateNames = prefixes.map(function(p) { return p + capitalizedName; }); + if (capitalizedName === 'Validate') + candidateNames.push(prefixes[0] + 'Validation'); + + var hookName = candidateNames.filter(function(hook) { return !!ctor[hook]; })[0]; + if (!hookName) return; // just to be sure, this should never happen + if (ctor.modelName) hookName = ctor.modelName + '.' + hookName; + deprecated('Model hook "' + hookName + '" is deprecated, ' + + 'use Operation hooks instead. ' + + 'http://docs.strongloop.com/display/LB/Operation+hooks'); +} diff --git a/package.json b/package.json index 26e8ccad..7e077e5c 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,9 @@ "url": "https://github.com/strongloop/loopback-datasource-juggler" }, "main": "index.js", + "browser": { + "depd": "./lib/browser.depd.js" + }, "scripts": { "test": "make test" }, @@ -30,6 +33,7 @@ "dependencies": { "async": "^0.9.0", "debug": "^2.1.1", + "depd": "^1.0.0", "inflection": "^1.6.0", "lodash": "~3.0.1", "loopback-connector": "1.x",