Support destructuring expression for context properties

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:

// 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();

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

Try

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

// values is now an array containin the three values
1 Like

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. :slightly_smiling_face:

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

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.

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

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

then somewhere else

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

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.

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.

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...

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