From 40f03f084b3462fe795d0fe2b8d6e8805620c981 Mon Sep 17 00:00:00 2001 From: Clark Wang Date: Mon, 24 Nov 2014 19:20:38 +0800 Subject: [PATCH 1/3] fix recursive calls if create belongsTo model in beforeCreate hook Signed-off-by: Clark Wang --- lib/relation-definition.js | 11 ++++++++--- test/relations.test.js | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 38f5552c..216a7431 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1192,11 +1192,16 @@ BelongsTo.prototype.create = function(targetModelData, cb) { modelTo.create(targetModelData, function(err, targetModel) { if(!err) { modelInstance[fk] = targetModel[pk]; - modelInstance.save(function(err, inst) { - if (cb && err) return cb && cb(err); + if (modelInstance.isNewRecord()) { self.resetCache(targetModel); cb && cb(err, targetModel); - }); + } else { + modelInstance.save(function(err, inst) { + if (cb && err) return cb && cb(err); + self.resetCache(targetModel); + cb && cb(err, targetModel); + }); + } } else { cb && cb(err); } diff --git a/test/relations.test.js b/test/relations.test.js index 9bcbc390..4d68e2c3 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1657,6 +1657,23 @@ describe('relations', function () { }); }); + it('should allow to create belongsTo model in beforeCreate hook', function (done) { + var mind; + Fear.beforeCreate = function (next) { + this.mind.create(function (err, m) { + mind = m; + if (err) next(err); else next(); + }); + }; + Fear.create(function (err, fear) { + should.not.exists(err); + should.exists(fear); + fear.mindId.should.be.equal(mind.id); + should.exists(fear.mind()); + done(); + }); + }); + }); describe('belongsTo with scope', function () { @@ -1680,7 +1697,10 @@ describe('relations', function () { p.personId.should.equal(person.id); person.name.should.equal('Fred'); person.passportNotes.should.equal('Some notes...'); - done(); + p.save(function (err, passport) { + should.not.exists(err); + done(); + }); }); }); From 2e98c51571d2455ead729fe0bd67d757d17bfda0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 15 Jan 2015 08:45:52 +0100 Subject: [PATCH 2/3] Fix detection of `util.inspect` version Improve the detection to work in browser too. This fixes a regression introduced in e4fc3878. --- lib/model.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/model.js b/lib/model.js index 916e09d5..543af043 100644 --- a/lib/model.js +++ b/lib/model.js @@ -441,8 +441,9 @@ ModelBaseClass.prototype.reset = function () { // Node v0.11+ allows custom inspect functions to return an object // instead of string. That way options like `showHidden` and `colors` // can be preserved. -var versionParts = process.versions.node - .split(/\./g).map(function(v) { return +v; }); +var versionParts = process.versions && process.versions.node ? + process.versions.node.split(/\./g).map(function(v) { return +v; }) : + [1, 0, 0]; // browserify ships 1.0-compatible version of util.inspect var INSPECT_SUPPORTS_OBJECT_RETVAL = versionParts[0] > 0 || From 825b4d5ba3d621ab838b749fb63978f9374a6487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 15 Jan 2015 08:53:57 +0100 Subject: [PATCH 3/3] v2.14.1 --- CHANGES.md | 47 ++++++++++++++++++++++++++++++++--------------- package.json | 2 +- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ab5092e0..dddfeb2b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,11 @@ +2015-01-15, Version 2.14.1 +========================== + + * Fix detection of `util.inspect` version (Miroslav Bajtoš) + + * fix recursive calls if create belongsTo model in beforeCreate hook (Clark Wang) + + 2015-01-14, Version 2.14.0 ========================== @@ -321,16 +329,9 @@ * Emit deleted event on delete for embedsMany relations (Jaka Hudoklin) - * Make sure falsy value is kept for properties not predefined (Raymond Feng) - * Add ping() to test connections (Raymond Feng) -2014-08-21, Version 2.5.1 -========================= - - - 2014-08-21, Version 2.5.2 ========================= @@ -338,6 +339,12 @@ * Make sure falsy value is kept for properties not predefined (Raymond Feng) + +2014-08-21, Version 2.5.1 +========================= + + * Bump version (Raymond Feng) + * Fix side-effects of PR #237 - see #242 (Fabien Franzen) @@ -902,14 +909,6 @@ * JSDoc improvements (Rand McKinney) - * validations: include more details in `err.message` (Miroslav Bajtoš) - - -2014-05-27, Version 1.5.4 -========================= - - * Bump version (Raymond Feng) - * Keep undefined/null values for the array type (Raymond Feng) * Remove JSDocs for scopeMethods.add(acInst) and scopeMethods.remove(acInst) (crandmck) @@ -945,6 +944,24 @@ * Remove remoting metadata (Ritchie Martori) +2014-05-27, Version 1.5.4 +========================= + + * Bump version (Raymond Feng) + + * Keep undefined/null values for the array type (Raymond Feng) + + * Remove JSDocs for scopeMethods.add(acInst) and scopeMethods.remove(acInst) (crandmck) + + * Copy info from api-model.md to JSDoc (crandmck) + + * validations: include more details in `err.message` (Miroslav Bajtoš) + + * Add a path to show customer.orders(query, cb) (Raymond Feng) + + * Add support for logical operator (AND/OR) (Raymond Feng) + + 2014-05-20, Version 1.5.2 ========================= diff --git a/package.json b/package.json index fecf1d69..aaf40176 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-datasource-juggler", - "version": "2.14.0", + "version": "2.14.1", "description": "LoopBack DataSoure Juggler", "keywords": [ "StrongLoop",