So I finally started to use the "context" feature. I've been using a JSON-DB successfully for years.
I used global.set(query, data) where data is JSON.
This works. But when the "query" has a "." inside, the key is broken up in the resulting JSON database (the filesystem global/global.json) will break up the query into sub elements (3 in this example) .
My query is:
scott.your@gmail.com_123_uncle
The resulting stored JSON (in global.json). Notice the storage has broken my query on the "." (as far as I know).
This is by design. The "query" as you call it is the context variable you wan to write to. However, Node-RED is "clever" and allows you to read/write a specific property deep inside an object variable.
Node-RED is "clever" and allows you to read/write a specific property deep inside an object variable.
I don't see how this is "clever" - it took apart a STRING variable which isn't deep inside the object. It's at the root of my object. I wouldn't write to the your@gmail portion of my data storage.
The power of JSON and stored on the file system, is it can be grabbed, shown in JSON tools, exported, imported, edited (with node-red stopped).
Note: that a JSON array would be as shown in my last example, and something I did first. That array isn't modified by node-red. But I was hoping the sqlite DB would be more efficient if I used the query approach versus the array approach (as it scales).
Are there other special characters that node-red was "clever" is breaking my query?
A JavaScript variable/constant name containing period's is not directly a valid name. What is "clever" is that the context set function recognises this and takes apart the name and creates any missing (sub)properties and puts the value in the result.
As I say, this is not a valid JavaScript identifier.
You cannot do const scott.your@gmail.com_123_uncle = 42. The context set function uses the same naming restriction. The context variable name has to be a valid JS var name.
your@gmail would not be valid either. However, it is possible to have that as a property name.
You should convert the periods and @ symbol to different, acceptable symbols such as - or _.
Otherwise, use a different storage library and not Node-RED's context. DuckDB for example.
Indeed, but the 1st argument to that function is not JSON nor is it meant to be. The 2nd argument can, of course be JSON, or, more simply a JavaScript Object.
I just tried the "change" node setting `global.scott@gmail.com" -- and there are no error messages.
I also looked at the Context document and it doesn't mention this either. I realize that isn't valid javascript - but I would think it would be documented somewhere.
Nobody has ever raised this before so possibly not a common requirement. However, the devs are always looking for people to contribute - would be a good addition to the docs I think.
What exactly is it that you want to do that doesn't work? If you do global.set("scott.your@gmail.com_123_uncle", something) and later let myVar = gobal.get("scott.your@gmail.com_123_uncle") then you should get back what you set.
I had thought that setting a value was just a string and the object. Versus a "variable name" and an object, especially as the "context" was touted as the JSON Database replacement.