Modify the node instance definition from the runtime?

Can you update the values of a node instance from the runtime? I am looking to run data migration on existing nodes when they are loaded.

Welcome to the forum.
Can you give a specific example of what you want to do that we can better understand the requirement?

I would like to run a function, migrations, on the config of a node. Having it modify the existing config. So if the user edits the node in the editor it will be populated with this modified data instead of what was loaded from the flows file.

My use case is over time some of my nodes have vastly changed from what they started as and it would be nice to update them to a new schema from the backend instead of having the user have to click on each node in the editor where I currently do this sort of thing inside the oneditsave method.

function SampleNode(config) {
    // Is there some exposed method I can call here to edit the config from the runtime? RED.nodes.???
    const migratedConfig = migrations(config);
    RED.nodes.createNode(this,migratedConfig);
}

RED.nodes.registerType("sample",SampleNode);

function migrations(schema) {
    const newSchema = {
       ...schema,
       version: 1,
       newProperty: "randomValue"
    };

    return newSchema;
}

Config

{
"id": "73ad87bc.f4be28",
"type": "sample",
"name": "",
"wires": [[]]
}

Anything is possible, whether wise is a different matter :grin:

It is theoretically possible to run a script to manipulate your flows json file directly. It would take some careful working out due to the necessities of JSON formatting.

I goes without saying that Node-RED should be offline when doing this and that you should always have at least one backup before you start.

Yes, using an external script I always an option. I have already provided one to users for removing some information from exported flows while sharing. https://zachowj.github.io/node-red-contrib-home-assistant-websocket/scrubber/

But I was looking for an option that is more seamless for the user.

My latest thought was if there's not an exposed method to update a flow config, that I might fake a post to /flows and update them that way.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.