Create a JavaScript Library
- Capella Operational
- how-to
How to create a JavaScript library to store and organize your JavaScript functions.
Introduction
You can create a JavaScript library for storing JavaScript functions. Creating a JavaScript library for your JavaScript functions is optional, but simplifies organization and access control for user-defined functions.
If you want to try out the examples in this section, follow the instructions given in Create an Account and Deploy Your Free Tier Operational Cluster to create a free account, deploy a cluster, and load a sample dataset.
Global and Scoped JavaScript Libraries
A JavaScript library can be global or scoped.
-
A global library is created within the
default:namespace, at the same level as the buckets in your database. A global library is available to all clients. -
A scoped library is created within a scope, at the same level as the collections within the scope. A scoped library is only available to clients that have access to that bucket and scope. Use a scoped JavaScript library to keep the code for user-defined functions separate.
The name of a JavaScript library must be unique within the specified namespace or scope.
JavaScript Functions
A JavaScript library can contain one or more JavaScript functions. The name of a JavaScript function must be unique within the JavaScript library.
For each JavaScript function, specify named parameters for any values you need to process or use.
If you want your JavaScript function to take a variable length list of parameters, specify a rest parameter for the JavaScript function, such as ... args.
After you create a JavaScript library and add JavaScript functions, you must create SQL++ user-defined functions to reference the JavaScript functions in the library, so they can be called as part of any SQL++ statement. A SQL++ user-defined function passes each argument it receives to the parameters in the JavaScript function.
Creating a JavaScript Library and Adding JavaScript Code
When you create a JavaScript library, you can add JavaScript functions to the library at the same time.
To create a JavaScript library:
-
On the Operational Clusters page, select the operational cluster where you want to work with user-defined functions.
-
Go to .
-
In the Data Insights area, to the left of the query editor, find the Functions section.
-
Next to the Functions section header, go to .
-
In the Library Name field, enter a name for your new JavaScript library.
-
Choose the access level for your JavaScript library:
-
Choose Global for a global library.
-
Choose Specific and select a bucket and scope for a scoped library.
-
-
(Optional) Add functions to your JavaScript library.
-
To manually define functions for your library, on the Define Functions tab, enter the code for each function.
-
To import a
.jsfile that contains function definitions, on the Import Library tab, drag and drop or choose your.jsfile.
-
-
Click Create.
The following library contains a JavaScript function called getBusinessDays.
function getBusinessDays(startDate, endDate) {
let count = 0;
const curDate = new Date(new Date(startDate).getTime());
while (curDate <= new Date(endDate)) {
const dayOfWeek = curDate.getDay();
if(dayOfWeek !== 0 && dayOfWeek !== 6)
count++;
curDate.setDate(curDate.getDate() + 1);
}
return count; (1)
}
Updating an Existing JavaScript Library
To add or edit functions in an existing JavaScript library:
-
On the Operational Clusters page, select the operational cluster where you want to work with user-defined functions.
-
Go to .
-
In the Data Insights area, to the left of the query editor, find the Functions section.
-
Next to the user-defined library that you want to update, go to .
-
Update the library to add new JavaScript functions, edit existing JavaScript functions, or both.
-
To manually define functions for your library, on the Create Functions tab, enter or edit the code for each function.
-
To import a
.jsfile that contains function definitions, on the Import Library tab, drag and drop or choose your.jsfile.
-
-
Click Update.
The following library contains JavaScript functions called getBusinessDays and sumListOfNumbers.
function getBusinessDays(startDate, endDate) {
let count = 0;
const curDate = new Date(new Date(startDate).getTime());
while (curDate <= new Date(endDate)) {
const dayOfWeek = curDate.getDay();
if(dayOfWeek !== 0 && dayOfWeek !== 6)
count++;
curDate.setDate(curDate.getDate() + 1);
}
return count;
}
function sumListOfNumbers(... args) {
var sum = 0;
args.forEach(value => sum = sum + value);
return sum;
}
Deleting a JavaScript Library
Before you can delete a library, you must first drop all SQL++ user-defined functions which point to any of the JavaScript functions within that library. For more information, see DROP FUNCTION.
To delete a JavaScript library:
-
On the Operational Clusters page, select the operational cluster where you want to delete a JavaScript library.
-
Go to .
-
In the Data Insights area, to the left of the query editor, find the Functions section.
-
Next to the JavaScript library that you want to delete, go to .
-
Confirm that you want to delete your JavaScript library.
-
Click Delete Library.