# Questions about creating D2 ui nodes and the node red data stores

**URL:** <https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316>\
**Category:** Dashboard\
**Tags:** dashboard-2\
**Created:** [28 May 2024 08:16 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316 "2024-05-28T08:16:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2024 08:16 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/1 "2024-05-28T08:16:36Z")

</div>

I am creating a D2 ui node and having some problems with the server side data store.  
Following the [docs](https://dashboard.flowfuse.com/contributing/widgets/third-party.html#data-retention-data-stores) and the example ui node I have, in `onInput()` in the node's js file

```auto
                // store the latest value in our Node-RED datastore
                base.stores.data.save(base, node, msg)
                // send it to any connected nodes in Node-RED
                send(msg)

```

Then in the .vue file I have

```auto
    mounted () {
        this.$socket.on('widget-load:' + this.id, (msg) => {
        ...

```

So when the browser connects or is refreshed it first receives the new config properties, then it receives a message containing the current data store. This works OK.

The problem I have is that if I edit the node and deploy then connected browsers receive the new config properties, but are also sent the message with the old data store contents, which may not be compatible with the new config.

I had expected that the server side data store would be cleared when a node is deployed, which appears not to be the case. So question 1. - Is this a bug or is there a reason for it?

The solution I came up with was to manually clear out the datastore when the node is recreated, so I added code to that in the .js file so that I now have

```auto
        RED.nodes.createNode(this, config)
        const node = this
        // which group are we rendering this widget
        const group = RED.nodes.getNode(config.group)
        const base = group.getBase()

        // clear the server side data store, this ensures it is cleared if the node is deployed
        console.log(`clearing server data store`)
        base.stores.data.save(base, node.id, {})
        ...

```

and I can see that is running as the console.log message appears in the log, but still the browser receives the old message with the invalid data. So question 2 is what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [28 May 2024 08:32 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/2 "2024-05-28T08:32:35Z")

</div>

> [@Colin](#):
>
> I had expected that the server side data store would be cleared when a node is deployed, which appears not to be the case. So question 1. - Is this a bug or is there a reason for it?

I didn't want to clear the data store at this point because in any of the cases I could think of, the `msg` history should still be applicable to a node, although now you've got me thinking about it, and I'm not sure. If we do a "full deploy" - that would clear `msg` history for _all_ UI nodes, and that feels a little too easy to do by accident?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 May 2024 09:41 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/3 "2024-05-28T09:41:18Z")

</div>

Do you know why my code does not clear it on deploy?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 May 2024 18:54 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/4 "2024-05-29T18:54:31Z")

</div>

And the answer as to why  
`base.stores.data.save(base, node.id, {})`  
does not clear down the data store to an empty message is that is should apparently be  
`base.stores.data.save(base, node, {})`  
So there appears to be a typo in the docs.

This API seems rather confusing as it appears that to get the current contents of the store one uses  
`base.stores.data.get(node.id)`  
but to set it one uses  
`base.stores.data.save(base, node, <data>)`

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [29 May 2024 23:38 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/5 "2024-05-29T23:38:10Z")

</div>

BTW Colin,  
You also have the option to clear the server datastore using  
`base.stores.data.clear(node.id);`  
After which the "get" from the datastore returns `undefined`  
(which is easier to determine as "empty" than a property-less object, though currently it will not be sent on `'widget-load'`)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2024 07:44 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/6 "2024-05-30T07:44:34Z")

</div>

Thanks, in fact either will do me, all I need to do is make sure that, after deploy, the data store does not contain information relating to the previous configuration, which may not be compatible with the new configuration.

Is the store API documented anywhere, or have you determined this from the code?

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [30 May 2024 08:19 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/7 "2024-05-30T08:19:50Z")

</div>

I took it from code in one of the examples

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [30 May 2024 09:04 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/8 "2024-05-30T09:04:19Z")

</div>

> [@Colin](#):
>
> So there appears to be a typo in the docs.

Sorry for the discrepancy Colin - I'll update the docs..

EDIT: Actually, the [docs](https://dashboard.flowfuse.com/contributing/guides/state-management.html#datastore-save) do list this already as it is, which docs were you seeing?

> [@Colin](#):
>
> Is the store API documented anywhere, or have you determined this from the code?

Docs: [State Management | Node-RED Dashboard 2.0](https://dashboard.flowfuse.com/contributing/guides/state-management.html#data-store-1)

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [30 May 2024 09:05 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/9 "2024-05-30T09:05:53Z")

</div>

> [@Colin](#):
>
> This API seems rather confusing as it appears that to get the current contents of the store one uses

Yeah, there are reasons, but I appreciate that's a confusion.

The reason is that `save` needs to check user permissions and any constraints set on a given node type (which relies on the `msg._client` value, so needs the full object).

The `get` is available to all, and so only needs the node's `id`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [30 May 2024 13:08 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/10 "2024-05-30T13:08:45Z")

</div>

> [@joepavitt](#):
>
> which docs were you seeing

The creating third party widgets guide [Building Third Party Widgets | Node-RED Dashboard 2.0](https://dashboard.flowfuse.com/contributing/widgets/third-party.html) says:

Then, whenever you want to store data in the datastore, you can do so with:

```auto
base.stores.data.save(base, node.id, msg)

```

I had not even seen the State Management guide. I think a link to there from the creating widgets guide would be good.

On the API, it seems odd to have to pass `base` across when it is referenced by `base.stores.data.save`.

---

<div class="post-metadata">

**Author:** ![joepavitt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/joepavitt/32/59722_2.png) [@joepavitt](https://discourse.nodered.org/u/joepavitt)\
**Post date:** [30 May 2024 15:20 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/11 "2024-05-30T15:20:51Z")

</div>

> [@Colin](#):
>
> On the API, it seems odd to have to pass `base` across when it is referenced by `base.stores.data.save`.

Yeah, I regret that, but the challenge now is backward compatibility - removing it would cause _a lot_ of headaches.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 June 2024 15:20 UTC](https://discourse.nodered.org/t/questions-about-creating-d2-ui-nodes-and-the-node-red-data-stores/88316/12 "2024-06-29T15:20:52Z")

</div>

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