Global Variable in Eventing function

We have a common eventing script for 3 buckets, we want to use a variable which we can define at the settings level in the eventing function and then use it inside the function as for all three buckets it is different. We are aware of bucket alias, and URL alias but this is not related to that, it’s just a different global variable info.
Let me know if there a way to achieve this.

@jon.strabala @vsr1

Hia @Ashish_Mishra,

In 5.5-6.6 there is not concept of a global, with that said you can of course read your global config in via KV

function OnUpdate(doc, meta) {
    // You would typically filter to mutations of interest
    if (meta.id !== 'doc_prefix_of_interest:') return;
    var config = cfg_bkt["common_config"];
    // then pass the config object around to other functions if needed
    
    // USER CODE
}

You have more options in our upcoming 7.0.0 release (you can even try the beta right now). In eventing we have a new binding called a “Constant alias” which is exactly what you are asking for.

You could also use the REST API to deploy your code, and it isn’t too difficult to “inject” the needed variables into a function via command line utilities like sed based on the bucket name. However be forewarned this approach modifying the function directly outside the UI is not supported. However come 7.0.0. there are more options to manipulate the function and its settings via the REST API, I think but am not positive that that you could add constant aliases as needed in 7.0.0 via REST avoiding the unsupported direct manipulation of the function body. In 7.0.0 you can use the REST API to extract and replace just the appcode (your JavaScript) so what you want is definitely doable using a CI/CD pipeline.

Best

Jon Strabala
Principal Product Manager - Server‌

Related to this topic. I have another question

Is it possible to do it?

Hi @nagarajan_subbaraj,

Currently this is not possible, yet in 7.0 you could use a constant alias but essentially you would be entering the same information twice in the settings, once for a bucket binding and a second time as a constant alias. It will work but it is not ideal.

As above you could utilize the same pattern but you would expend two bucket ops on every mutation not very elegant but it would work.

function OnUpdate(doc, meta) {
    // You would typically filter to mutations of interest
    if (meta.id !== 'doc_prefix_of_interest:') return;
    var source_config = source["common_config"];
    var target_config = target["common_config"];
    // then pass the config object around to other functions if needed
    
    // USER CODE
}

Come 7.0.1 you should be able to use the GET advanced bucket accessor in where you can cache the value for up to a second. This will give a big performance boost 25X or so for reading near static lookup table (or in your case the alias to real bucket binding).

This would be a useful feature the ability to look up the alias bindings such that a N1QL() statement can be constructed with the bucket (6.6) or keyspace (7.0) used by the alias. Please DM me directly and we will discuss your use case as I would like to prioritise this.

Thanks

Jon Strabala

If what you want to do is set the expiry time on items in a bucket you can used advanced bucket accessors and skip N1QL entirely should be about 5X faster than the Query service.

Refer to Function: Advanced Document Controlled Expiry | Couchbase Docs

Pay attention to the bottom of the example

Best

Jon Strabala
Principal Product Manager - Server‌