Persistent storage


i'd like to store some values persistently in the global context. In my function node I have something like this:

var values = global.get('Status', "persistStore") || new Map();
values.forEach((value, key, map) =>
    // do some stuff
global.set('Status', values, "persistStore");

The following files always looks like this even though I know that something is in the values map as it is displayed correctly.

pi@raspberrypi:~ $ cat .node-red/context/global/global.json
"Status": {}

In my settings.js I have

// persistent storage
   contextStorage: {
        default     : {module: "memory"},
        persistStore: {module: "localfilesystem"}

and when starting there are no complaints and both storages are accepted like this.

Is it not possible to store the map as is?


With that code if you ever get an empty object into the status then it will just stay there, as the new Map() code will never be executed. Perhaps you saved that at some point and then modified the code. In the editor go to the context pane on the right and delete that entry. Then try the code again.

I suspect that is a different object that you are displaying. Perhaps you have forgotten to specify the correct store when you get it elsewhere.

Map type objects cannot be saved and restored from JSON which is how context stores values.

You'd need to use a vanilla JavaScript object

Okay, understood.

Might an error or a warning make sense if the provided object could not be stored?

Am I right in thinking that only apply to persistent stores? If so that explains why it appears to work until a restart I suppose.

Yes, this is true. Before I changed that, it was perfectly fine in memory.

However, I now ended up in doing that:

var m = new Map();
m.set("A", {a: "x", b: "y"});
m.set("B", {a: "a", b: "b"});

const obj1 = Object.fromEntries(m);
global.set("Test", obj1, "persistStore");

// do some stuff

obj2 = global.get("Test", "persistStore");
var k = new Map(Object.entries(obj2));

:sigh: I wish I had time to work on stores. I think there are lots of things that could be done. I'm fairly sure it would be possible to work out a way to serialise more complex objects for example. And I'd really like to find a way to use a nosql db as a store maybe CouchDB or PouchDB for example.

Most of all, I'd like to find a way to emit events from a store so that a flow could listen for updates.

I did start to look at GunDB but it is so bizarre that I gave up.

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