Script examples

Script examples

 

Cloud_Edition_button.webp

This section contains examples in the form of code snippets to illustrate how the Script function can be used:

Examples

Tracking consumed volume

Example - Tracking consumed volume

To keep track of the consumed volume of this function:

state.node.consumedVolume = state.node.consumedVolume || 0; const shouldPush = (++state.node.consumedVolume % 10) === 0; if (shouldPush) { log.info("Reached chargeable volume, pushing event for charging"); // volumeDiscount is updated by another function in the stream await push({ charge: true, volumeDiscount: state.stream.volumeDiscount }); } else { log.info('Consumed volume is ${state.node.consumedVolume}'); }

Using store in Transform and Flush

Example - Using store in Transform and Flush

In Transform

To set the latest call ID to the payload call ID:

await store.set('latestCallId', JSON.stringify(data));

To fetch the value of the latest call ID:

const result = JSON.parse(await store.get('latestCallId'));

In Flush

To delete the latest call ID at the end of each transaction:

await store.del('latestCallId');

Set a timeout value

Example - Set a timeout value

You can set a timeout value by stating it to the system. In this example, we will set it to 3 seconds, and then the deployment will be triggered. 

// wait for 3 sec await new Promise(resolve => setTimeout(resolve, 3000)); // then push payload await push(payload)

Collect multiple files in a bucket, merge, and output as one file

Example - Collect multiple files in a bucket, merge, and output as one file

You can collect multiple files, merge them, and forward them as one file by:

  1. Configuring the forwarder with a "custom filename".

  2. Caching all records in memory.

  3. Pushing all records to the bucket when meta.lastCall = TRUE.

Collecting multiple files consumes more memory. It is recommended not to use this design for large payloads.

Example of stream design 

Example code in script Transform

state.node.counter = state.node.counter || 0; state.node.recordBatch = state.node.recordBatch || []; // Cache all records in memory state.node.recordBatch.push(payload); state.node.counter++;

Example code in script Flush

// Send all cached records when stream terminates if (meta.lastCall) { log.info("Total Processed Record :: " + state.node.counter); for (let key in state.node.recordBatch) { await push(state.node.recordBatch[key]); } }

Stream sharing data using shared persistent storage

Example - Stream sharing data using shared persistent storage

To create a stream that shares data using shared persistent storage, you can design it like this:

Example stream design

Example code storing data in shared persistent storage

//Counter configuration: set to Bounded and Max value = 2 let key = "key_" + payload.value; let obj = { id: payload.value, value: payload.value * 2, date: new Date(), tag: "test" } await sharedStore.set(key, obj); await push(obj);

 Example code retrieving data from shared persistent storage

let key = "key_" + payload.value; let obj = await sharedStore.get(key); await push(obj);

Example npm libraries

Below are a few code examples of how you can use npm libraries.

Example - Using uuid npm library

To generate uuid of your choice,

const uuid = require("uuid/v4"); payload.id = uuid();

For further information on the library, see npm library | uuid.

Example - Using moment npm library

To parse, validate and format date,

const moment = require("moment"); payload.date = moment().format('YYYY-MM-DD')

For further information on the library, see npm library | moment.

Example - Using lodash npm library

To use JS lodash library,

const lodash = require("lodash"); payload.random = lodash.random(5,15);

For further information on the library, see npm library | lodash.

Example - Using fast-xml-parser npm library

To use the fast-xml-parser,

const { XMLParser, XMLValidator } = require('fast-xml-parser'); if (XMLValidator.validate(payload.xmlValueString)) { const parser = new XMLParser(); const jsonObj = parser.parse(payload.xmlValueString); await push(jsonObj); }

For further information on the library, see npm library | fast-xml-parser.

For further information on JavaScript, see DevDocs — JavaScript documentation.

For an example of how to use the Script function in a stream, see My first stream.