Language Constructs

Language constructs are fundamental units of a language. Javascript is the language to be used while creating Functions. Using Google v8 as the execution container, Couchbase Functions inherit support for most ECMAScript language constructs.

To support Couchbase Functions requirement, three new constructs are added and three of the existing JavaScript constructs are removed.

Added Language Constructs

Following constructs have been added.

Removed Language Constructs

Following constructs have been removed.

  • Global State

  • Asynchrony

  • Browser and other Extensions

Global State

Functions do not allow global variables.

All states must be saved and retrieved from the persistence providers. In the Couchbase Data Platform, the data service is used as the persistence provider. Data Service bucket(s) stores all the global states information. Using the bindings entity, Functions can access these global states.

For the Function-logic to remain agnostic of the rebalance operation, this restriction on usage of global variables is mandatory.

var count = 0;        // This is a global variable and is not supported.

function OnUpdate(doc, meta)
{
  count++;
}

Asynchrony

Access to parent scope is mandatory for Asynchrony, particularly asynchronous callback. Access to parent scope results in a node-specific, long-running state that prevents the capture of entire long-running state in the persistence providers. With this, the Function handlers become short-running, straight-line code, without sleep and wakeups. For this reason the asynchronous callback constructs are not supported.

Limited asynchrony is added back through time observers. Time observers can be configured not to make the state node-specific.

function OnUpdate(doc, meta)
{
  setTimeout(function(){}, 300); //synchronous flows are supported.
}

Browser and Other Extensions

A Function executes on Couchbase Server similar to the code that is visible in the browser. As a developer, you need to understand that the code displayed in the below example runs in the browser. The ‘window’ term in the code ‘window.XMLHttpRequest()’, is not a server-side construct but is in the context of a browser.

function OnUpdate(doc, meta)
{
  var rpc = window.XMLHttpRequest(); // Browser extension are not supported.
}