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/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 || diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 80b9dca0..504eb939 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1193,11 +1193,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/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", diff --git a/test/relations.test.js b/test/relations.test.js index f6cd3680..7d8bed84 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(); + }); }); });