# Custom context storage plugins

**URL:** <https://discourse.nodered.org/t/custom-context-storage-plugins/95249>\
**Category:** Share Your Nodes\
**Created:** [8 February 2025 17:44 UTC](https://discourse.nodered.org/t/custom-context-storage-plugins/95249 "2025-02-08T17:44:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [10 February 2025 10:11 UTC](https://discourse.nodered.org/t/custom-context-storage-plugins/95249/2 "2025-02-10T10:11:13Z")

</div>

Good work on creating a bunch of storage adapters, will make many folks happy.

I was looking through the [codebase](https://github.com/sebenik/node-red-context/blob/master/packages/base/index.js) and I have a question about the set functionality.

I assume that the BaseContext [`set` method](https://github.com/sebenik/node-red-context/blob/55659138d198a5bdf9a0c847479709a4c2a767ef/packages/base/index.js#L73) ...

```auto
 set(scope, key, value, callback) {
    return this.queue.enqueue(async () => {
      if (!key) { throw new Error('Key is not defined'); }
      if (!callback || typeof callback !== 'function') { throw new Error('Callback is not a function'); }

      if (!Array.isArray(key)) { key = [key]; }
      if (!Array.isArray(value)) { value = [value]; }

      if (key.length !== value.length) { throw new Error('Number of values don\'t match number of keys'); }

```

... is called when I do something similar to ...

```auto
flow.set("responses", content, () => { node.send(msg) })  

```

... in a function node?

What I don't understand is the conversion to arrays of `key` and `value` in this case.

In my example above, the key is `"responses"` (i.e. string) and value is `content` which is an array. Now your code would convert the key to `["responses"]` and `content` remains an array. Then the lengths are compared but since `content` is an array of length \> 1, an error would be thrown.

But what I was trying to do (and what works with the memory storage of NR) is assign the responses key an array as value and not assign a collection of keys a single value each.

I understand that the underlying storage may or may not support arrays as values but that should not cause an error or if it does, then it should be something like "array values not supported by storage mechanism XYZ".

What am I missing here?

EDIT:

Having a looking at the existing [redis](https://github.com/node-red/node-red-context-redis/blob/fdba50a68dbe9008cc5f418d30c8c9badae7ced5/index.js#L278-L289) store:

```auto
Redis.prototype.set = function (scope, key, value, callback) {
    if (callback && typeof callback !== 'function') {
        throw new Error('Callback must be a function');
    }
    try {
        if (!Array.isArray(key)) {
            key = [key];
            value = [value];
        } else if (!Array.isArray(value)) {
            // key is an array, but value is not - wrap it as an array
            value = [value];
        }

```

the difference seems to be that if the key _isn't_ an array, then it becomes an array _and_ the value is wrapped in an extra array.

So in my example it becomes `["responses"]` and `[[... content array ...]]` - which do have the same length of one.

---

_[View the full topic](https://discourse.nodered.org/t/custom-context-storage-plugins/95249)._
