You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
enabled adding abritary commands
added test and add_command alias original and special char commands tweaked code spacing
This commit is contained in:
committed by
Ruben Bridgewater
parent
4f7f1adf50
commit
0437aa4985
4
index.js
4
index.js
@@ -1100,4 +1100,6 @@ exports.AggregateError = errorClasses.AggregateError;
|
||||
// Add all redis commands / node_redis api to the client
|
||||
require('./lib/individualCommands');
|
||||
require('./lib/extendedApi');
|
||||
require('./lib/commands');
|
||||
|
||||
//enables adding new commands (for modules and new commands)
|
||||
exports.addCommand = exports.add_command = require('./lib/commands');
|
@@ -17,11 +17,7 @@ var changeFunctionName = (function () {
|
||||
}
|
||||
}());
|
||||
|
||||
// TODO: Rewrite this including the invidual commands into a Commands class
|
||||
// that provided a functionality to add new commands to the client
|
||||
|
||||
commands.list.forEach(function (command) {
|
||||
|
||||
var addCommand = function (command) {
|
||||
// Some rare Redis commands use special characters in their command name
|
||||
// Convert those to a underscore to prevent using invalid function names
|
||||
var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1');
|
||||
@@ -61,8 +57,12 @@ commands.list.forEach(function (command) {
|
||||
}
|
||||
return this.internal_send_command(new Command(command, arr, callback));
|
||||
};
|
||||
//alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run)
|
||||
if (commandName !== command) {
|
||||
RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command];
|
||||
}
|
||||
if (changeFunctionName) {
|
||||
Object.defineProperty(RedisClient.prototype[command], 'name', {
|
||||
Object.defineProperty(RedisClient.prototype[commandName], 'name', {
|
||||
value: commandName
|
||||
});
|
||||
}
|
||||
@@ -104,10 +104,18 @@ commands.list.forEach(function (command) {
|
||||
this.queue.push(new Command(command, arr, callback));
|
||||
return this;
|
||||
};
|
||||
//alias commands with illegal function names (e.g. NR.RUN becomes NR_RUN and nr_run)
|
||||
if (commandName !== command) {
|
||||
Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
|
||||
}
|
||||
if (changeFunctionName) {
|
||||
Object.defineProperty(Multi.prototype[command], 'name', {
|
||||
Object.defineProperty(Multi.prototype[commandName], 'name', {
|
||||
value: commandName
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
commands.list.forEach(addCommand);
|
||||
|
||||
module.exports = addCommand;
|
||||
|
36
test/commands/addCommand.spec.js
Normal file
36
test/commands/addCommand.spec.js
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
|
||||
var config = require('../lib/config');
|
||||
var redis = config.redis;
|
||||
var assert = require('assert');
|
||||
|
||||
describe("The 'addCommand/add_command' method", function () {
|
||||
var client = redis.createClient();
|
||||
var testCommands = {
|
||||
newcommand : 'newcommand',
|
||||
nonJsSafe : 'really-new.command',
|
||||
jsSafe : 'really_new_command'
|
||||
};
|
||||
|
||||
it('camel case version exists', function () {
|
||||
assert.strictEqual(typeof redis.addCommand, 'function');
|
||||
});
|
||||
it('snake version exists', function () {
|
||||
assert.strictEqual(typeof redis.add_command, 'function');
|
||||
});
|
||||
it('does not already have the test standard command', function () {
|
||||
assert.strictEqual(client[testCommands.newcommand], undefined);
|
||||
});
|
||||
it('generates a new method for an added command', function () {
|
||||
redis.addCommand(testCommands.newcommand);
|
||||
assert.strictEqual(typeof client[testCommands.newcommand], 'function');
|
||||
});
|
||||
it('does not already have the test non-JS-safe command', function () {
|
||||
assert.strictEqual(client[testCommands.nonJsSafe], undefined);
|
||||
});
|
||||
it('converts illegal command names to JS-safe functions', function () {
|
||||
redis.addCommand(testCommands.nonJsSafe);
|
||||
assert.strictEqual(typeof client[testCommands.jsSafe], 'function');
|
||||
});
|
||||
client.quit();
|
||||
});
|
Reference in New Issue
Block a user