1
0
Fork 0
arangodb/Documentation/Books/Users/AqlExtending/Functions.mdpp

141 lines
3.7 KiB
Plaintext

!CHAPTER Registering and Unregistering User Functions
AQL user functions can be registered in the selected database
using the *aqlfunctions* object as follows:
```js
var aqlfunctions = require("@arangodb/aql/functions");
```
To register a function, the fully qualified function name plus the
function code must be specified.
The [HTTP Interface](../HttpAqlUserFunctions/README.md) also offers User Functions management.
!SUBSECTION Registering an AQL user function
`aqlfunctions.register(name, code, isDeterministic)`
Registers an AQL user function, identified by a fully qualified function
name. The function code in *code* must be specified as a JavaScript
function or a string representation of a JavaScript function.
If the function code in *code* is passed as a string, it is required that
the string evaluates to a JavaScript function definition.
If a function identified by *name* already exists, the previous function
definition will be updated. Please also make sure that the function code
does not violate the [Conventions](../AqlExtending/Conventions.md) for AQL
functions.
The *isDeterministic* attribute can be used to specify whether the
function results are fully deterministic (i.e. depend solely on the input
and are the same for repeated calls with the same input values). It is not
used at the moment but may be used for optimizations later.
The registered function is stored in the selected database's system
collection *_aqlfunctions*.
The function returns *true* when it updates/replaces an existing AQL
function of the same name, and *false* otherwise. It will throw an exception
when it detects syntactially invalid function code.
**Examples**
```js
require("@arangodb/aql/functions").register("myfunctions::temperature::celsiustofahrenheit",
function (celsius) {
return celsius * 1.8 + 32;
});
```
The function code will not be executed in *strict mode* or *strong mode* by
default. In order to make a user function being run in strict mode, use
`use strict` explicitly, e.g.:
```js
require("@arangodb/aql/functions").register("myfunctions::temperature::celsiustofahrenheit",
function (celsius) {
"use strict";
return celsius * 1.8 + 32;
});
```
!SUBSECTION Deleting an existing AQL user function
`aqlfunctions.unregister(name)`
Unregisters an existing AQL user function, identified by the fully qualified
function name.
Trying to unregister a function that does not exist will result in an
exception.
**Examples**
```js
require("@arangodb/aql/functions").unregister("myfunctions::temperature::celsiustofahrenheit");
```
!SUBSECTION Unregister Group
<!-- js/common/modules/@arangodb/aql/functions.js -->
delete a group of AQL user functions
`aqlfunctions.unregisterGroup(prefix)`
Unregisters a group of AQL user function, identified by a common function
group prefix.
This will return the number of functions unregistered.
**Examples**
```js
require("@arangodb/aql/functions").unregisterGroup("myfunctions::temperature");
require("@arangodb/aql/functions").unregisterGroup("myfunctions");
```
!SUBSECTION Listing all AQL user functions
`aqlfunctions.toArray()`
Returns all previously registered AQL user functions, with their fully
qualified names and function code.
The result may optionally be restricted to a specified group of functions
by specifying a group prefix:
`aqlfunctions.toArray(prefix)`
**Examples**
To list all available user functions:
```js
require("@arangodb/aql/functions").toArray();
```
To list all available user functions in the *myfunctions* namespace:
```js
require("@arangodb/aql/functions").toArray("myfunctions");
```
To list all available user functions in the *myfunctions::temperature* namespace:
```js
require("@arangodb/aql/functions").toArray("myfunctions::temperature");
```