Adds increment operation to Redis (#1745)

This commit is contained in:
Omar Ajoue 2021-05-08 01:33:14 +02:00 committed by GitHub
parent 29842e2f5f
commit fc3ebf1a4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -54,6 +54,11 @@ export class Redis implements INodeType {
value: 'info', value: 'info',
description: 'Returns generic information about the Redis instance.', description: 'Returns generic information about the Redis instance.',
}, },
{
name: 'Increment',
value: 'incr',
description: 'Atomically increments a key by 1. Creates the key if it does not exist.',
},
{ {
name: 'Keys', name: 'Keys',
value: 'keys', value: 'keys',
@ -186,6 +191,60 @@ export class Redis implements INodeType {
], ],
}, },
// ----------------------------------
// incr
// ----------------------------------
{
displayName: 'Key',
name: 'key',
type: 'string',
displayOptions: {
show: {
operation: [
'incr',
],
},
},
default: '',
required: true,
description: 'Name of the key to increment.',
},
{
displayName: 'Expire',
name: 'expire',
type: 'boolean',
displayOptions: {
show: {
operation: [
'incr',
],
},
},
default: false,
description: 'Set a timeout on key?',
},
{
displayName: 'TTL',
name: 'ttl',
type: 'number',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
operation: [
'incr',
],
expire: [
true,
],
},
},
default: 60,
description: 'Number of seconds before key expiration.',
},
// ---------------------------------- // ----------------------------------
// keys // keys
// ---------------------------------- // ----------------------------------
@ -459,7 +518,7 @@ export class Redis implements INodeType {
resolve(this.prepareOutputData([{ json: convertInfoToObject(result as unknown as string) }])); resolve(this.prepareOutputData([{ json: convertInfoToObject(result as unknown as string) }]));
client.quit(); client.quit();
} else if (['delete', 'get', 'keys', 'set'].includes(operation)) { } else if (['delete', 'get', 'keys', 'set', 'incr'].includes(operation)) {
const items = this.getInputData(); const items = this.getInputData();
const returnItems: INodeExecutionData[] = []; const returnItems: INodeExecutionData[] = [];
@ -517,6 +576,19 @@ export class Redis implements INodeType {
await setValue(client, keySet, value, expire, ttl, keyType); await setValue(client, keySet, value, expire, ttl, keyType);
returnItems.push(items[itemIndex]); returnItems.push(items[itemIndex]);
} else if (operation === 'incr') {
const keyIncr = this.getNodeParameter('key', itemIndex) as string;
const expire = this.getNodeParameter('expire', itemIndex, false) as boolean;
const ttl = this.getNodeParameter('ttl', itemIndex, -1) as number;
const clientIncr = util.promisify(client.incr).bind(client);
// @ts-ignore
const incrementVal = await clientIncr(keyIncr);
if (expire === true && ttl > 0) {
const clientExpire = util.promisify(client.expire).bind(client);
await clientExpire(keyIncr, ttl);
}
returnItems.push({json: {[keyIncr]: incrementVal}});
} }
} }