From c8474d5f1600093d4729cd8ce0cb1d8055d4701c Mon Sep 17 00:00:00 2001 From: Robert Ernens Date: Fri, 15 Feb 2019 13:52:00 +0100 Subject: [PATCH] issue #45 - implement setNext changes to allow its usage within pre-save hook --- lib/sequence.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/sequence.js b/lib/sequence.js index e85c888..f07721c 100644 --- a/lib/sequence.js +++ b/lib/sequence.js @@ -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)); } @@ -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)); });