Skip to content

Commit

Permalink
issue ramiel#45 - implement setNext changes to allow its usage within…
Browse files Browse the repository at this point in the history
… pre-save hook
  • Loading branch information
Robert Ernens committed Feb 15, 2019
1 parent 13c5aa0 commit c8474d5
Showing 1 changed file with 26 additions and 2 deletions.
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

0 comments on commit c8474d5

Please sign in to comment.