redis-mock
Advanced tools
Comparing version 0.56.1 to 0.56.2
@@ -114,4 +114,17 @@ 'use strict'; | ||
append: new ArgumentParser('APPEND', { | ||
default: [ | ||
{ | ||
name: 'key', | ||
type: String | ||
}, | ||
{ | ||
name: 'value', | ||
type: String | ||
} | ||
] | ||
}), | ||
}; | ||
@@ -342,41 +342,11 @@ const events = require("events"), | ||
RedisClient.prototype.set = RedisClient.prototype.SET = function (...userArgs) { | ||
exec(parsers.set, userArgs, (args, cb) => { | ||
//TODO: introduce support for GET | ||
exec(parsers.set, userArgs, (args, cb) => | ||
this._selectedDb.set(args.default.key, args.default.value, cb, Object.assign({}, args.flags, args.named)) | ||
); | ||
}; | ||
const getExpirationTime = () => { | ||
if (args.named.ex) { | ||
return args.named.ex; | ||
} | ||
if (args.named.px) { | ||
return args.named.px / 1000; | ||
} | ||
return undefined; | ||
}; | ||
const keyExists = args.default.key in this._selectedDb.storage; | ||
const expirationTime = getExpirationTime(); | ||
const postProcess = () => { | ||
if (expirationTime) { | ||
this._selectedDb.expire(args.default.key, expirationTime, (err, result) => { | ||
helpers.callCallback(cb, err, "OK"); | ||
}); | ||
} else { | ||
helpers.callCallback(cb, null, "OK"); | ||
} | ||
}; | ||
if ((keyExists && args.named.xx) || (!keyExists && args.named.nx) || (!args.named.xx && !args.named.nx)) { | ||
// it it's okay to set the value | ||
this._selectedDb.set(args.default.key, args.default.value, (err, result) => { | ||
if (err) { | ||
return helpers.callCallback(cb, err); | ||
} | ||
postProcess(); | ||
}); | ||
} else { | ||
// if it's not okay to set the value, but might still be okay to update the expiration timestamp, or GET the result | ||
postProcess(); | ||
} | ||
}); | ||
RedisClient.prototype.append = RedisClient.prototype.APPEND = function(...userArgs) { | ||
exec(parsers.append, userArgs, (args, cb) => | ||
this._selectedDb.append(args.default.key, args.default.value, cb) | ||
); | ||
}; | ||
@@ -383,0 +353,0 @@ |
@@ -18,9 +18,45 @@ const Item = require("./item.js"); | ||
* Set | ||
* | ||
* [EX seconds|PX milliseconds|KEEPTTL] [NX|XX] [GET] | ||
* | ||
* TODO: introduce support for GET | ||
*/ | ||
exports.set = function (key, value, callback) { | ||
this.storage[key] = createItem(value); | ||
exports.set = function (key, value, callback, args = {}) { | ||
const getExpirationTime = () => { | ||
if (args.ex) { | ||
return args.ex; | ||
} | ||
if (args.px) { | ||
return args.px / 1000; | ||
} | ||
return undefined; | ||
}; | ||
helpers.callCallback(callback, null, "OK"); | ||
const keyExists = key in this.storage; | ||
const expirationTime = getExpirationTime(); | ||
if ((keyExists && args.xx) || (!keyExists && args.nx) || (!args.xx && !args.nx)) { | ||
// it it's okay to set the value | ||
this.storage[key] = createItem(value); | ||
} | ||
if (expirationTime) { | ||
this.expire(key, expirationTime, (err, result) => { | ||
helpers.callCallback(callback, err, "OK"); | ||
}); | ||
} else { | ||
helpers.callCallback(callback, null, "OK"); | ||
} | ||
}; | ||
exports.append = function(key, value, cb) { | ||
let existing = this.get(key); | ||
if (typeof existing !== 'string') { | ||
existing = ''; | ||
} | ||
const newValue = existing + value; | ||
this.set(key, newValue); | ||
helpers.callCallback(cb, null, newValue.length); | ||
}; | ||
/** | ||
@@ -73,2 +109,3 @@ * Ping | ||
helpers.callCallback(callback, err, value); | ||
return value; | ||
}; | ||
@@ -75,0 +112,0 @@ |
{ | ||
"name": "redis-mock", | ||
"version": "0.56.1", | ||
"version": "0.56.2", | ||
"description": "Redis client mock object for unit testing", | ||
@@ -5,0 +5,0 @@ "author": "Kristian Faeldt <kristian.faeldt@gmail.com>", |
@@ -88,2 +88,3 @@ redis-mock | ||
* set | ||
* append | ||
* getset | ||
@@ -90,0 +91,0 @@ * mget |
121491
3744
225