Inline Coding in CONNECT platform

As we discussed here, the core idea of CONNECT platform is that main logical flow of backend code is asynchronous. However, not all levels of the logic are necessarily asynchronous, so you would need to easily mix the asynchronous graphs of CONNECT with traditional synchronous code.

Fortunately, CONNECT platform is based on NodeJS, and you can easily integrate Javascript code pieces into the platform. There are two main ways to do that:

  1. Inline Codes: value and expression nodes already parse Javascript expressions. You can even write functions with zero or one arguments and write arbitrary amount of code easily right in the middle of a graph.
  2. Packages: for much more elaborate codes, like submodules, you can easily create Javascript modules, add a little bit of extra metadata that turns your module into a CONNECT package, and push it to some remote git repository. Then you can install it as a package on the “packages” section of your CONNECT project easily. You can even publish the packages to NPM and allow fellow CONNECT users to utilize your modules.

You can read on how to create CONNECT packages here. However, here we are going to cover how can you do inline code pieces on the platform.

Inline Codes Using Expressions

So this is how you would create an expression that gets a number and multiplies it by two:

fig. 1, how to add a usual expression

Now imagine if you wanted to get a list of numbers and multiply it by two, and for some reason you did not want to use Javascript’s filter, then you would do something like this:

fig. 2, doing inline code in an expression

Basically, when the object described in an expression or value is a function with zero arguments, the node will call the function and output its return value. In the context of this function, you also have access to all the inputs as they are named, as you can see in figure 2, that you can easily access a in the function’s body.

In the function’s context, you also have access to these other objects:

  • console which you can use for doing console logs and what not.
  • require function, which you can use to require any module you might need.
  • error function, which you can utilize to indicate an error has occurred. You can either pass it an error message (a string) or an Error object.
  • context : context is a shared object that is created for each request. All nodes that are invoked while processing that request can have access to it, read from it and write to it. For example, you can use context.req to access the express request object.

If you want to do a little bit of asynchronous logic in your code as well, then you can uses a function with one argument instead of zero:

For example, this code in an expression will run a node on path /something on items of a list. The done object is a function that you can call when you are done, and the data you pass to it would be the output of the expression. You can also call it without any arguments, and the output of the expression would be undefined .

Fun note: the is actually the way you would invoke a node from within Javascript, regardless of it being native itself (written in Javascript) or a graph created visually.

Whats Next?

Check out the rest of the guides maybe? ;)