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

issue #45 - implement setNext changes to allow its usage within pre-s… #46

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions lib/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,28 @@ Sequence.prototype._setMethods = function() {
// });
// }.bind(this));

this._schema.method('setNext', function(id, callback) {
/*
* #45 allow usage of setNext in pre-save hook prohibits save
* add save arg to make save an option ( default )
*/

this._schema.method('setNext', function(id, save, callback) {
var sequence = sequenceArchive.getSequence(id);

/*
* preserve old behaviour, if save is a function means that save arg is the callback function
* and in this case set withSave to true to preserve previous behavour
*/

var withSave;

if ( save instanceof Function ) {
withSave = true;
callback = save;
} else {
withSave = save;
}

if (_.isNull(sequence)) {
return callback(new Error('Trying to increment a wrong sequence using the id ' + id));
}
Expand All @@ -221,7 +240,12 @@ Sequence.prototype._setMethods = function() {
sequence._setNextCounter(this, function(err, seq) {
if (err) return callback(err);
this[sequence._options.inc_field] = seq;
this.save(callback);
if (withSave) {
this.save(callback);
} else {
callback();
}

}.bind(this));
});

Expand Down