fix(Redis Node): Fix issue with hash set not working as expected (#5832)

This commit is contained in:
Jon 2023-03-30 16:04:34 +01:00 committed by GitHub
parent 5bcab8fcbe
commit db2544146f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -349,7 +349,18 @@ export class Redis implements INodeType {
default: 'automatic', default: 'automatic',
description: 'The type of the key to set', description: 'The type of the key to set',
}, },
{
displayName: 'Value Is JSON',
name: 'valueIsJSON',
type: 'boolean',
displayOptions: {
show: {
keyType: ['hash'],
},
},
default: true,
description: 'Whether the value is JSON or key value pairs',
},
{ {
displayName: 'Expire', displayName: 'Expire',
name: 'expire', name: 'expire',
@ -605,6 +616,7 @@ export class Redis implements INodeType {
expire: boolean, expire: boolean,
ttl: number, ttl: number,
type?: string, type?: string,
valueIsJSON?: boolean,
) => { ) => {
if (type === undefined || type === 'automatic') { if (type === undefined || type === 'automatic') {
// Request the type first // Request the type first
@ -627,9 +639,26 @@ export class Redis implements INodeType {
await clientSet(keyName, value.toString()); await clientSet(keyName, value.toString());
} else if (type === 'hash') { } else if (type === 'hash') {
const clientHset = util.promisify(client.hset).bind(client); const clientHset = util.promisify(client.hset).bind(client);
for (const key of Object.keys(value)) { if (valueIsJSON) {
// @ts-ignore let values: unknown;
await clientHset(keyName, key, (value as IDataObject)[key]!.toString()); if (typeof value === 'string') {
try {
values = JSON.parse(value);
} catch {
// This is how we originally worked and prevents a breaking change
values = value;
}
} else {
values = value;
}
for (const key of Object.keys(values as object)) {
// @ts-ignore
await clientHset(keyName, key, (values as IDataObject)[key]!.toString());
}
} else {
const values = value.toString().split(' ');
//@ts-ignore
await clientHset(keyName, values);
} }
} else if (type === 'list') { } else if (type === 'list') {
const clientLset = util.promisify(client.lset).bind(client); const clientLset = util.promisify(client.lset).bind(client);
@ -742,10 +771,15 @@ export class Redis implements INodeType {
const keySet = this.getNodeParameter('key', itemIndex) as string; const keySet = this.getNodeParameter('key', itemIndex) as string;
const value = this.getNodeParameter('value', itemIndex) as string; const value = this.getNodeParameter('value', itemIndex) as string;
const keyType = this.getNodeParameter('keyType', itemIndex) as string; const keyType = this.getNodeParameter('keyType', itemIndex) as string;
const valueIsJSON = this.getNodeParameter(
'valueIsJSON',
itemIndex,
true,
) as boolean;
const expire = this.getNodeParameter('expire', itemIndex, false) as boolean; const expire = this.getNodeParameter('expire', itemIndex, false) as boolean;
const ttl = this.getNodeParameter('ttl', itemIndex, -1) as number; const ttl = this.getNodeParameter('ttl', itemIndex, -1) as number;
await setValue(client, keySet, value, expire, ttl, keyType); await setValue(client, keySet, value, expire, ttl, keyType, valueIsJSON);
returnItems.push(items[itemIndex]); returnItems.push(items[itemIndex]);
} else if (operation === 'incr') { } else if (operation === 'incr') {
const keyIncr = this.getNodeParameter('key', itemIndex) as string; const keyIncr = this.getNodeParameter('key', itemIndex) as string;