redis-connection-pool
Advanced tools
Comparing version 3.0.2 to 4.0.0
@@ -18,3 +18,3 @@ /// <reference types="node" /> | ||
*/ | ||
import { RedisClientOptions, RedisModules, RedisClientType } from 'redis'; | ||
import { RedisClientOptions, RedisClientType } from 'redis'; | ||
import { Pool } from 'generic-pool'; | ||
@@ -26,2 +26,8 @@ export interface RedisConnectionPoolConfig { | ||
} | ||
declare type SingleCommandResult = string | number | boolean | Buffer | { | ||
[x: string]: string | Buffer; | ||
} | { | ||
key: string | Buffer; | ||
element: string | Buffer; | ||
} | (string | Buffer)[]; | ||
declare type IdentifierType = string; | ||
@@ -63,234 +69,133 @@ /** | ||
redis: RedisClientOptions; | ||
pool: Pool<RedisClientType<RedisModules>>; | ||
pool: Pool<RedisClientType>; | ||
private initializing; | ||
constructor(cfg?: RedisConnectionPoolConfig); | ||
init(): Promise<void>; | ||
/** | ||
* Function: expire | ||
* Execute a redis BLPOP command | ||
* | ||
* Execute a redis EXPIRE command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* ttl - (number) - TTL in seconds | ||
* | ||
* @param key - The list key | ||
*/ | ||
expire(key: string, ttl: number): Promise<string | number | boolean | Buffer | (string | Buffer)[] | { | ||
key: string | Buffer; | ||
element: string | Buffer; | ||
} | { | ||
[x: string]: string | Buffer; | ||
blpop(key: string): Promise<{ | ||
key: string; | ||
element: SingleCommandResult; | ||
}>; | ||
/** | ||
* Function: del | ||
* Execute a redis BRPOP command | ||
* | ||
* Execute a redis DEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* | ||
* @param key - The list key | ||
*/ | ||
del(key: string): Promise<string | number | boolean | Buffer | (string | Buffer)[] | { | ||
key: string | Buffer; | ||
element: string | Buffer; | ||
} | { | ||
[x: string]: string | Buffer; | ||
brpop(key: string): Promise<{ | ||
key: string; | ||
element: SingleCommandResult; | ||
}>; | ||
/** | ||
* Function: keys | ||
* Execute a redis DEL command | ||
* | ||
* Execute a redis KEYS command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The prefix of the keys to return | ||
* | ||
* @param key - The key of the value you wish to delete | ||
*/ | ||
keys(key: string): Promise<Array<string>>; | ||
del(key: string): Promise<number>; | ||
/** | ||
* Function: hdel | ||
* Execute a redis EXPIRE command | ||
* | ||
* Execute a redis HDEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* fields - [string] - Array of field names to be deleted | ||
* | ||
* @param key - A key to assign value to | ||
* @param ttl - TTL in seconds | ||
*/ | ||
hdel(key: string, fields: Array<string>): Promise<string | number | boolean | Buffer | (string | Buffer)[] | { | ||
key: string | Buffer; | ||
element: string | Buffer; | ||
} | { | ||
[x: string]: string | Buffer; | ||
}>; | ||
expire(key: string, ttl: number): Promise<number>; | ||
/** | ||
* Function: sendCommand | ||
* Execute a redis GET command | ||
* | ||
* Sends an explicit command to the redis server. Helpful for new commands in redis | ||
* that aren't supported yet by this JS API. | ||
* | ||
* Parameters: | ||
* | ||
* command_name - (string) - The redis command to execute | ||
* args - (array) - The arguments to the redis command | ||
* | ||
* For eg: | ||
* send_command('HSET', ['firstRedisKey', 'key1', 'Hello Redis'] ) | ||
* @param key - The key of the value you wish to get | ||
*/ | ||
sendCommand(command_name: string, args: Array<string>): Promise<string | number | boolean | Buffer | (string | Buffer)[] | { | ||
key: string | Buffer; | ||
element: string | Buffer; | ||
} | { | ||
[x: string]: string | Buffer; | ||
}>; | ||
get(key: string): Promise<string>; | ||
/** | ||
* Function: ttl | ||
* Execute a redis HDEL command | ||
* | ||
* Execute a redis TTL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose TTL(time-to-expire) has to be returned | ||
* | ||
* @param key - The key of the value you wish to delete | ||
* @param fields - Array of additional field names to be deleted | ||
*/ | ||
ttl(key: string): Promise<any>; | ||
hdel(key: string, fields: Array<string>): Promise<number>; | ||
/** | ||
* Function: get | ||
* | ||
* Execute a redis GET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to get | ||
* | ||
*/ | ||
get(key: string): Promise<any>; | ||
/** | ||
* Function: hget | ||
* | ||
* Execute a redis HGET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* field - (string) - The field name to retrieve | ||
* | ||
* @param key - The key of the hash you wish to get | ||
* @param field - The field name to retrieve | ||
*/ | ||
hget(key: string, field: string): Promise<any>; | ||
hget(key: string, field: string): Promise<string>; | ||
/** | ||
* Function: hgetall | ||
* | ||
* Execute a redis HGETALL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* | ||
* @param key - The key of the hash you wish to get | ||
*/ | ||
hgetall(key: string): Promise<any>; | ||
hgetall(key: string): Promise<{ | ||
[index: string]: string; | ||
}>; | ||
/** | ||
* Function: blpop | ||
* Execute a redis HSET command | ||
* | ||
* Execute a redis BLPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key to assign the hash to | ||
* @param field - Name of the field to set | ||
* @param data - Value to assign to hash | ||
*/ | ||
blpop(key: string): Promise<any>; | ||
hset(key: string, field: string, data: string): Promise<number>; | ||
/** | ||
* Function: brpop | ||
* Execute a redis INCR command | ||
* | ||
* Execute a redis BRPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key whose value you wish to increment | ||
*/ | ||
brpop(key: string): Promise<any>; | ||
incr(key: string): Promise<number>; | ||
/** | ||
* Function: brpoplpush | ||
* | ||
* Execute a redis BRPOPLPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key1 - (string) - The pop list key | ||
* key2 - (string) - The push list key | ||
* | ||
* Initializes the Redis connection pool, connecting to redis. | ||
*/ | ||
brpoplpush(key1: string, key2: string): Promise<any>; | ||
init(): Promise<void>; | ||
/** | ||
* Function: incr | ||
* Execute a redis KEYS command | ||
* | ||
* Execute a redis INCR command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose value you wish to increment | ||
* | ||
* @param key - The prefix of the keys to return | ||
*/ | ||
incr(key: string): Promise<any>; | ||
keys(key: string): Promise<Array<string>>; | ||
/** | ||
* Function: set | ||
* Execute a redis LPUSH command | ||
* | ||
* Execute a redis SET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* data - (string) - Value to assign to key | ||
* ttl - (number) - optional TTL (Time to Live) in seconds | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
set(key: string, data: any, ttl?: number): Promise<string>; | ||
lpush(key: string, data: string): Promise<number>; | ||
/** | ||
* Function: hset | ||
* Execute a redis RPUSH command | ||
* | ||
* Execute a redis HSET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign the hash to | ||
* field - (string) - Name of the field to set | ||
* data - (string) - Value to assign to hash | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
hset(key: string, field: string, data: string): Promise<number>; | ||
rpush(key: string, data: string): Promise<number>; | ||
/** | ||
* Function: rpush | ||
* Sends an explicit command to the redis server. Helpful for all the commands in redis | ||
* that aren't supported natively by this pool API. | ||
* | ||
* Execute a redis RPUSH command | ||
* @param command_name - Name of redis command to execute | ||
* @param args - List of arguments for the redis command | ||
* | ||
* Parameters: | ||
* @example | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* sendCommand('ECHO', ['Hello Redis'] ) | ||
* | ||
*/ | ||
rpush(key: string, data: string): Promise<number>; | ||
sendCommand(command_name: string, args: Array<string>): Promise<SingleCommandResult>; | ||
/** | ||
* Function: lpush | ||
* Execute a redis SET command | ||
* | ||
* Execute a redis LPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* | ||
* @param key - A key to assign value to | ||
* @param data - Value to assign to key | ||
* @param ttl - TTL (Time to Live) in seconds | ||
*/ | ||
lpush(key: string, data: string): Promise<number>; | ||
set(key: string, data: string | number, ttl?: number): Promise<string | null>; | ||
/** | ||
* Function: shutdown | ||
* | ||
* Drain the pool and close all connections to Redis. | ||
*/ | ||
shutdown(): Promise<void>; | ||
/** | ||
* Execute a redis TTL command | ||
* | ||
* @param {string} key - A key whose TTL(time-to-expire) will be returned | ||
*/ | ||
shutdown(): Promise<void>; | ||
ttl(key: string): Promise<number>; | ||
private singleCommand; | ||
@@ -297,0 +202,0 @@ private getFuncs; |
@@ -90,61 +90,26 @@ "use strict"; | ||
} | ||
init() { | ||
/** | ||
* Execute a redis BLPOP command | ||
* | ||
* @param key - The list key | ||
*/ | ||
blpop(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.pool = (0, generic_pool_1.createPool)({ | ||
create: () => __awaiter(this, void 0, void 0, function* () { | ||
log('create'); | ||
if (this.initializing) { | ||
log('Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
throw Error('Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
} | ||
else { | ||
this.initializing = true; | ||
} | ||
const client = (0, redis_1.createClient)(this.redis); | ||
client.on('error', (err) => { | ||
throw new Error(err); | ||
}); | ||
client.on('ready', () => { | ||
log('ready'); | ||
}); | ||
log('connecting'); | ||
yield client.connect(); | ||
this.initializing = false; | ||
return client; | ||
}), | ||
destroy: (client) => __awaiter(this, void 0, void 0, function* () { | ||
yield client.quit(); | ||
}) | ||
}, { | ||
max: this.max_clients | ||
}); | ||
return yield this.getFuncs('BLPOP', key); | ||
}); | ||
} | ||
/** | ||
* Function: expire | ||
* Execute a redis BRPOP command | ||
* | ||
* Execute a redis EXPIRE command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* ttl - (number) - TTL in seconds | ||
* | ||
* @param key - The list key | ||
*/ | ||
expire(key, ttl) { | ||
brpop(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.singleCommand('EXPIRE', [key, ttl]); | ||
return yield this.getFuncs('BRPOP', key); | ||
}); | ||
} | ||
/** | ||
* Function: del | ||
* | ||
* Execute a redis DEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* | ||
* @param key - The key of the value you wish to delete | ||
*/ | ||
@@ -157,91 +122,38 @@ del(key) { | ||
/** | ||
* Function: keys | ||
* Execute a redis EXPIRE command | ||
* | ||
* Execute a redis KEYS command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The prefix of the keys to return | ||
* | ||
* @param key - A key to assign value to | ||
* @param ttl - TTL in seconds | ||
*/ | ||
keys(key) { | ||
expire(key, ttl) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.singleCommand('KEYS', [key]); | ||
return yield this.singleCommand('EXPIRE', [key, ttl]); | ||
}); | ||
} | ||
/** | ||
* Function: hdel | ||
* Execute a redis GET command | ||
* | ||
* Execute a redis HDEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* fields - [string] - Array of field names to be deleted | ||
* | ||
* @param key - The key of the value you wish to get | ||
*/ | ||
hdel(key, fields) { | ||
get(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.singleCommand('HDEL', [key, fields]); | ||
return yield this.getFuncs('GET', key); | ||
}); | ||
} | ||
/** | ||
* Function: sendCommand | ||
* Execute a redis HDEL command | ||
* | ||
* Sends an explicit command to the redis server. Helpful for new commands in redis | ||
* that aren't supported yet by this JS API. | ||
* | ||
* Parameters: | ||
* | ||
* command_name - (string) - The redis command to execute | ||
* args - (array) - The arguments to the redis command | ||
* | ||
* For eg: | ||
* send_command('HSET', ['firstRedisKey', 'key1', 'Hello Redis'] ) | ||
* @param key - The key of the value you wish to delete | ||
* @param fields - Array of additional field names to be deleted | ||
*/ | ||
sendCommand(command_name, args) { | ||
hdel(key, fields) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.singleCommand(command_name, args); | ||
return yield this.singleCommand('HDEL', [key].concat(fields)); | ||
}); | ||
} | ||
/** | ||
* Function: ttl | ||
* | ||
* Execute a redis TTL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose TTL(time-to-expire) has to be returned | ||
* | ||
*/ | ||
ttl(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('TTL', key); | ||
}); | ||
} | ||
/** | ||
* Function: get | ||
* | ||
* Execute a redis GET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to get | ||
* | ||
*/ | ||
get(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('GET', key); | ||
}); | ||
} | ||
/** | ||
* Function: hget | ||
* | ||
* Execute a redis HGET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* field - (string) - The field name to retrieve | ||
* | ||
* @param key - The key of the hash you wish to get | ||
* @param field - The field name to retrieve | ||
*/ | ||
@@ -254,10 +166,5 @@ hget(key, field) { | ||
/** | ||
* Function: hgetall | ||
* | ||
* Execute a redis HGETALL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* | ||
* @param key - The key of the hash you wish to get | ||
*/ | ||
@@ -270,78 +177,85 @@ hgetall(key) { | ||
/** | ||
* Function: blpop | ||
* Execute a redis HSET command | ||
* | ||
* Execute a redis BLPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key to assign the hash to | ||
* @param field - Name of the field to set | ||
* @param data - Value to assign to hash | ||
*/ | ||
blpop(key) { | ||
hset(key, field, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('BLPOP', key); | ||
const client = yield this.pool.acquire(); | ||
const res = client.HSET(key, field, data); | ||
yield this.pool.release(client); | ||
return res; | ||
}); | ||
} | ||
/** | ||
* Function: brpop | ||
* Execute a redis INCR command | ||
* | ||
* Execute a redis BRPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key whose value you wish to increment | ||
*/ | ||
brpop(key) { | ||
incr(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('BRPOP', key); | ||
return yield this.getFuncs('INCR', key); | ||
}); | ||
} | ||
/** | ||
* Function: brpoplpush | ||
* | ||
* Execute a redis BRPOPLPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key1 - (string) - The pop list key | ||
* key2 - (string) - The push list key | ||
* | ||
* Initializes the Redis connection pool, connecting to redis. | ||
*/ | ||
brpoplpush(key1, key2) { | ||
init() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('BRPOPLPUSH', key1, key2); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.pool = (0, generic_pool_1.createPool)({ | ||
create: () => __awaiter(this, void 0, void 0, function* () { | ||
log('create'); | ||
if (this.initializing) { | ||
log('Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
throw Error('Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
} | ||
else { | ||
this.initializing = true; | ||
} | ||
const client = (0, redis_1.createClient)(this.redis); | ||
client.on('error', (err) => { | ||
throw new Error(err); | ||
}); | ||
client.on('ready', () => { | ||
log('ready'); | ||
}); | ||
log('connecting'); | ||
yield client.connect(); | ||
this.initializing = false; | ||
return client; | ||
}), | ||
destroy: (client) => __awaiter(this, void 0, void 0, function* () { | ||
yield client.quit(); | ||
}) | ||
}, { | ||
max: this.max_clients | ||
}); | ||
}); | ||
} | ||
/** | ||
* Function: incr | ||
* Execute a redis KEYS command | ||
* | ||
* Execute a redis INCR command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose value you wish to increment | ||
* | ||
* @param key - The prefix of the keys to return | ||
*/ | ||
incr(key) { | ||
keys(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('INCR', key); | ||
return yield this.singleCommand('KEYS', [key]); | ||
}); | ||
} | ||
/** | ||
* Function: set | ||
* Execute a redis LPUSH command | ||
* | ||
* Execute a redis SET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* data - (string) - Value to assign to key | ||
* ttl - (number) - optional TTL (Time to Live) in seconds | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
set(key, data, ttl = 0) { | ||
lpush(key, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const client = yield this.pool.acquire(); | ||
const res = client.SET(key, data, { "EX": ttl }); | ||
const res = client.LPUSH(key, data); | ||
yield this.pool.release(client); | ||
@@ -352,17 +266,11 @@ return res; | ||
/** | ||
* Function: hset | ||
* Execute a redis RPUSH command | ||
* | ||
* Execute a redis HSET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign the hash to | ||
* field - (string) - Name of the field to set | ||
* data - (string) - Value to assign to hash | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
hset(key, field, data) { | ||
rpush(key, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const client = yield this.pool.acquire(); | ||
const res = client.HSET(key, field, data); | ||
const res = client.RPUSH(key, data); | ||
yield this.pool.release(client); | ||
@@ -373,35 +281,29 @@ return res; | ||
/** | ||
* Function: rpush | ||
* Sends an explicit command to the redis server. Helpful for all the commands in redis | ||
* that aren't supported natively by this pool API. | ||
* | ||
* Execute a redis RPUSH command | ||
* @param command_name - Name of redis command to execute | ||
* @param args - List of arguments for the redis command | ||
* | ||
* Parameters: | ||
* @example | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* sendCommand('ECHO', ['Hello Redis'] ) | ||
* | ||
*/ | ||
rpush(key, data) { | ||
sendCommand(command_name, args) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const client = yield this.pool.acquire(); | ||
const res = client.RPUSH(key, data); | ||
yield this.pool.release(client); | ||
return res; | ||
return yield this.singleCommand(command_name, args); | ||
}); | ||
} | ||
/** | ||
* Function: lpush | ||
* Execute a redis SET command | ||
* | ||
* Execute a redis LPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* | ||
* @param key - A key to assign value to | ||
* @param data - Value to assign to key | ||
* @param ttl - TTL (Time to Live) in seconds | ||
*/ | ||
lpush(key, data) { | ||
set(key, data, ttl = 0) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const client = yield this.pool.acquire(); | ||
const res = client.LPUSH(key, data); | ||
const res = client.SET(key, data, { "EX": ttl }); | ||
yield this.pool.release(client); | ||
@@ -412,6 +314,3 @@ return res; | ||
/** | ||
* Function: shutdown | ||
* | ||
* Drain the pool and close all connections to Redis. | ||
* | ||
*/ | ||
@@ -424,6 +323,16 @@ shutdown() { | ||
} | ||
singleCommand(funcName, functionParams) { | ||
/** | ||
* Execute a redis TTL command | ||
* | ||
* @param {string} key - A key whose TTL(time-to-expire) will be returned | ||
*/ | ||
ttl(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.getFuncs('TTL', key); | ||
}); | ||
} | ||
singleCommand(funcName, functionParams = []) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const client = yield this.pool.acquire(); | ||
const res = yield client[funcName](...(functionParams || [])); | ||
const res = yield client[funcName](...functionParams); | ||
yield this.pool.release(client); | ||
@@ -444,5 +353,2 @@ return res; | ||
} | ||
else if (funcName === 'BRPOPLPUSH') { | ||
res = yield client.BRPOPLPUSH(key, field, 0); | ||
} | ||
else if (funcName === 'HGET') { | ||
@@ -449,0 +355,0 @@ res = yield client.HGET(key, field); |
{ | ||
"name": "redis-connection-pool", | ||
"version": "3.0.2", | ||
"version": "4.0.0", | ||
"description": "a redis client connection pool", | ||
@@ -26,3 +26,4 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "tsc", | ||
"release": "npm run lint && npm run test && npm run coverage && npm run build && npm run integration && npm run doc", | ||
"build": "tsc -v && tsc", | ||
"lint:fix": "eslint --fix src/", | ||
@@ -33,3 +34,3 @@ "lint": "eslint src/", | ||
"integration": "mocha -r ts-node/register src/index.integration.ts && yarn run build && mocha dist/index.integration.js", | ||
"doc": "jsdoc2md --files ./src/index.ts --configure ./jsdoc2md.json > ./API.md" | ||
"doc": "typedoc ./src/index.ts" | ||
}, | ||
@@ -39,20 +40,20 @@ "dependencies": { | ||
"generic-pool": "^3.8.2", | ||
"redis": "^4.0.4" | ||
"redis": "^4.3.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "7.18.10", | ||
"@babel/core": "7.18.10", | ||
"@babel/core": "7.18.13", | ||
"@babel/preset-env": "7.18.10", | ||
"@babel/preset-typescript": "7.18.6", | ||
"@types/chai": "4.3.3", | ||
"@types/debug": "^4.1.7", | ||
"@types/debug": "4.1.7", | ||
"@types/eslint": "8.4.6", | ||
"@types/generic-pool": "3.1.11", | ||
"@types/mocha": "9.1.1", | ||
"@types/node": "17.0.23", | ||
"@types/node": "^17.0.23", | ||
"@types/proxyquire": "1.3.28", | ||
"@types/redis": "4.0.11", | ||
"@types/redis": "^4.0.11", | ||
"@types/sinon": "10.0.13", | ||
"@typescript-eslint/eslint-plugin": "5.33.1", | ||
"@typescript-eslint/parser": "5.33.1", | ||
"@typescript-eslint/eslint-plugin": "5.35.1", | ||
"@typescript-eslint/parser": "5.35.1", | ||
"c8": "7.12.0", | ||
@@ -68,3 +69,4 @@ "chai": "4.3.6", | ||
"ts-node": "10.9.1", | ||
"typescript": "4.7.4" | ||
"typedoc": "^0.23.11", | ||
"typescript": "4.8.2" | ||
}, | ||
@@ -71,0 +73,0 @@ "repository": { |
@@ -14,2 +14,3 @@ import { expect } from 'chai'; | ||
HSET: sinon.stub(), | ||
HDEL: sinon.stub(), | ||
GET: sinon.stub(), | ||
@@ -109,2 +110,9 @@ SET: sinon.stub(), | ||
it('hdel', async () => { | ||
clientFake.HDEL.returns(1); | ||
expect(await pool.hdel('identifier', ['identifier 2'])).to.eql(1); | ||
sinon.assert.calledOnce(clientFake.HDEL); | ||
sinon.assert.calledWith(clientFake.HDEL, 'identifier', 'identifier 2') | ||
}); | ||
it('rpush', async () => { | ||
@@ -111,0 +119,0 @@ await pool.rpush('identifier', 'a value'); |
363
src/index.ts
@@ -19,5 +19,3 @@ /** | ||
createClient, | ||
RedisClientOptions, | ||
RedisModules, | ||
RedisClientType | ||
RedisClientOptions, RedisClientType, | ||
} from 'redis'; | ||
@@ -36,4 +34,12 @@ import {createPool, Pool} from 'generic-pool'; | ||
type FuncNameType = 'HDEL' | 'DEL' | 'GET' | 'HGETALL' | 'TTL' | 'INCR' | | ||
'BRPOP' | 'HGET' | 'BLPOP' | 'BRPOPLPUSH' | 'EXPIRE' | 'KEYS' | ||
type SingleCommandResult = | ||
string | number | boolean | Buffer | { [x: string]: string | Buffer; } | | ||
{ key: string | Buffer; element: string | Buffer; } | (string | Buffer)[] | ||
/** | ||
* List of redis commands which have been implemented by the RedisConnectionPool class | ||
*/ | ||
type FuncNames = 'HDEL' | 'DEL' | 'GET' | 'HGETALL' | 'TTL' | 'INCR' | | ||
'BRPOP' | 'HGET' | 'BLPOP' | 'EXPIRE' | 'KEYS'; | ||
type IdentifierType = string; | ||
@@ -90,3 +96,3 @@ | ||
redis: RedisClientOptions; | ||
pool: Pool<RedisClientType<RedisModules>>; | ||
pool: Pool<RedisClientType>; | ||
private initializing = false; | ||
@@ -99,243 +105,157 @@ | ||
async init() { | ||
this.pool = createPool({ | ||
create: async () => { | ||
log('create'); | ||
if (this.initializing) { | ||
log( | ||
'Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
throw Error( | ||
'Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
} else { | ||
this.initializing = true; | ||
} | ||
const client = createClient(this.redis); | ||
client.on('error', (err) => { | ||
throw new Error(err); | ||
}); | ||
client.on('ready', () => { | ||
log('ready'); | ||
}); | ||
log('connecting'); | ||
await client.connect(); | ||
this.initializing = false; | ||
return client; | ||
}, | ||
destroy: async (client) => { | ||
await client.quit(); | ||
} | ||
}, { | ||
max: this.max_clients | ||
}); | ||
} | ||
/** | ||
* Function: expire | ||
* Execute a redis BLPOP command | ||
* | ||
* Execute a redis EXPIRE command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* ttl - (number) - TTL in seconds | ||
* | ||
* @param key - The list key | ||
*/ | ||
async expire(key: string, ttl: number) { | ||
return await this.singleCommand('EXPIRE', [key, ttl]); | ||
async blpop(key: string): Promise<{key: string, element: SingleCommandResult}> { | ||
return await this.getFuncs('BLPOP', key); | ||
} | ||
/** | ||
* Function: del | ||
* Execute a redis BRPOP command | ||
* | ||
* Execute a redis DEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* | ||
* @param key - The list key | ||
*/ | ||
async del(key: string) { | ||
return await this.singleCommand('DEL', [key]); | ||
async brpop(key: string): Promise<{key: string, element: SingleCommandResult}> { | ||
return await this.getFuncs('BRPOP', key); | ||
} | ||
/** | ||
* Function: keys | ||
* Execute a redis DEL command | ||
* | ||
* Execute a redis KEYS command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The prefix of the keys to return | ||
* | ||
* @param key - The key of the value you wish to delete | ||
*/ | ||
async keys(key: string): Promise<Array<string>> { | ||
return await this.singleCommand('KEYS', [key]) as Array<string>; | ||
async del(key: string): Promise<number> { | ||
return await this.singleCommand('DEL', [key]) as number; | ||
} | ||
/** | ||
* Function: hdel | ||
* Execute a redis EXPIRE command | ||
* | ||
* Execute a redis HDEL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to delete | ||
* fields - [string] - Array of field names to be deleted | ||
* | ||
* @param key - A key to assign value to | ||
* @param ttl - TTL in seconds | ||
*/ | ||
async hdel(key: string, fields: Array<string>) { | ||
return await this.singleCommand('HDEL', [key, fields]); | ||
async expire(key: string, ttl: number): Promise<number> { | ||
return await this.singleCommand('EXPIRE', [key, ttl]) as number; | ||
} | ||
/** | ||
* Function: sendCommand | ||
* Execute a redis GET command | ||
* | ||
* Sends an explicit command to the redis server. Helpful for new commands in redis | ||
* that aren't supported yet by this JS API. | ||
* | ||
* Parameters: | ||
* | ||
* command_name - (string) - The redis command to execute | ||
* args - (array) - The arguments to the redis command | ||
* | ||
* For eg: | ||
* send_command('HSET', ['firstRedisKey', 'key1', 'Hello Redis'] ) | ||
* @param key - The key of the value you wish to get | ||
*/ | ||
async sendCommand(command_name: string, args: Array<string>) { | ||
return await this.singleCommand(command_name as FuncNameType, args); | ||
async get(key: string): Promise<string> { | ||
return await this.getFuncs<string>('GET', key); | ||
} | ||
/** | ||
* Function: ttl | ||
* Execute a redis HDEL command | ||
* | ||
* Execute a redis TTL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose TTL(time-to-expire) has to be returned | ||
* | ||
* @param key - The key of the value you wish to delete | ||
* @param fields - Array of additional field names to be deleted | ||
*/ | ||
async ttl(key: string) { | ||
return await this.getFuncs('TTL', key); | ||
async hdel(key: string, fields: Array<string>): Promise<number> { | ||
return await this.singleCommand('HDEL', [key].concat(fields)) as number; | ||
} | ||
/** | ||
* Function: get | ||
* | ||
* Execute a redis GET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the value you wish to get | ||
* | ||
*/ | ||
async get(key: string) { | ||
return await this.getFuncs('GET', key); | ||
} | ||
/** | ||
* Function: hget | ||
* | ||
* Execute a redis HGET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* field - (string) - The field name to retrieve | ||
* | ||
* @param key - The key of the hash you wish to get | ||
* @param field - The field name to retrieve | ||
*/ | ||
async hget(key: string, field: string) { | ||
return await this.getFuncs('HGET', key, field); | ||
async hget(key: string, field: string): Promise<string> { | ||
return await this.getFuncs<string>('HGET', key, field); | ||
} | ||
/** | ||
* Function: hgetall | ||
* | ||
* Execute a redis HGETALL command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The key of the hash you wish to get | ||
* | ||
* @param key - The key of the hash you wish to get | ||
*/ | ||
async hgetall(key: string) { | ||
return await this.getFuncs('HGETALL', key); | ||
async hgetall(key: string): Promise<{[index: string]: string}> { | ||
return await this.getFuncs<{[index: string]: string}>('HGETALL', key); | ||
} | ||
/** | ||
* Function: blpop | ||
* Execute a redis HSET command | ||
* | ||
* Execute a redis BLPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key to assign the hash to | ||
* @param field - Name of the field to set | ||
* @param data - Value to assign to hash | ||
*/ | ||
async blpop(key: string) { | ||
return await this.getFuncs('BLPOP', key); | ||
async hset(key: string, field: string, data: string): Promise<number> { | ||
const client = await this.pool.acquire(); | ||
const res = client.HSET(key, field, data); | ||
await this.pool.release(client); | ||
return res; | ||
} | ||
/** | ||
* Function: brpop | ||
* Execute a redis INCR command | ||
* | ||
* Execute a redis BRPOP command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* | ||
* @param key - A key whose value you wish to increment | ||
*/ | ||
async brpop(key: string) { | ||
return await this.getFuncs('BRPOP', key); | ||
async incr(key: string): Promise<number> { | ||
return await this.getFuncs<number>('INCR', key); | ||
} | ||
/** | ||
* Function: brpoplpush | ||
* | ||
* Execute a redis BRPOPLPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key1 - (string) - The pop list key | ||
* key2 - (string) - The push list key | ||
* | ||
* Initializes the Redis connection pool, connecting to redis. | ||
*/ | ||
async brpoplpush(key1: string, key2: string) { | ||
return await this.getFuncs('BRPOPLPUSH', key1, key2); | ||
async init(): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
this.pool = createPool({ | ||
create: async () => { | ||
log('create'); | ||
if (this.initializing) { | ||
log( | ||
'Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
throw Error( | ||
'Create method already called. (Redis config error? ' + | ||
'or maybe you forgot to await the init function?)'); | ||
} else { | ||
this.initializing = true; | ||
} | ||
const client = createClient(this.redis); | ||
client.on('error', (err) => { | ||
throw new Error(err); | ||
}); | ||
client.on('ready', () => { | ||
log('ready'); | ||
}); | ||
log('connecting'); | ||
await client.connect(); | ||
this.initializing = false; | ||
return client; | ||
}, | ||
destroy: async (client) => { | ||
await client.quit(); | ||
} | ||
}, { | ||
max: this.max_clients | ||
}); | ||
} | ||
/** | ||
* Function: incr | ||
* Execute a redis KEYS command | ||
* | ||
* Execute a redis INCR command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key whose value you wish to increment | ||
* | ||
* @param key - The prefix of the keys to return | ||
*/ | ||
async incr(key: string) { | ||
return await this.getFuncs('INCR', key); | ||
async keys(key: string): Promise<Array<string>> { | ||
return await this.singleCommand('KEYS', [key]) as Array<string>; | ||
} | ||
/** | ||
* Function: set | ||
* Execute a redis LPUSH command | ||
* | ||
* Execute a redis SET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign value to | ||
* data - (string) - Value to assign to key | ||
* ttl - (number) - optional TTL (Time to Live) in seconds | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
async set(key: string, data: any, ttl = 0) { | ||
async lpush(key: string, data: string): Promise<number> { | ||
const client = await this.pool.acquire(); | ||
const res = client.SET(key, data, { "EX": ttl }); | ||
const res = client.LPUSH(key, data); | ||
await this.pool.release(client); | ||
@@ -346,16 +266,10 @@ return res; | ||
/** | ||
* Function: hset | ||
* Execute a redis RPUSH command | ||
* | ||
* Execute a redis HSET command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - A key to assign the hash to | ||
* field - (string) - Name of the field to set | ||
* data - (string) - Value to assign to hash | ||
* | ||
* @param key - The list key | ||
* @param data - Value to assign to the list | ||
*/ | ||
async hset(key: string, field: string, data: string) { | ||
async rpush(key: string, data: string): Promise<number> { | ||
const client = await this.pool.acquire(); | ||
const res = client.HSET(key, field, data); | ||
const res = client.RPUSH(key, data); | ||
await this.pool.release(client); | ||
@@ -366,33 +280,27 @@ return res; | ||
/** | ||
* Function: rpush | ||
* Sends an explicit command to the redis server. Helpful for all the commands in redis | ||
* that aren't supported natively by this pool API. | ||
* | ||
* Execute a redis RPUSH command | ||
* @param command_name - Name of redis command to execute | ||
* @param args - List of arguments for the redis command | ||
* | ||
* Parameters: | ||
* @example | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* sendCommand('ECHO', ['Hello Redis'] ) | ||
* | ||
*/ | ||
async rpush(key: string, data: string) { | ||
const client = await this.pool.acquire(); | ||
const res = client.RPUSH(key, data); | ||
await this.pool.release(client); | ||
return res; | ||
async sendCommand(command_name: string, args: Array<string>): Promise<SingleCommandResult> { | ||
return await this.singleCommand(command_name as FuncNames, args); | ||
} | ||
/** | ||
* Function: lpush | ||
* Execute a redis SET command | ||
* | ||
* Execute a redis LPUSH command | ||
* | ||
* Parameters: | ||
* | ||
* key - (string) - The list key | ||
* data - (string) - Value to assign to the list | ||
* | ||
* @param key - A key to assign value to | ||
* @param data - Value to assign to key | ||
* @param ttl - TTL (Time to Live) in seconds | ||
*/ | ||
async lpush(key: string, data: string) { | ||
async set(key: string, data: string|number, ttl = 0): Promise<string|null> { | ||
const client = await this.pool.acquire(); | ||
const res = client.LPUSH(key, data); | ||
const res = client.SET(key, data, { "EX": ttl }); | ||
await this.pool.release(client); | ||
@@ -403,8 +311,5 @@ return res; | ||
/** | ||
* Function: shutdown | ||
* | ||
* Drain the pool and close all connections to Redis. | ||
* | ||
*/ | ||
async shutdown() { | ||
async shutdown(): Promise<void> { | ||
await this.pool.drain(); | ||
@@ -414,5 +319,17 @@ await this.pool.clear(); | ||
private async singleCommand(funcName: FuncNameType, functionParams: Array<any>) { | ||
/** | ||
* Execute a redis TTL command | ||
* | ||
* @param {string} key - A key whose TTL(time-to-expire) will be returned | ||
*/ | ||
async ttl(key: string): Promise<number> { | ||
return await this.getFuncs<number>('TTL', key); | ||
} | ||
private async singleCommand( | ||
funcName: FuncNames, | ||
functionParams: Array<any> = [] | ||
): Promise<SingleCommandResult> { | ||
const client = await this.pool.acquire(); | ||
const res = await client[funcName](...(functionParams || [])); | ||
const res = await client[funcName](...functionParams); | ||
await this.pool.release(client); | ||
@@ -422,7 +339,7 @@ return res; | ||
private async getFuncs( | ||
funcName: FuncNameType, | ||
private async getFuncs<T>( | ||
funcName: FuncNames, | ||
key: string, | ||
field: string | undefined = undefined | ||
) { | ||
): Promise<T> { | ||
const client = await this.pool.acquire(); | ||
@@ -435,4 +352,2 @@ let res; | ||
res = await client[funcName](key, 0); | ||
} else if (funcName === 'BRPOPLPUSH') { | ||
res = await client.BRPOPLPUSH(key, field, 0); | ||
} else if (funcName === 'HGET') { | ||
@@ -439,0 +354,0 @@ res = await client.HGET(key, field); |
Sorry, the diff of this file is not supported yet
53092
27
1177
Updatedredis@^4.3.0