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.
This commit is contained in:
Miroslav Bajtoš 2015-02-26 11:15:31 +01:00
parent 6891da4535
commit 03daaca9de
4 changed files with 43 additions and 1 deletions

View File

@ -5,7 +5,7 @@ OPTS = --growl
TESTS = test/*.test.js TESTS = test/*.test.js
test: test:
$(TESTER) $(OPTS) $(TESTS) NO_DEPRECATION=loopback-datasource-juggler $(TESTER) $(OPTS) $(TESTS)
test-verbose: test-verbose:
$(TESTER) $(OPTS) --reporter spec $(TESTS) $(TESTER) $(OPTS) --reporter spec $(TESTS)
testing: testing:

16
lib/browser.depd.js Normal file
View File

@ -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);
}
};
};

View File

@ -1,3 +1,5 @@
var deprecated = require('depd')('loopback-datasource-juggler');
/*! /*!
* Module exports * Module exports
*/ */
@ -39,6 +41,13 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback)
} }
var inst = this; 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 // we only call "before" hook when we have actual action (work) to perform
if (work) { if (work) {
if (beforeHook) { if (beforeHook) {
@ -71,3 +80,16 @@ Hookable.prototype.trigger = function trigger(actionName, work, data, callback)
function capitalize(string) { function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1); 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');
}

View File

@ -16,6 +16,9 @@
"url": "https://github.com/strongloop/loopback-datasource-juggler" "url": "https://github.com/strongloop/loopback-datasource-juggler"
}, },
"main": "index.js", "main": "index.js",
"browser": {
"depd": "./lib/browser.depd.js"
},
"scripts": { "scripts": {
"test": "make test" "test": "make test"
}, },
@ -30,6 +33,7 @@
"dependencies": { "dependencies": {
"async": "^0.9.0", "async": "^0.9.0",
"debug": "^2.1.1", "debug": "^2.1.1",
"depd": "^1.0.0",
"inflection": "^1.6.0", "inflection": "^1.6.0",
"lodash": "~3.0.1", "lodash": "~3.0.1",
"loopback-connector": "1.x", "loopback-connector": "1.x",