In this lecture, I'll be looking at some of the key nodes we've not yet looked at,
and explain why using some of the nodes in the function section
of the pallet is a better option than just using the function node.
The function node is a very useful node,
as it provides a way to incorporate custom code into a flow.
But it's often used in places where one of
the other standard nodes might be a better option.
The function node runs code defined in
its configuration within a sandbox using the node.js vm package.
The Sandbox provides a level of isolation from the core node-red run time.
Each function node has its own sandbox context,
which does incur a performance penalty as
a sandbox context has to be established when the node is created,
and then each time a message arrives at the function node,
the run time has to switch into the function node's context to run the code,
and then switch back out to the context when the function node is completed.
While the function node does provide a very useful capability of running custom code,
it's often overused to do simple tasks,
such as setting a property on the message object which can be
completed in one of the standard nodes available in the pallet.
The rest of this lecture,
I'll introduce you to a couple of useful nodes.
And the first one I'll look at is the change node.
The change node can be used to modify
a message object or the flow or global context objects.
You have configuration options to set or delete properties,
you can move or rename properties,
and you can search and replace property values.
You can also make multiple changes to any of the objects within a single change node.
When setting a property, you can access any existing property on the incoming message,
or the flow or global context objects.
You can specify a string,
a number, a boolean true or false value,
set an object value from JSON,
a buffer, a timestamp,
or the output of a JSONata expression.
And I'll be discussing JSON values, what a buffer is,
and JSONata expressions later in the video.
When changing a value,
you can search for a value to be replaced.
The search value can be specified in the node configuration,
or from another property in the incoming message,
or the flow or global context objects.
Regular expressions can be used to search for patterns in the property being changed,
and we'll discuss these later in the video.
You can specify a property of the incoming message,
or the flow or global context objects to be deleted,
and the move option moves the property specified.
It can be thought of as a set operation on
the destination and delete operation on the source object.
Looking at a couple of the options on the set and change options,
a JSON value can be specified when setting or changing a value.
If you select a JSON value,
you'll see the edit field has an ellipsis character at the end,
which allows you to open up a JSON editor.
This JSON editor that will help you validate the JSON.
And it's worth noting that you need to enter valid JSON in this field,
and not a JavaScript object literal definition,
which allows things such as single quotes to be used.
I've included a link in the resources section of the course,
where you can find further details of valid JSON syntax.
A buffer allows you to create a JSON array of byte values.
You can create an array of unsigned byte values.
If the input cannot be passed as an array of unsigned byte values,
then the input is taken as a UTF-8 string.
The buffer editor indicates how the content is being handled,
and the buffer array is shown in the lower panel.
A timestamp value is the number of milliseconds since midnight at start of day,
on the 1st of January 1970 at Greenwich Mean Time.
JSONata is a lightweight query and transformation language for JSON data,
which allows you to easily query, manipulate JSON data.
I'll cover the syntax of JSONata in another video,
but there's a link to the JSONata website in the course's resources section.
Hopefully, you'll see how useful
the change node can be at manipulating object properties.
So next, I'm going to look at the switch node.
If you're more familiar with traditional programming,
the switch node can be thought of as the branch operation,
similar to an if or a switch statement.
It allows you to take a different route through a flow
depending on a specified condition or rule.
You can specify one or more rules in the switch node configuration,
and can select if you want to apply all the rules against the test property,
or stop after the first match.
Matching goes from top to bottom.
There's also an option of providing an otherwise clause.
Note, the otherwise rule only fires if one
of the other conditions above it in the list does not fire.
If you select to stop after the first match,
then no rule after the otherwise rule will ever be considered.
So, the otherwise rule should always be placed at the bottom of the list.
Rules can be reordered by dragging them in the configuration panel.
There are a number of options for specifying the rules
including regular expressions which will be discussed in another lecture.
You can specify a property from the incoming message or the flow
or global context objects to compare the rule against.
You can also supply a JSONata expression to generate the value to compare against.
And again, JSONata syntax will be discussed in another video.
Each new rule you create will add an output connector to the switch node.
You can choose to name the connectors if you want, but by default,
you can see the rule associated to the output connected by hovering over it.
If you reorder the rules,
then the output connectors will also be reordered to match the rule order.
As an exercise, you should explore the other nodes in
the function section of the pallet to become familiar with how they work and
the capabilities they offer as using one of the standard nodes is
preferrable to writing custom code in a function node that does the same task.