Creating the N1QL User-Defined Function
Before you can run the JavaScript function you have created (see Creating a JavaScript Library), you will need to create a N1QL User-Defined Function to reference it. You can create a N1QL User-Defined Function by using:
-
the UDF UI in the Query Workbench.
-
the standard N1QL
CREATE FUNCTIONDDL, using the External Functions option to reference the Javascript function. -
the REST-API to execute a
CREATE FUNCTIONcall.
For more information on N1QL User Defined Functions in general, read User-Defined Functions
-
Query Workbench
-
REST API
-
N1QL
-
Access the
UDFscreen from the administration console.
-
Click on the
+ add functionlink from theUDFsscreen.
which will display the
Add Functionscreen.
-
Use the
Namespacedrop-down lists to select the bucket and scope where your Javascript function resides. -
Fill in the
Function Nameof your N1QL User-Defined Function. -
Leave the
Parametersas they are.The …in the parameters box denotes a variable length list of parameters. This is why you don’t have to fill in this field. -
Select
Javascriptfor the function type. A field will appear in the dialog with a list of available libraries in the namespace you selected.
From this list select the library containing your function.
-
Enter the name of the JavaScript function in the
Library Function Namefield.
Run a curl command from the shell to add a N1QL User-Defined Function that calls your Javascript function.
curl -v http://localhost:8093/query/service \
-u Administrator:password \
-d 'statement=CREATE FUNCTION default:`travel-sample`.inventory.GetBusinessDays(...)
LANGUAGE JAVASCRIPT as "getBusinessDays" AT "travel-sample/inventory/my-library"'
Execute the CREATE FUNCTION in the CBQ Shell to create the N1QL User-Defined Function:
CREATE FUNCTION default:`travel-sample`.`inventory`.GetBusinessDays(...) LANGUAGE JAVASCRIPT as "getBusinessDays" AT "travel-sample/inventory/my-library";
| The N1QL User-Defined Function will take the same scope as the JavaScript UDF it is referencing. |
Having created your N1QL User-Defined Function, the next step is to use N1QL statement to call the function.