node-json-db icon indicating copy to clipboard operation
node-json-db copied to clipboard

Problems with recursively when push of data

Open MathieuAndrade opened this issue 3 years ago • 1 comments

Hello !

This library is very awesome and it is exactly what i search for my project.

But i have encountered a problem with nested array objects. When i save settings of my nodes, node-json-db recursively override all settings of all nodes in file, evenly if override params is set to false.

Here is my JSON File
{
   "nodes": [
        {
            "defaultName": "Dimmer",
            "defaultManufacturer": "Fibaro",
            "defaultModel": "Dimmer Switch",
            "type": "physical",
            "pluginId": "9a5d34c4-d8cf-4093-94f2-142005ebfd66",
            "pluginSelector": "2",
            "capabilities": [
                {
                    "defaultName": "Switch",
                    "type": "onoff"
                },
                {
                    "defaultName": "Dimmer",
                    "type": "brightness",
                    "settings": {
                        "min": 8,
                        "max": 70,
                        "step": 1
                    }
                }
            ]
        },
        {
            "defaultName": "Dimmer",
            "defaultManufacturer": "Fibaro",
            "defaultModel": "Dimmer Switch",
            "type": "physical",
            "pluginId": "9a5d34c4-d8cf-4093-94f2-142005ebfd66",
            "pluginSelector": "3",
            "capabilities": [
                {
                    "defaultName": "Switch",
                    "type": "onoff"
                },
                {
                    "defaultName": "Dimmer",
                    "type": "brightness",
                    "settings": {
                        "min": 8,
                        "max": 70,
                        "step": 1
                    }
                }
            ]
        },
        {
            "defaultName": "Dimmer",
            "defaultManufacturer": "Fibaro",
            "defaultModel": "Dimmer Switch",
            "type": "physical",
            "pluginId": "9a5d34c4-d8cf-4093-94f2-142005ebfd66",
            "pluginSelector": "4",
            "capabilities": [
                {
                    "defaultName": "Switch",
                    "type": "onoff"
                },
                {
                    "defaultName": "Dimmer",
                    "type": "brightness",
                    "settings": {
                        "min": 8,
                        "max": 70,
                        "step": 1
                    }
                }
            ]
        },
        {
            "defaultName": "Dimmer",
            "defaultManufacturer": "Fibaro",
            "defaultModel": "Dimmer Switch",
            "type": "physical",
            "pluginId": "9a5d34c4-d8cf-4093-94f2-142005ebfd66",
            "pluginSelector": "5",
            "capabilities": [
                {
                    "defaultName": "Switch",
                    "type": "onoff"
                },
                {
                    "defaultName": "Dimmer",
                    "type": "brightness",
                    "settings": {
                        "min": 8,
                        "max": 70,
                        "step": 1
                    }
                }
            ]
        },
        {
            "defaultName": "Dimmer",
            "defaultManufacturer": "Fibaro",
            "defaultModel": "Dimmer Switch",
            "type": "physical",
            "pluginId": "9a5d34c4-d8cf-4093-94f2-142005ebfd66",
            "pluginSelector": "6",
            "capabilities": [
                {
                    "defaultName": "Switch",
                    "type": "onoff"
                },
                {
                    "defaultName": "Dimmer",
                    "type": "brightness",
                    "settings": {
                        "min": 8,
                        "max": 70,
                        "step": 1
                    }
                }
            ]
        }
    ]
}

All brightness abilities settings are set to min=8 and max=70, but all settings are meant to be different.

If I put a console.log in my insert function my settings are good.

  public async insert(path: string, data: unknown, override = true): Promise<void> {
    console.dir(data, { depth: null });
    await this.database.push(path, data, override);
  }
