chen-redis
Version:
Redis for Chen Framework
293 lines • 9.15 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};
const redis = require('redis');
/**
* RedisConnection class
*/
class RedisConnection {
/**
* Redis configuration
* @param {RedisConfig} private config
*/
constructor(config) {
this.config = config;
}
/**
* Get redis client
* @return {redis.RedisClient}
*/
getClient() {
if (!this.client) {
this.client = redis.createClient(this.config);
}
return this.client;
}
/**
* Execute redis command
* @param {string} command
* @param {any[]} ...args
* @return {Promise<U>}
*/
executeCommand(command, ...args) {
return new Promise((resolve, reject) => {
args.push((err, res) => {
if (err)
return reject(err);
resolve(res);
});
let client = this.getClient();
client[command].apply(client, args);
});
}
/**
* Set the value of key
* @param {string} key
* @param {any} value
* @return {Promise<string>}
*/
set(key, value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('set', key, value);
});
}
/**
* Get the value of key
* @param {string} key
* @return {Promise<string>}
*/
get(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('get', key);
});
}
/**
* Returns all keys matching pattern.
* @param {string match
* @return {Promise<any[]>}
*/
keys(match) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('keys', match);
});
}
/**
* Returns the length of the list stored at key
* @param {string} key
* @return {Promise<number>}
*/
llen(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('llen', key);
});
}
/**
* Returns all fields and values of the hash stored at key
* @param {string} key
* @return {Promise<KeyValuePair<any>>}
*/
hgetall(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('hgetall', key);
});
}
/**
* Sets the specified fields to their respective values in the hash stored at key
* @param {string} key
* @param {KeyValuePair<any>} values
* @return {Promise<boolean>}
*/
hmset(key, values) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('hmset', key, values);
});
}
/**
* Sets field in the hash stored at key to value
* @param {string} key
* @param {string} field
* @param {any} value
* @return {Promise<number>}
*/
hset(key, field, value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('hset', key, field, value);
});
}
/**
* Returns the value associated with field in the hash stored at key.
* @param {string} key
* @param {string} field
* @return {Promise<string>}
*/
hget(key, field) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('hget', key, field);
});
}
/**
* Returns if key exists
* @param {string | string[]} key
* @return {Promise<number>}
*/
exists(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('exists', key);
});
}
/**
* Removes the specified keys
* @param {string | string[]} key
* @return {Promise<number>}
*/
del(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('del', key);
});
}
/**
* Returns the specified elements of the list stored at key
* @param {string} key
* @param {number} start
* @param {number} stop
* @return {Promise<any[]>}
*/
lrange(key, start, stop) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('lrange', key, start, stop);
});
}
/**
* Removes and returns the first element of the list stored at key
* @param {string} key
* @return {Promise<string>}
*/
lpop(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('lpop', key);
});
}
/**
* Insert all the specified values at the tail of the list stored at key
* @param {string} key
* @param {any | any[]} values
* @return {Promise<number>}
*/
rpush(key, values) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('rpush', key, values);
});
}
/**
* Insert all the specified values at the head of the list stored at key
* @param {string} key
* @param {any | any[]} values
* @return {Promise<number>}
*/
lpush(key, values) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('lpush', key, values);
});
}
/**
* Returns the score of member in the sorted set at key
* @param {string} key
* @param {string} value
* @return {Promise<string>}
*/
zscore(key, value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('zscore', key, value);
});
}
/**
* Adds all the specified members with the specified scores to the sorted set stored at key
* @param {string} key
* @param {number} score
* @param {string} value
* @param {string = ''} option
* @return {Promise<number>}
*/
zadd(key, score, value, option = '') {
return __awaiter(this, void 0, void 0, function* () {
let args = [score, value];
if (option) {
args.unshift(option);
}
args.unshift(key);
return yield this.executeCommand('zadd', args);
});
}
/**
* Returns the specified range of elements in the sorted set stored at key
* @param {string} key
* @param {number} start
* @param {number} stop
* @param {boolean = false} withScores
* @return {Promise<any[]>}
*/
zrevrange(key, start, stop, withScores = false) {
return __awaiter(this, void 0, void 0, function* () {
let args = [key, start, stop];
if (withScores) {
args.push('WITHSCORES');
}
return yield this.executeCommand('zrevrange', args);
});
}
/**
* Returns the specified range of elements in the sorted set stored at key
* @param {string} key
* @param {number} start
* @param {number} stop
* @param {boolean = false} withScores
* @return {Promise<any[]>}
*/
zrange(key, start, stop, withScores = false) {
return __awaiter(this, void 0, void 0, function* () {
let args = [key, start, stop];
if (withScores) {
args.push('WITHSCORES');
}
return yield this.executeCommand('zrange', args);
});
}
/**
* Returns the sorted set cardinality (number of elements) of the sorted set stored at key
* @param {string} key
* @return {Promise<number>}
*/
zcard(key) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('zcard', key);
});
}
/**
* Removes the specified members from the sorted set stored at key
* @param {string} key
* @param {string} value
* @return {Promise<number>}
*/
zrem(key, value) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('zrem', key, value);
});
}
/**
* Delete all keys of current selected DB
* @return {Promise<string>}
*/
flushdb() {
return __awaiter(this, void 0, void 0, function* () {
return yield this.executeCommand('flushdb');
});
}
}
exports.RedisConnection = RedisConnection;
//# sourceMappingURL=connection.js.map