tedis-mod
Version:
redis client for node.js with typescript and async
1,083 lines • 72.8 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var base_1 = require("./base");
var tools_1 = require("src/util/tools");
var Tedis = /** @class */ (function (_super) {
__extends(Tedis, _super);
function Tedis(options) {
return _super.call(this, options) || this;
}
/*******************************************************************************************************
* KEY *************************************************************************************************
*******************************************************************************************************/
/**
* Removes the specified keys. A key is ignored if it does not exist.
*
* @param key The first key.
* @param keys The other key.
* @returns The number of keys that were removed.
*/
Tedis.prototype.del = function (key) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["DEL", key], keys));
};
/**
* Returns if key exists.
*
* Since Redis 3.0.3 it is possible to specify multiple keys instead of a single one. In such a case,
* it returns the total number of keys existing. Note that returning 1 or 0 for a single key is just
* a special case of the variadic usage, so the command is completely backward compatible.
*
* The user should be aware that if the same existing key is mentioned in the arguments multiple times,
* it will be counted multiple times. So if somekey exists, EXISTS somekey somekey will return 2.
*
* @param key The first key.
* @param keys The other key.
* @returns 1 if the key exists. 0 if the key does not exist.
*/
Tedis.prototype.exists = function (key) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["EXISTS", key], keys));
};
/**
* Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key
* with an associated timeout is often said to be volatile in Redis terminology.
*
* @param key The key.
* @param seconds Expiration time in seconds.
* @returns 1 if the timeout was set. 0 if key does not exist.
*/
Tedis.prototype.expire = function (key, seconds) {
return this.command("EXPIRE", key, seconds);
};
/**
* `EXPIREAT` has the same effect and semantic as `EXPIRE`, but instead of specifying the number of
* seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since
* January 1, 1970). A timestamp in the past will delete the key immediately.
*
* Please for the specific semantics of the command refer to the documentation of `EXPIRE`.
*
* @param key The key.
* @param timestamp Expiration time in timestamp.
* @returns 1 if the timeout was set. 0 if key does not exist.
*/
Tedis.prototype.expireat = function (key, timestamp) {
return this.command("EXPIREAT", key, timestamp);
};
/**
* Returns all keys matching pattern.
*
* @param pattern Pattern string.
* @returns List of keys matching pattern.
*/
Tedis.prototype.keys = function (pattern) {
return this.command("KEYS", pattern);
};
/**
* Move key from the currently selected database (see `SELECT`) to the specified destination database.
* When key already exists in the destination database, or it does not exist in the source database,
* it does nothing. It is possible to use `MOVE` as a locking primitive because of this.
*
* @param key The key.
* @param db The specified database number.
* @returns 1 if key was moved. 0 if key was not moved.
*/
Tedis.prototype.move = function (key, db) {
return this.command("MOVE", key, db);
};
/**
* Remove the existing timeout on key, turning the key from volatile (a key with an expire set) to
* persistent (a key that will never expire as no timeout is associated).
*
* @param key The key.
* @returns 1 if the timeout was removed. 0 if key does not exist or does not have an associated timeout.
*/
Tedis.prototype.persist = function (key) {
return this.command("PERSIST", key);
};
/**
* This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds
* instead of seconds.
*
* @param key The key.
* @param milliseconds Expiration time in milliseconds.
* @returns 1 if the timeout was set. 0 if key does not exist.
*/
Tedis.prototype.pexpire = function (key, milliseconds) {
return this.command("PEXPIRE", key, milliseconds);
};
/**
* `PEXPIREAT` has the same effect and semantic as `EXPIREAT`, but the Unix time at which the key will
* expire is specified in milliseconds instead of seconds.
*
* @param key The key.
* @param millisecondsTimestamp Expiration time in millisecondsTimestamp.
* @returns 1 if the timeout was set. 0 if key does not exist.
*/
Tedis.prototype.pexpireat = function (key, millisecondsTimestamp) {
return this.command("PEXPIREAT", key, millisecondsTimestamp);
};
/**
* Like `TTL` this command returns the remaining time to live of a key that has an expire set, with the
* sole difference that `TTL` returns the amount of remaining time in seconds while `PTTL` returns it in
* milliseconds.
*
* @param key The key.
* @returns `TTL` in milliseconds, or a negative value in order to signal an error (see the description
* above). The command returns -2 if the key does not exist. The command returns -1 if the key exists
* but has no associated expire.
*/
Tedis.prototype.pttl = function (key) {
return this.command("PTTL", key);
};
/**
* Return a random key from the currently selected database.
*
* @returns The random key, or nil when the database is empty.
*/
Tedis.prototype.randomkey = function () {
return this.command("RANDOMKEY");
};
/**
* Renames key to newkey. It returns an error when key does not exist. If newkey already exists it is
* overwritten, when this happens `RENAME` executes an implicit DEL operation, so if the deleted key
* contains a very big value it may cause high latency even if `RENAME` itself is usually a constant-time
* operation.
*
* Note: Before Redis 3.2.0, an error is returned if source and destination names are the same.
*
* @param key The key.
* @param newKey The newKey.
* @returns "OK".
*/
Tedis.prototype.rename = function (key, newKey) {
return this.command("RENAME", key, newKey);
};
/**
* Renames key to newkey if newkey does not yet exist. It returns an error when key does not exist.
*
* Note: Before Redis 3.2.0, an error is returned if source and destination names are the same.
*
* @param key The key.
* @param newKey The newKey.
* @returns 1 if key was renamed to newkey. 0 if newkey already exists.
*/
Tedis.prototype.renamenx = function (key, newKey) {
return this.command("RENAMENX", key, newKey);
};
/**
* Returns the remaining time to live of a key that has a timeout. This introspection capability allows
* a Redis client to check how many seconds a given key will continue to be part of the dataset.
*
* In Redis 2.6 or older the command returns -1 if the key does not exist or if the key exist but has
* no associated expire.
*
* Starting with Redis 2.8 the return value in case of error changed:
* - The command returns -2 if the key does not exist.
* - The command returns -1 if the key exists but has no associated expire.
*
* See also the `PTTL` command that returns the same information with milliseconds resolution (Only
* available in Redis 2.6 or greater).
*
* @param key The key.
* @returns TTL in seconds, or a negative value in order to signal an error (see the description above).
*/
Tedis.prototype.ttl = function (key) {
return this.command("TTL", key);
};
/**
* Returns the string representation of the type of the value stored at key. The different types that
* can be returned are: string, list, set, zset and hash.
*
* @param key The key.
* @returns Type of key(string, hash, list, set, zset), or none when key does not exist.
*/
Tedis.prototype.type = function (key) {
return this.command("TYPE", key);
};
/*******************************************************************************************************
* STRING **********************************************************************************************
*******************************************************************************************************/
/**
* If key already exists and is a string, this command appends the value at the end of the string. If
* key does not exist it is created and set as an empty string, so `APPEND` will be similar to SET in
* this special case.
*
* @param key The key.
* @param value The value.
* @returns The length of the string after the append operation.
*/
Tedis.prototype.append = function (key, value) {
return this.command("APPEND", key, value);
};
/**
* Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing
* the operation. An error is returned if the key contains a value of the wrong type or contains a string
* that can not be represented as integer. This operation is limited to 64 bit signed integers.
*
* @param key The key.
* @returns The value of key after the decrement.
*/
Tedis.prototype.decr = function (key) {
return this.command("DECR", key);
};
/**
* Decrements the number stored at key by decrement. If the key does not exist, it is set to 0 before
* performing the operation. An error is returned if the key contains a value of the wrong type or
* contains a string that can not be represented as integer. This operation is limited to 64 bit
* signed integers.
*
* @param key The key.
* @param increment The increment.
* @returns The value of key after the decrement.
*/
Tedis.prototype.decrby = function (key, increment) {
return this.command("DECRBY", key, increment);
};
/**
* Get the value of key. If the key does not exist the special value nil is returned. An error is returned
* if the value stored at key is not a string, because `GET` only handles string values.
*
* @param key The key.
* @returns The value of key, or nil when key does not exist.
*/
Tedis.prototype.get = function (key) {
return this.command("GET", key);
};
/**
* Returns the bit value at offset in the string value stored at key.
*
* When offset is beyond the string length, the string is assumed to be a contiguous space with 0 bits.
* When key does not exist it is assumed to be an empty string, so offset is always out of range and
* the value is also assumed to be a contiguous space with 0 bits.
*
* @param key The key.
* @param offset The offset.
* @returns The bit value stored at offset.
*/
Tedis.prototype.getbit = function (key, offset) {
return this.command("GETBIT", key, offset);
};
/**
* Returns the substring of the string value stored at key, determined by the offsets start and end
* (both are inclusive). Negative offsets can be used in order to provide an offset starting from
* the end of the string. So -1 means the last character, -2 the penultimate and so forth.
*
* The function handles out of range requests by limiting the resulting range to the actual length
* of the string.
*
* @param key The key.
* @param range The range. Start is the start position, and end is the end position
* @returns Substring.
*/
Tedis.prototype.getrange = function (key, _a) {
var _b = __read(_a === void 0 ? [0, -1] : _a, 2), start = _b[0], end = _b[1];
return this.command("GETRANGE", key, start, end);
};
/**
* Atomically sets key to value and returns the old value stored at key. Returns an error when key
* exists but does not hold a string value.
*
* @param key The key.
* @param value The value.
* @returns The old value stored at key, or nil when key did not exist.
*/
Tedis.prototype.getset = function (key, value) {
return this.command("GETSET", key, value);
};
/**
* Increments the number stored at key by one. If the key does not exist, it is set to 0 before
* performing the operation. An error is returned if the key contains a value of the wrong type
* or contains a string that can not be represented as integer. This operation is limited to 64
* bit signed integers.
*
*
* Note: this is a string operation because Redis does not have a dedicated integer type. The
* string stored at the key is interpreted as a base-10 64 bit signed integer to execute the
* operation.
*
* Redis stores integers in their integer representation, so for string values that actually
* hold an integer, there is no overhead for storing the string representation of the integer.
*
* @param key The key.
* @returns the value of key after the increment.
*/
Tedis.prototype.incr = function (key) {
return this.command("INCR", key);
};
/**
* Increments the number stored at key by increment. If the key does not exist, it is set to 0 before
* performing the operation. An error is returned if the key contains a value of the wrong type or
* contains a string that can not be represented as integer. This operation is limited to 64 bit signed
* integers.
*
* @param key The key.
* @param increment The increment.
* @returns The value of key after the increment.
*/
Tedis.prototype.incrby = function (key, increment) {
return this.command("INCRBY", key, increment);
};
/**
* Increment the string representing a floating point number stored at key by the specified increment.
* By using a negative increment value, the result is that the value stored at the key is decremented
* (by the obvious properties of addition). If the key does not exist, it is set to 0 before performing
* the operation. An error is returned if one of the following conditions occur:
* - The key contains a value of the wrong type (not a string).
* - The current key content or the specified increment are not parsable as a double precision floating
* point number.
*
* If the command is successful the new incremented value is stored as the new value of the key (replacing
* the old one), and returned to the caller as a string.
*
* @param key The key.
* @param increment The increment.
* @returns The value of key after the increment.
*/
Tedis.prototype.incrbyfloat = function (key, increment) {
return this.command("INCRBYFLOAT", key, increment);
};
/**
* Returns the values of all specified keys. For every key that does not hold a string value or does not
* exist, the special value nil is returned. Because of this, the operation never fails.
*
* @param key The key.
* @param keys The other key.
* @returns List of values at the specified keys.
*/
Tedis.prototype.mget = function (key) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["MGET", key], keys));
};
/**
* Sets the given keys to their respective values. `MSET` replaces existing values with new values, just
* as regular `SET`. See `MSETNX` if you don't want to overwrite existing values.
*
* `MSET` is atomic, so all given keys are set at once. It is not possible for clients to see that some
* of the keys were updated while others are unchanged.
*
* @param objKV The objects that need to be saved
* @returns Always OK since `MSET` can't fail.
*/
Tedis.prototype.mset = function (objKV) {
var arrayKV = [];
Reflect.ownKeys(objKV).forEach(function (key) {
arrayKV.push(key, objKV[key]);
});
return this.command.apply(this, __spread(["MSET"], arrayKV));
};
/**
* Sets the given keys to their respective values. `MSETNX` will not perform any operation at all even
* if just a single key already exists.
*
* Because of this semantic `MSETNX` can be used in order to set different keys representing different
* fields of an unique logic object in a way that ensures that either all the fields or none at all are
* set.
*
* `MSETNX` is atomic, so all given keys are set at once. It is not possible for clients to see that
* some of the keys were updated while others are unchanged.
*
* @param objKv The objects that need to be saved
* @returns 1 if the all the keys were set. 0 if no key was set (at least one key already existed).
*/
Tedis.prototype.msetnx = function (objKv) {
var arrayKV = [];
Reflect.ownKeys(objKv).forEach(function (key) {
arrayKV.push(key, objKv[key]);
});
return this.command.apply(this, __spread(["MSETNX"], arrayKV));
};
/**
* `PSETEX` works exactly like `SETEX` with the sole difference that the expire time is specified in
* milliseconds instead of seconds.
*
* @param key The key.
* @param milliseconds Expiration time in milliseconds.
* @param value The value.
* @returns "OK".
*/
Tedis.prototype.psetex = function (key, milliseconds, value) {
return this.command("PSETEX", key, milliseconds, value);
};
/**
* Set key to hold the string value. If key already holds a value, it is overwritten, regardless of
* its type. Any previous time to live associated with the key is discarded on successful SET operation.
*
* Options Starting with Redis 2.6.12 SET supports a set of options that modify its behavior:
* - EX seconds -- Set the specified expire time, in seconds.
* - PX milliseconds -- Set the specified expire time, in milliseconds.
* - NX -- Only set the key if it does not already exist.
* - XX -- Only set the key if it already exist.
*
* @param key The key.
* @param value The value.
* @returns OK if `SET` was executed correctly. Null reply: a Null Bulk Reply is returned if the `SET`
* operation was not performed because the user specified the NX or XX option but the condition was
* not met.
*/
Tedis.prototype.set = function (key, value) {
return this.command("SET", key, value);
};
/**
* Sets or clears the bit at offset in the string value stored at key.
*
* The bit is either set or cleared depending on value, which can be either 0 or 1. When key does not
* exist, a new string value is created. The string is grown to make sure it can hold a bit at offset.
* The offset argument is required to be greater than or equal to 0, and smaller than 2^32 (this limits
* bitmaps to 512MB). When the string at key is grown, added bits are set to 0.
*
* @param key The key.
* @param offset The offset
* @param value The value.
* @returns The original bit value stored at offset.
*/
Tedis.prototype.setbit = function (key, offset, value) {
return this.command("SETBIT", key, offset, value);
};
/**
* Set key to hold the string value and set key to timeout after a given number of seconds.
*
* @param key The key.
* @param seconds Expiration time in seconds.
* @param value The value.
* @returns "OK".
*/
Tedis.prototype.setex = function (key, seconds, value) {
return this.command("SETEX", key, seconds, value);
};
/**
* Set key to hold string value if key does not exist. In that case, it is equal to `SET`. When key already
* holds a value, no operation is performed. `SETNX` is short for "SET if Not eXists".
*
* @param key The key.
* @param keys The other key.
* @returns 1 if the key was set, 0 if the key was not set.
*/
Tedis.prototype.setnx = function (key) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["SETNX", key], keys));
};
/**
* Overwrites part of the string stored at key, starting at the specified offset, for the entire length of
* value. If the offset is larger than the current length of the string at key, the string is padded with
* zero-bytes to make offset fit. Non-existing keys are considered as empty strings, so this command will
* make sure it holds a string large enough to be able to set value at offset.
*
* @param key The key.
* @param offset The offset.
* @param value The value.
* @returns The length of the string after it was modified by the command.
*/
Tedis.prototype.setrange = function (key, offset, value) {
return this.command("SETRANGE", key, offset, value);
};
/**
* Returns the length of the string value stored at key. An error is returned when key holds a non-string value.
*
* @param key The key.
* @returns The length of the string at key, or 0 when key does not exist.
*/
Tedis.prototype.strlen = function (key) {
return this.command("STRLEN", key);
};
/*******************************************************************************************************
* HASH ************************************************************************************************
*******************************************************************************************************/
/**
* Removes the specified fields from the hash stored at key. Specified fields that do not exist within
* this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.
*
* @param key The key.
* @param field The field.
* @param fields The other field.
* @returns The number of fields that were removed from the hash, not including specified but non existing
* fields.
*/
Tedis.prototype.hdel = function (key, field) {
var fields = [];
for (var _i = 2; _i < arguments.length; _i++) {
fields[_i - 2] = arguments[_i];
}
return this.command.apply(this, __spread(["HDEL", key, field], fields));
};
/**
* Returns if field is an existing field in the hash stored at key.
* @param key The key.
* @param field The field.
* @returns 1 if the hash contains field. 0 if the hash does not contain field, or key does not exist.
*/
Tedis.prototype.hexists = function (key, field) {
return this.command("HEXISTS", key, field);
};
/**
* Returns the value associated with field in the hash stored at key.
*
* @param key The key.
* @param field The field.
* @returns The value associated with field, or nil when field is not present in the hash or key does not exist.
*/
Tedis.prototype.hget = function (key, field) {
return this.command("HGET", key, field);
};
/**
* Returns all fields and values of the hash stored at key. In the returned value, every field name is followed
* by its value, so the length of the reply is twice the size of the hash.
*
* @param key The key.
* @returns List of fields and their values stored in the hash, or an empty list when key does not exist.
*/
Tedis.prototype.hgetall = function (key) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = tools_1.Array2Object;
return [4 /*yield*/, this.command("HGETALL", key)];
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
}
});
});
};
/**
* Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new
* key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.
*
* The range of values supported by `HINCRBY` is limited to 64 bit signed integers.
*
* @param key The key.
* @param field The field.
* @param increment The increment.
* @returns The value at field after the increment.
*/
Tedis.prototype.hincrby = function (key, field, increment) {
return this.command("HINCRBY", key, field, increment);
};
/**
* Increment the specified field of a hash stored at key, and representing a floating point number, by the
* specified increment. If the increment value is negative, the result is to have the hash field value
* decremented instead of incremented. If the field does not exist, it is set to 0 before performing the
* operation. An error is returned if one of the following conditions occur:
* - The field contains a value of the wrong type (not a string).
* - The current field content or the specified increment are not parsable as a double precision floating point number.
*
* @param key The key.
* @param field The field.
* @param increment The increment.
* @returns The value at field after the increment.
*/
Tedis.prototype.hincrbyfloat = function (key, field, increment) {
return this.command("HINCRBYFLOAT", key, field, increment);
};
/**
* Returns all field names in the hash stored at key.
*
* @param key The key.
* @returns List of fields in the hash, or an empty list when key does not exist.
*/
Tedis.prototype.hkeys = function (key) {
return this.command("HKEYS", key);
};
/**
* Returns the number of fields contained in the hash stored at key.
* @param key The key.
* @returns Number of fields in the hash, or 0 when key does not exist.
*/
Tedis.prototype.hlen = function (key) {
return this.command("HLEN", key);
};
/**
* Returns the values associated with the specified fields in the hash stored at key.
*
* For every field that does not exist in the hash, a nil value is returned. Because non-existing keys are
* treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.
*
* @param key The key.
* @param field The field.
* @param fields The other field.
* @returns List of values associated with the given fields, in the same order as they are requested.
*/
Tedis.prototype.hmget = function (key, field) {
var fields = [];
for (var _i = 2; _i < arguments.length; _i++) {
fields[_i - 2] = arguments[_i];
}
return this.command.apply(this, __spread(["HMGET", key, field], fields));
};
/**
* Sets the specified fields to their respective values in the hash stored at key. This command overwrites
* any specified fields already existing in the hash. If key does not exist, a new key holding a hash is
* created.
*
* @param key The key.
* @param data The data.
* @returns "OK".
*/
Tedis.prototype.hmset = function (key, data) {
var arrayFV = [];
Reflect.ownKeys(data).forEach(function (field) {
arrayFV.push(field, data[field]);
});
return this.command.apply(this, __spread(["HMSET", key], arrayFV));
};
/**
* Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created.
* If field already exists in the hash, it is overwritten.
*
* @param key The key.
* @param field The field.
* @param value The value.
* @returns 1 if field is a new field in the hash and value was set. 0 if field already exists in the hash
* and the value was updated.
*/
Tedis.prototype.hset = function (key, field, value) {
return this.command("HSET", key, field, value);
};
/**
* Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist,
* a new key holding a hash is created. If field already exists, this operation has no effect.
*
* @param key The key.
* @param field The field.
* @param value The value.
* @returns 1 if field is a new field in the hash and value was set. 0 if field already exists in the hash
* and no operation was performed.
*/
Tedis.prototype.hsetnx = function (key, field, value) {
return this.command("HSETNX", key, field, value);
};
/**
* Returns the string length of the value associated with field in the hash stored at key. If the key or the
* field do not exist, 0 is returned.
*
* @param key The key.
* @param field The field.
* @returns The string length of the value associated with field, or zero when field is not present in the
* hash or key does not exist at all.
*/
Tedis.prototype.hstrlen = function (key, field) {
return this.command("HSTRLEN", key, field);
};
/**
* Returns all values in the hash stored at key.
* @param key The key.
* @returns List of values in the hash, or an empty list when key does not exist.
*/
Tedis.prototype.hvals = function (key) {
return this.command("HVALS", key);
};
/*******************************************************************************************************
* LIST ************************************************************************************************
*******************************************************************************************************/
/**
* BLPOP is a blocking list pop primitive. It is the blocking version of LPOP because it blocks the
* connection when there are no elements to pop from any of the given lists. An element is popped from
* the head of the first list that is non-empty, with the given keys being checked in the order that
* they are given.
*
* @param timeout The timeout.
* @param keys The keys.
* @returns
* - A nil multi-bulk when no element could be popped and the timeout expired.
* - A two-element multi-bulk with the first element being the name of the key where an element was
* popped and the second element being the value of the popped element.
*/
Tedis.prototype.blpop = function (timeout) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["BLPOP"], keys, [timeout]));
};
/**
* BRPOP is a blocking list pop primitive. It is the blocking version of RPOP because it blocks the
* connection when there are no elements to pop from any of the given lists. An element is popped
* from the tail of the first list that is non-empty, with the given keys being checked in the order
* that they are given.
*
* @param timeout The timeout.
* @param keys The keys.
* @returns
* - A nil multi-bulk when no element could be popped and the timeout expired.
* - A two-element multi-bulk with the first element being the name of the key where an element was
* popped and the second element being the value of the popped element.
*/
Tedis.prototype.brpop = function (timeout) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return this.command.apply(this, __spread(["BRPOP"], keys, [timeout]));
};
/**
* BRPOPLPUSH is the blocking variant of RPOPLPUSH. When source contains elements, this command behaves
* exactly like RPOPLPUSH. When used inside a MULTI/EXEC block, this command behaves exactly like
* RPOPLPUSH. When source is empty, Redis will block the connection until another client pushes to it
* or until timeout is reached. A timeout of zero can be used to block indefinitely.
*
* @param source The source.
* @param destination The destination.
* @param timeout The timeout.
* @returns The element being popped from source and pushed to destination. If timeout is reached, a
* Null reply is returned.
*/
Tedis.prototype.brpoplpush = function (source, destination, timeout) {
return this.command("BRPOPLPUSH", source, destination, timeout);
};
/**
* Returns the element at index index in the list stored at key. The index is zero-based, so 0 means
* the first element, 1 the second element and so on. Negative indices can be used to designate elements
* starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate and so
* forth.
*
* When the value at key is not a list, an error is returned.
*
* @param key The key.
* @param index The index.
* @returns The requested element, or nil when index is out of range.
*/
Tedis.prototype.lindex = function (key, index) {
return this.command("LINDEX", key, index);
};
/**
* Inserts value in the list stored at key either before or after the reference value pivot.
*
* When key does not exist, it is considered an empty list and no operation is performed.
*
* An error is returned when key exists but does not hold a list value.
*
* @param key The key.
* @param type The type.
* @param pivot The pivot.
* @param value The value.
* @returns the length of the list after the insert operation, or -1 when the value pivot was not found.
*/
Tedis.prototype.linsert = function (key, type, pivot, value) {
return this.command("LINSERT", key, type, pivot, value);
};
/**
* Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty
* list and 0 is returned. An error is returned when the value stored at key is not a list.
*
* @param key The key.
* @returns The length of the list at key.
*/
Tedis.prototype.llen = function (key) {
return this.command("LLEN", key);
};
/**
* Removes and returns the first element of the list stored at key.
*
* @param key The key.
* @returns The value of the first element, or nil when key does not exist.
*/
Tedis.prototype.lpop = function (key) {
return this.command("LPOP", key);
};
/**
* Insert all the specified values at the head of the list stored at key. If key does not exist, it is
* created as empty list before performing the push operations. When key holds a value that is not a list,
* an error is returned.
*
* It is possible to push multiple elements using a single command call just specifying multiple arguments
* at the end of the command. Elements are inserted one after the other to the head of the list, from the
* leftmost element to the rightmost element. So for instance the command LPUSH mylist a b c will result
* into a list containing c as first element, b as second element and a as third element.
*
* @param key The key.
* @param value The value.
* @param values The other value.
* @returns The length of the list after the push operations.
*/
Tedis.prototype.lpush = function (key, value) {
var values = [];
for (var _i = 2; _i < arguments.length; _i++) {
values[_i - 2] = arguments[_i];
}
return this.command.apply(this, __spread(["LPUSH", key, value], values));
};
/**
* Inserts value at the head of the list stored at key, only if key already exists and holds a list. In
* contrary to LPUSH, no operation will be performed when key does not yet exist.
*
* @param key The key.
* @param value The value.
* @returns The length of the list after the push operation.
*/
Tedis.prototype.lpushx = function (key, value) {
return this.command("LPUSHX", key, value);
};
/**
* Returns the specified elements of the list stored at key. The offsets start and stop are zero-based
* indexes, with 0 being the first element of the list (the head of the list), 1 being the next element
* and so on.
*
* These offsets can also be negative numbers indicating offsets starting at the end of the list. For
* example, -1 is the last element of the list, -2 the penultimate, and so on.
*
* ### Consistency with range functions in various programming languages
* Note that if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements,
* that is, the rightmost item is included. This may or may not be consistent with behavior of
* range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice
* or Python's range() function).
*
* ### Out-of-range indexes
* Out of range indexes will not produce an error. If start is larger than the end of the list, an empty
* list is returned. If stop is larger than the actual end of the list, Redis will treat it like the
* last element of the list.
*
* @param key The key.
* @param start The start.
* @param stop The stop.
* @returns List of elements in the specified range.
*/
Tedis.prototype.lrange = function (key, start, stop) {
return this.command("LRANGE", key, start, stop);
};
/**
* Removes the first count occurrences of elements equal to value from the list stored at key. The count
* argument influences the operation in the following ways:
* - count > 0: Remove elements equal to value moving from head to tail.
* - count < 0: Remove elements equal to value moving from tail to head.
* - count = 0: Remove all elements equal to value.
*
* For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored
* at list.
*
* Note that non-existing keys are treated like empty lists, so when key does not exist, the command will
* always return 0.
*
* @param key The key.
* @param count The count.
* @param value The value.
* @returns The number of removed elements.
*/
Tedis.prototype.lrem = function (key, count, value) {
return this.command("LREM", key, count, value);
};
/**
* Sets the list element at index to value. For more information on the index argument, see LINDEX.
*
* An error is returned for out of range indexes.
*
* @param key The key.
* @param count The count.
* @param value The value.
* @returns "OK".
*/
Tedis.prototype.lset = function (key, index, value) {
return this.command("LSET", key, index, value);
};
/**
* Trim an existing list so that it will contain only the specified range of elements specified. Both
* start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the
* next element and so on.
*
* For example: LTRIM foobar 0 2 will modify the list stored at foobar so that only the first three
* elements of the list will remain.
*
* start and end can also be negative numbers indicating offsets from the end of the list, where -1
* is the last element of the list, -2 the penultimate element and so on.
*
* Out of range indexes will not produce an error: if start is larger than the end of the list, or
* start > end, the result will be an empty list (which causes key to be removed). If end is larger
* than the end of the list, Redis will treat it like the last element of the list.
*
* A common use of LTRIM is together with LPUSH / RPUSH. For example:
* ```bash
* LPUSH mylist someelement
* LTRIM mylist 0 99
* ```
*
* This pair of commands will push a new element on the list, while making sure that the list will
* not grow larger than 100 elements. This is very useful when using Redis to store logs for example.
* It is important to note that when used in this way LTRIM is an O(1) operation because in the
* average case just one element is removed from the tail of the list.
*
* @param key The key.
* @param start The start.
* @param stop The stop.
* @returns "OK".
*/
Tedis.prototype.ltrim = function (key, start, stop) {
return this.command("LTRIM", key, start, stop);
};
/**
* Removes and returns the last element of the list stored at key.
* @param key The key.
* @returns The value of the last element, or nil when key does not exist.
*/
Tedis.prototype.rpop = function (key) {
return this.command("RPOP", key);
};
/**
* Atomically returns and removes the last element (tail) of the list stored at source, and pushes
* the element at the first element (head) of the list stored at destination.
*
* For example: consider source holding the list a,b,c, and destination holding the list x,y,z.
* Executing RPOPLPUSH results in source holding a,b and destination holding c,x,y,z.
*
* If source does not exist, the value nil is returned and no operation is performed. If source and
* destination are the same, the operation is equivalent to removing the last element from the list
* and pushing it as first element of the list, so it can be considered as a list rotation command.
*
* @param source The source.
* @param destination The destination.
* @returns The element being popped and pushed.
*/
Tedis.prototype.rpoplpush = function (source, destination) {
return this.command("RPOPLPUSH", source, destination);
};
/**
* Insert all the specified values at the tail of the list stored at key. If key does not exist,
* it is created as empty list before performing the push operation. When key holds a value that
* is not a list, an error is returned.
*
* It is possible to push multiple elements using a single command call just specifying multiple
* arguments at the end of the command. Elements are inserted one after the other to the tail of
* the list, from the leftmost element to the rightmost element. So for instance the command RPUSH
* mylist a b c will result into a list containing a as first element, b as second element and c
* as third element.
*
* @param key The key.
* @param value The value.
* @param values The other value.
* @returns The length of the list after the push operation.
*/
Tedis.prototype.rpush = function (key, value) {
var values = [];
for (var _i = 2; _i < arguments.length; _i++) {
values[_i - 2] = arguments[_i];
}
return this.command.apply(this, __spread(["RPUSH", key, value], values));
};
/**
* Inserts value at the tail of the list stored at key, only if key already exists and holds a list.
* In contrary to RPUSH, no operation will be performed when key does not yet exist.
*
* @param key The key.
* @param value The value.
* @returns The length of the list after the push operation.
*/
Tedis.prototype.rpushx = function (key, value) {
return this.command("RPUSHX", key, value);
};
/*******************************************************************************************************
* SET *************************************************************************************************
*******************************************************************************************************/
/**
* Add the specified members to the set stored at key. Specified members that are already a member of
* this set are ignored. If key does not exist, a new set is created before adding the specified members.
*
* An error is returned when the value stored at key is not a set.
*
* @param key The key.
* @param member The member.
* @param members The other member.
* @returns The number of elements that were added to the set, not including all the elements already
* present into the set.
*/
Tedis.prototype.sadd = function (key, member) {
var members = [];
for (var _i = 2; _i < arguments.length; _i++) {
members[_i - 2] = arguments[_i];
}
return this.command.apply(this, __spread(["SADD", key, member], members));
};
/**
* Returns the set cardinality (number of elements) of the set stored at key.
*
* @param key The key.
* @returns The cardinality (number of elements) of the set, or 0 if key does not exist.
*/
Tedis.prototype.scard = function (key) {
return this.command("SCARD", key);
};
/**
* Returns the members of the set resulting from the difference between the first set and all the
* successive sets. Keys that do not exist are considered to be empty sets.
*
* @param key The key.
* @param anotherkey The anotherkey.
*