diff --git a/lib/sequence.js b/lib/sequence.js index d222194..11bdf73 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -110,13 +110,14 @@ Sequence.prototype.getId = function() { * reference */ Sequence.prototype._getCounterReferenceField = function(doc) { - var reference = []; + var reference = {}; if (this._useReference === false) { reference = null; }else { for (var i in this._options.reference_fields) { - reference.push(JSON.stringify(doc[this._options.reference_fields[i]])); + reference[this._options.reference_fields[i]] = doc[this._options.reference_fields[i]]; + // reference.push(JSON.stringify(doc[this._options.reference_fields[i]])); } } @@ -153,7 +154,7 @@ Sequence.prototype._createCounterModel = function() { CounterSchema = mongoose.Schema( { id: {type: String, required: true}, - reference_value: {type:Array, required: true}, + reference_value: { type: mongoose.Schema.Types.Mixed, required: true}, seq: {type:Number, default: 0, required: true} }, { diff --git a/test/base.js b/test/base.js index 709cdcb..2be2cb8 100644 --- a/test/base.js +++ b/test/base.js @@ -303,39 +303,39 @@ describe('Basic => ', function() { }); - describe('a counter which referes others fields => ', function() { + describe('a counter which referes others fields => ', function () { - before(function() { + before(function () { var ComposedSchema = new Schema({ - country: String, + country: Schema.Types.ObjectId, city: String, inhabitant: Number }); - ComposedSchema.plugin(AutoIncrement, {id: 'inhabitant_counter', inc_field: 'inhabitant', reference_fields: ['country', 'city']}); + ComposedSchema.plugin(AutoIncrement, { id: 'inhabitant_counter', inc_field: 'inhabitant', reference_fields: ['city', 'country'] }); this.Composed = mongoose.model('Composed', ComposedSchema); }); - it('increment on save', function(done) { - var t = new this.Composed({country:'France', city:'Paris'}); - t.save(function(err) { + it('increment on save', function (done) { + var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Paris' }); + t.save(function (err) { if (err) return done(err); assert.deepEqual(t.inhabitant, 1); done(); }); }); - it('saving a document with the same reference increment the counter', function(done) { - var t = new this.Composed({country:'France', city:'Paris'}); - t.save(function(err) { + it('saving a document with the same reference increment the counter', function (done) { + var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Paris' }); + t.save(function (err) { if (err) return done(err); assert.deepEqual(t.inhabitant, 2); done(); }); }); - it('saving with a different reference do not increment the counter', function(done) { - var t = new this.Composed({country:'USA', city:'New York'}); - t.save(function(err) { + it('saving with a different reference do not increment the counter', function (done) { + var t = new this.Composed({ country: mongoose.Types.ObjectId('59c380f51207391238e7f3f2'), city: 'Carcasonne' }); + t.save(function (err) { if (err) return done(err); assert.deepEqual(t.inhabitant, 1); done();