Skip to content

Apply a UDF

Single record

UDFs that execute on a single record are Record UDFs. The record may or may not exist in the database, which allows the UDF to update or create a record.

To invoke a Record UDF, use Client.execute_udf():

pub async fn execute_udf(
&self,
policy: &WritePolicy,
key: &Key,
server_path: &str,
function_name: &str,
args: Option<&[Value]>,
) -> Result<Option<Value>>

📖 API Reference: Client::execute_udf

Where,

  • policy: The policy governing the operation.
  • key: The key of the record on which to invoke the function.
  • server_path: The name of the package containing the named function, without the .lua extension.
  • udf_name: The UDF module that contains the function to invoke.
  • function_name: The function to invoke.
  • args: The optional function arguments.

This example defines a UDF in the module examples.lua:

function readBin(r, name)
return r[name]
end

readBin returns the value of record r in bin name.

The client application can invoke readBin on a record:

let result = client.execute_udf(&WritePolicy::default(),
&key, "examples", "readBin", Some(&[as_val!("name")]))
.await;

📖 API Reference: Client::execute_udf

key specifies the record to pass to the UDF as the parameter r.

Multiple arguments

If the UDF accepts multiple arguments, add each argument to client.execute_udf().

For example, if the following UDF is defined in example.lua:

function multiplyAndAdd(r, a, b)
return r['factor'] * a + b;
end

This multiplies the bin factor by a and adds b, and returns results to the caller. Then, to invoke multiplyAndAdd() from Rust, run:

let result = client.execute_udf(&WritePolicy::default(),
&key, "examples", "multiplyAndAdd", Some(&[as_val!(10), as_val!(5)]))
.await;

📖 API Reference: Client::execute_udf

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?