# Support destructuring expression for context properties

**URL:** <https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831>\
**Category:** Feature Requests\
**Created:** [5 June 2020 13:19 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831 "2020-06-05T13:19:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 13:19 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/1 "2020-06-05T13:19:53Z")

</div>

Currently if I need several properties from the context store, each has to be read using a separate `global.get('foo')` call. It would clean up buch of my function nodes if it was possible to read all properties at once. The syntax could be for example: `global.getAll()` or just `global.get()` without arguments.

Examples:

```auto
// get multiple libs defined in settings.js
const {fs, moment} = global.getAll();

// get libs and individual functions within
const {moment, lodash: {isEmpty, isString} = global.getAll();

// get all node context props at once with default values if context is still empty
let {count = 0, total = 0} = context.getAll();

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 June 2020 13:46 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/2 "2020-06-05T13:46:47Z")

</div>

Why not create an object and store the object in context? I do it all the time.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 June 2020 13:47 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/3 "2020-06-05T13:47:37Z")

</div>

Try

```auto
const values = global.get(['foo','bar','baz']);

// values is now an array containin the three values

```

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 13:56 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/4 "2020-06-05T13:56:19Z")

</div>

> [@zenofmud](#):
>
> Why not create an object and store the object in context? I do it all the time.

Yep that's my current workaround for `settings.js`. I have an object called `fn` which contains the modules I use. It's still just not the same thing. 🙂

> [@knolleary](#):
>
> Try
> 
> ```auto
> const values = global.get(['foo','bar','baz']);
> 
> ```

Thanks, I had actually forgot this syntax but it still isn't perfect as the keys would need to be duplicated:

```auto
const [foo, bar, baz] = global.get(['foo','bar','baz']);

```

But it could be cleaner in some situations than declaring everything in their own line, so good to have it available.

Oh forgot to mention one detail in the OP. `.getAll()` would return an empty object if there's nothing stored.

---

<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:** [5 June 2020 14:45 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/5 "2020-06-05T14:45:33Z")

</div>

I am not sure you realise exactly what @zenofmud meant. For example you can do this

```auto
let myContext = global.get("myContext") || {}
myContext.x = 7
myContext.y = "something"
global.set("myContext", myContext)

```

then somewhere else

```auto
let myContext = global.get("myContext") || {}
// now use myContext.x and myContext.y or add new properties
// and obviously global.set it again if necessary

```

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 14:55 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/6 "2020-06-05T14:55:51Z")

</div>

I do realise this but it's just my preference to work directly with the values initially. I do collect them in to an object before storing to context if it makes sense.

This is just me wishing to be able to make more use of the "modern" 2015 JavaScript syntax available since Node 8.x. Mainly due to be able to use the same patterns I use at work.

So to sum the above, this is more or a syntactical preference type of feature request. I do understand and use workarounds around the lack of this feature. It was noticing the presence of `global.getKeys()` that made me think about this.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 June 2020 15:18 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/7 "2020-06-05T15:18:55Z")

</div>

The main issue is the potential performance cost. If you have a lot of data in context - particular in a synchronous store - then a `getAll` requires a lot of work which would be wasted if you then used destructuring to pull out one or two items.

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 15:22 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/8 "2020-06-05T15:22:35Z")

</div>

Yep I thought about this but I would happily take the hit as I doubt it to be anything I would notice. With that said, I was assuming the (memory store at least) to just be a single object in the background but I take this so that it isn't?

But yep, did not think about custom stores etc. Then again there might even be use cases for retrieving all the data? Of course that could be done using the `keys()` function...

---

<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:** [4 August 2020 15:22 UTC](https://discourse.nodered.org/t/support-destructuring-expression-for-context-properties/27831/9 "2020-08-04T15:22:49Z")

</div>

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