Here is result of my console.log
{
  defaultName: 'Dimmer',
  defaultManufacturer: 'Fibaro',
  defaultModel: 'Dimmer Switch',
  type: 'physical',
  pluginId: '9a5d34c4-d8cf-4093-94f2-142005ebfd66',
  pluginSelector: '2',
  capabilities: [
    { defaultName: 'Switch', type: 'onoff' },
    {
      defaultName: 'Dimmer',
      type: 'brightness',
      settings: { min: 4, max: 99, step: 1 }
    }
  ]
}
14:04:32.540 Zwave ➡ Zwave node 2 registered
{
  defaultName: 'Dimmer',
  defaultManufacturer: 'Fibaro',
  defaultModel: 'Dimmer Switch',
  type: 'physical',
  pluginId: '9a5d34c4-d8cf-4093-94f2-142005ebfd66',
  pluginSelector: '3',
  capabilities: [
    { defaultName: 'Switch', type: 'onoff' },
    {
      defaultName: 'Dimmer',
      type: 'brightness',
      settings: { min: 1, max: 45, step: 1 }
    }
  ]
}
14:04:32.578 Zwave ➡ Zwave node 3 registered
{
  defaultName: 'Dimmer',
  defaultManufacturer: 'Fibaro',
  defaultModel: 'Dimmer Switch',
  type: 'physical',
  pluginId: '9a5d34c4-d8cf-4093-94f2-142005ebfd66',
  pluginSelector: '4',
  capabilities: [
    { defaultName: 'Switch', type: 'onoff' },
    {
      defaultName: 'Dimmer',
      type: 'brightness',
      settings: { min: 4, max: 99, step: 1 }
    }
  ]
}
14:04:32.631 Zwave ➡ Zwave node 4 registered
{
  defaultName: 'Dimmer',
  defaultManufacturer: 'Fibaro',
  defaultModel: 'Dimmer Switch',
  type: 'physical',
  pluginId: '9a5d34c4-d8cf-4093-94f2-142005ebfd66',
  pluginSelector: '5',
  capabilities: [
    { defaultName: 'Switch', type: 'onoff' },
    {
      defaultName: 'Dimmer',
      type: 'brightness',
      settings: { min: 22, max: 59, step: 1 }
    }
  ]
}
14:04:32.663 Zwave ➡ Zwave node 5 registered
If i stringify the data of nodes before i insert in json file, all data are good and not overridden
    "nodes": [
        "{\"defaultName\":\"Dimmer\",\"defaultManufacturer\":\"Fibaro\",\"defaultModel\":\"Dimmer Switch\",\"type\":\"physical\",\"pluginId\":\"9a5d34c4-d8cf-4093-94f2-142005ebfd66\",\"pluginSelector\":\"2\",\"capabilities\":[{\"defaultName\":\"Switch\",\"type\":\"onoff\"},{\"defaultName\":\"Dimmer\",\"type\":\"brightness\",\"settings\":{\"min\":4,\"max\":99,\"step\":1}}]}",
        "{\"defaultName\":\"Dimmer\",\"defaultManufacturer\":\"Fibaro\",\"defaultModel\":\"Dimmer Switch\",\"type\":\"physical\",\"pluginId\":\"9a5d34c4-d8cf-4093-94f2-142005ebfd66\",\"pluginSelector\":\"3\",\"capabilities\":[{\"defaultName\":\"Switch\",\"type\":\"onoff\"},{\"defaultName\":\"Dimmer\",\"type\":\"brightness\",\"settings\":{\"min\":1,\"max\":45,\"step\":1}}]}",
        "{\"defaultName\":\"Dimmer\",\"defaultManufacturer\":\"Fibaro\",\"defaultModel\":\"Dimmer Switch\",\"type\":\"physical\",\"pluginId\":\"9a5d34c4-d8cf-4093-94f2-142005ebfd66\",\"pluginSelector\":\"4\",\"capabilities\":[{\"defaultName\":\"Switch\",\"type\":\"onoff\"},{\"defaultName\":\"Dimmer\",\"type\":\"brightness\",\"settings\":{\"min\":4,\"max\":99,\"step\":1}}]}",
        "{\"defaultName\":\"Dimmer\",\"defaultManufacturer\":\"Fibaro\",\"defaultModel\":\"Dimmer Switch\",\"type\":\"physical\",\"pluginId\":\"9a5d34c4-d8cf-4093-94f2-142005ebfd66\",\"pluginSelector\":\"5\",\"capabilities\":[{\"defaultName\":\"Switch\",\"type\":\"onoff\"},{\"defaultName\":\"Dimmer\",\"type\":\"brightness\",\"settings\":{\"min\":22,\"max\":59,\"step\":1}}]}",
        "{\"defaultName\":\"Dimmer\",\"defaultManufacturer\":\"Fibaro\",\"defaultModel\":\"Dimmer Switch\",\"type\":\"physical\",\"pluginId\":\"9a5d34c4-d8cf-4093-94f2-142005ebfd66\",\"pluginSelector\":\"6\",\"capabilities\":[{\"defaultName\":\"Switch\",\"type\":\"onoff\"},{\"defaultName\":\"Dimmer\",\"type\":\"brightness\",\"settings\":{\"min\":8,\"max\":70,\"step\":1}}]}"
    ],

Is there a trick to avoid this comportement instead of stringify data ?

MathieuAndrade avatar Aug 16 '22 14:08 MathieuAndrade

Hello,

I don't think there is an issue with the library, but with the way you must be creating your object.

If you do something like :

const config = {
 brightness: 200
}

const node1 = {
	name: "Foo",
    config: config
}

const node2 = {
   name: "Bar",
   config: config
}

Both node1 and node2 will share the same reference to the same config object (it's not copied).

So when you do:

config.brightness = 100;

both node1.config.brightness and node2.config.brightness will be changed.

That the only thing that comes to mind that could cause the issue you're encountering.

Belphemur avatar Aug 26 '22 18:08 Belphemur