How to use data from redis on custom node execute method?

Within my custom node, I need to access Redis to retrieve data generated during a previous workflow execution.

Here’s a simplified outline of the execute method where this access is needed:

const input = this.getInputData(0)[0]

var sessionId = input.json.sessionId
if (sessionId) {
    const session = redis.get(sessionId) // Pseudocode to Get Data from redis using the sessionId as key
    if (session) {
        responseData = {
           isNewSession : false,
           ... other fields ...
        }
        // globalSessions[sessionId.toString()] = true
 else {
        responseData = {
           isNewSession : true,
           ... other fields ...
        }
        sessionData = { ... } 
        redis.set(sessionId, sessionData) // Pseudocode to set data on redis                 
}

const returnItem: INodeExecutionData = {
    json: {
        responseData: responseData,
    },
};

How can I achieve this? Specifically, my question is: How can I obtain a reference to the Redis instance from within my custom node’s code?

Furthermore, is it possible to use a more convenient or abstracted solution for this, perhaps similar to how the ‘IA Simple Memory’ node handles persistent data?

Ideally, I would like to offer both methods (direct Redis access and a potentially simpler alternative) so the user can choose the most suitable option. Any help or examples would be greatly appreciated

I suggest to use the redis nodes instead of trying with code nodes

Hi @Franz, thanks for your help.

The code I shared is a simplified example. The actual code is more extensive and involves more complex logic with additional Redis database interactions after the “if” statement.
I understand this could be achieved using standard Redis nodes.

However, this is a logical sequence that I will need to replicate in many future workflows. This is intended for integration with my system, so in the future, I will need to document it for external developers who must be able to understand it easily.

This is why I want to create a custom node (not just a simple code node) to handle this functionality. This way, a user can simply add the custom node to their workflow, and it will work.

A potential workaround is to create this logic within a separate workflow and call it from the main workflow. While this would work, it’s not as straightforward as installing my custom nodes (the user would need the package anyway) and just connecting a single node.