Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug where the reference_fields were used as id #12

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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();
Expand Down Expand Up @@ -97,9 +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(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);
}
}

Expand All @@ -112,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"');
}
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down