diff --git a/README.md b/README.md index 9d1d41a9..2f2d991a 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,13 @@ redis.pipeline([ ]).exec(function () { /* ... */ }); ``` +`#length` property shows how many commands in the pipeline: + +```javascript +const length = redis.pipeline().set('foo', 'bar').get('foo').length; +// length === 2 +``` + ## Transaction Most of the time, the transaction commands `multi` & `exec` are used together with pipeline. diff --git a/lib/pipeline.js b/lib/pipeline.js index 487f4fd5..eba25cfa 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -31,6 +31,12 @@ function Pipeline(redis) { _this.resolve = resolve; _this.reject = reject; }); + + Object.defineProperty(this, 'length', { + get: function () { + return _this._queue.length; + } + }); } _.assign(Pipeline.prototype, Commander.prototype); diff --git a/test/functional/pipeline.js b/test/functional/pipeline.js index c75d87c3..f082d47d 100644 --- a/test/functional/pipeline.js +++ b/test/functional/pipeline.js @@ -112,7 +112,7 @@ describe('pipeline', function () { }); it('should work', function (done) { - redis.pipeline().echo('foo', 'bar', '123', 'abc').exec(function(err, results) { + redis.pipeline().echo('foo', 'bar', '123', 'abc').exec(function (err, results) { expect(err).to.eql(null); expect(results).to.eql([ [null, ['foo', 'bar', '123', 'abc']] @@ -207,4 +207,19 @@ describe('pipeline', function () { }); }); }); + + describe('#length', function () { + it('return the command count', function () { + var redis = new Redis(); + + var pipeline1 = redis.pipeline().multi().set('foo', 'bar').get('foo').exec(); + expect(pipeline1.length).to.eql(4); + + var pipeline2 = redis.pipeline([ + ['set', 'foo', 'bar'], + ['get', 'foo'] + ]); + expect(pipeline2.length).to.eql(2); + }); + }); });