Persistence across restarts in custom node

I am creating a custom node which has some internal variables which I want to persist across restarts and I am trying to find an elegant way to do that in paradigm. The first time you load the node, you can set the values using the editor, but there is an option for the node to learn and update those default values automatically - I want those learned values to persist.

I have looked into the persist node and it can work, but I would prefer for the the learned variables to show up in the editor and not get set when the flow starts. I was thinking about storing the variables in global context and changing settings.js for the global context to persist and then maybe I can grab from the global context in "oneditprepare" - I am not sure if this will work though. If the node works how I want it to, I will release it for others to use and want it to work with minimal configuration - if the end user has to change a global setting (which could potentially break other nodes) or manage the persistence themselves (with persist nodes) it will not be as universal.

I just looking for ideas which I can research more and test, I'm not trying to post a generic "solve my problem for me" type of post. Hopefully you all have ideas.

Normally of course, variables for your node in the Editor are persisted when you hit the deploy button. There isn't really a node-red way to persist variables from the Editor other than that.

The other way would be to craft your own persistent store. You can do that in the .js file for your node and give access to the Editor via your own custom API. You would need to be careful about folder and file names for the persistence though.

One other possibility would be to use node-red's own variables - but use the context (e.g. node-specific) store and not global unless you really want the variable to be accessible outside your node. To get persistence though, your instance of node-red has to be configured for persistent variables.

If you want some examples of both of these approaches, you can look through the code for the uibuilder nodes.

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