From b9b6f04acd24b9d8c5acf7ef6a87ac8397e75a18 Mon Sep 17 00:00:00 2001 From: linusbrolin Date: Tue, 27 Sep 2016 18:31:45 +0200 Subject: [PATCH 1/4] Fixed bug where the reference_fields were used as id, when they are not supposed to be This caused different schemas to use the same counter, despite having different id's for the counters, but the same reference_fields. --- lib/sequence.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/sequence.js b/lib/sequence.js index 321e548..7098274 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -99,7 +99,9 @@ Sequence.prototype._getCounterReferenceField = function(doc) { reference = null; }else { for (var i in this._options.reference_fields) { - reference.push(JSON.stringify(doc[this._options.reference_fields[i]])); + reference.push({ + [this._options.reference_fields[i]]: JSON.stringify(doc[this._options.reference_fields[i]]) + }); } } @@ -161,7 +163,7 @@ Sequence.prototype._setHooks = function() { } referenceValue = _this._getCounterReferenceField(doc); - _this._setNextCounter(_this._options.reference_fields, referenceValue, function(err, seq) { + _this._setNextCounter(_this._options.id, referenceValue, function(err, seq) { if (err) return done(err); doc[_this._options.inc_field] = seq; done(); From 44d02fa46dff2865a196056f5b8d8ea46571b110 Mon Sep 17 00:00:00 2001 From: linusbrolin Date: Tue, 27 Sep 2016 21:20:52 +0200 Subject: [PATCH 2/4] fixed warning in test --- lib/sequence.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sequence.js b/lib/sequence.js index 7098274..57615c7 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -100,7 +100,7 @@ Sequence.prototype._getCounterReferenceField = function(doc) { }else { for (var i in this._options.reference_fields) { reference.push({ - [this._options.reference_fields[i]]: JSON.stringify(doc[this._options.reference_fields[i]]) + [this._options.reference_fields[i]]: JSON.stringify(doc[this._options.reference_fields[i]]) }); } } From 2dcf512dd87b701fa984b98aede69295ac82ffe0 Mon Sep 17 00:00:00 2001 From: linusbrolin Date: Tue, 27 Sep 2016 21:29:10 +0200 Subject: [PATCH 3/4] new attempt at fixing test warnings --- lib/sequence.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/sequence.js b/lib/sequence.js index 57615c7..3fc7ec1 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -32,7 +32,7 @@ Sequence = function(schema, options) { if (_.isNull(options.reference_fields)) { options.reference_fields = options.inc_field; this._useReference = false; - }else { + } else { this._useReference = true; } @@ -41,7 +41,7 @@ Sequence = function(schema, options) { if (this._useReference === true && _.isNull(options.id)) { throw new Error('Cannot use reference fields without specifying an id'); - }else { + } else { options.id = options.id || options.inc_field; } @@ -61,7 +61,7 @@ Sequence.getInstance = function(schema, options) { var sequence = new Sequence(schema, options), id = sequence.getId(); - if(sequenceArchive.existsSequence(id)){ + if (sequenceArchive.existsSequence(id)){ throw new Error('Counter already defined for field "'+id+'"'); } sequence.enable(); @@ -97,11 +97,11 @@ Sequence.prototype._getCounterReferenceField = function(doc) { if (this._useReference === false) { reference = null; - }else { + } else { for (var i in this._options.reference_fields) { - reference.push({ - [this._options.reference_fields[i]]: JSON.stringify(doc[this._options.reference_fields[i]]) - }); + var obj = {}; + obj[this._options.reference_fields[i]] = JSON.stringify(doc[this._options.reference_fields[i]]); + reference.push(obj); } } @@ -114,7 +114,7 @@ Sequence.prototype._createSchemaKeys = function() { var fieldDesc = {}; fieldDesc[this._options.inc_field] = 'Number'; this._schema.add(fieldDesc); - }else { + } else { if (schemaKey.instance !== 'Number') { throw new Error('Auto increment field already present and not of type "Number"'); } From 86a5d74077de58e7f3a23de24512cf992399b569 Mon Sep 17 00:00:00 2001 From: linusbrolin Date: Tue, 27 Sep 2016 21:36:57 +0200 Subject: [PATCH 4/4] attempt to fix promise warning in test --- test/base.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/base.js b/test/base.js index e55babc..e553e25 100644 --- a/test/base.js +++ b/test/base.js @@ -6,6 +6,9 @@ var chai = require('chai'), Schema = mongoose.Schema, AutoIncrement = require('../index'); +// Use native promises +mongoose.Promise = global.Promise; + describe('Basic => ', function() { before(function connection(done) {