Skip to content

Commit d0f33b3

Browse files
committed
Support allowOverwrite config options.
1 parent a1c3289 commit d0f33b3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/redis.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = CoreObject.extend({
2020
this._client = require('then-redis').createClient(redisOptions);
2121
}
2222
this._maxNumberOfRecentUploads = 10;
23+
this._allowOverwrite = options.allowOverwrite;
2324
},
2425

2526
upload: function(/*key, tag, value*/) {
@@ -43,13 +44,14 @@ module.exports = CoreObject.extend({
4344

4445
_uploadIfKeyDoesNotExist: function(redisKey, value) {
4546
var client = this._client;
47+
var allowOverwrite = !!this._allowOverwrite;
4648

4749
return Promise.resolve()
4850
.then(function() {
4951
return client.get(redisKey);
5052
})
5153
.then(function(value) {
52-
if (value) {
54+
if (value && !allowOverwrite) {
5355
return Promise.reject('Value already exists for key: ' + redisKey);
5456
}
5557
})

tests/unit/lib/redis-nodetest.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,30 @@ describe('redis', function() {
5050
});
5151
});
5252

53+
it('uploads the contents if the key already exists but allowOverwrite is true', function() {
54+
var fileUploaded = false;
55+
56+
var redis = new Redis({
57+
allowOverwrite: true,
58+
redisClient: {
59+
get: function(key) {
60+
return Promise.resolve('some-other-value');
61+
},
62+
set: function(key, value) {
63+
fileUploaded = true;
64+
},
65+
lpush: function() { },
66+
ltrim: function() { }
67+
}
68+
});
69+
70+
var promise = redis.upload('key', 'value');
71+
return assert.isFulfilled(promise)
72+
.then(function() {
73+
assert.ok(fileUploaded);
74+
});
75+
});
76+
5377
it('updates the list of recent uploads once upload is successful', function() {
5478
var recentUploads = [];
5579

0 commit comments

Comments
 (0)