---
title: "Apply a UDF"
description: "Learn how to apply Aerospike Record UDFs using the Node.js client for single records, batches, and background queries."
---

# Apply a UDF

> For the complete documentation index see: [llms.txt](https://aerospike.com/docs/llms.txt)
> 
> All documentation pages available in markdown.

Jump to the [Code block](#code-block) for a combined complete example.

UDFs that execute on a single record are Record UDFs. The record may or may not exist in the database, and the UDF may create, read, and/or update a record.

## Setup

The following examples use the following setup to illustrate applying a record UDF.

```js
const Aerospike = await import("aerospike");

const batchType = Aerospike.batchType;

const exp = Aerospike.exp;

// Set hosts to your server's address and port

const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };

// Establishes a connection to the server

const client = await Aerospike.connect(config);
```

The record structure:

```text
Occurred: Integer

Reported: Integer

Posted: Integer

Report: Map

{

    shape: List,

    summary: String,

    city: String,

    state: String,

    duration: String

}

Location: GeoJSON
```

## Single record commands

### Single argument

This example uses the `setRecent` function in the module `example.lua`. See [Manage UDFs](https://aerospike.com/docs/develop/client/node/usage/udf/manage) for `example.lua` code and information on registering the UDF.

This compares the `posted` bin to a supplied value and writes `true`/`false` to a `recent` bin based on the result.

```js
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000

const key = new Aerospike.Key("sandbox", "ufodata", 5000);

// Creates a UDF object

const udf = {

  module: "example",

  funcname: "setRecent",

  args: [20210101],

};

// Execute the UDF

await client.apply(key, udf);

// View the updated record

const record = await client.get(key);

console.info("Record: %o", record.bins);
```

### Multiple argument

This example uses the `getDaysBetween` function in the module `example.lua`. See [Manage UDFs](https://aerospike.com/docs/develop/client/node/usage/udf/manage) for `example.lua` code and information on registering the UDF.

This compares two dates, provided by passing the bin names with the date values, and returns the days between. The following example returns the days between the sighting occurrence and posting to the site.

```js
// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000

const key = new Aerospike.Key("sandbox", "ufodata", 5000);

// Creates a UDF object

const udf = {

  module: "example",

  funcname: "getDaysBetween",

  args: ["occurred", "posted"],

};

// Execute the UDF

const result = await client.apply(key, udf);

// output the result of the UDF

console.info("%o days between occurrence and post", result);
```

## Batch operation with a UDF

::: note
Server version 6.0.0 and above is required for batch UDF operations
:::

This example uses the multi-argument example from above, and apply it to a batch of records.

```js
// Create batch of keys

const keys = Array.from(

  { length: 10 },

  (_, i) => new Aerospike.Key("sandbox", "ufodata", i + 1),

);

// Creates a UDF object

const udf = {

  module: "example",

  funcname: "getDaysBetween",

  args: ["occurred", "posted"],

};

// Execute the UDF

const batchResult = await client.batchApply(keys, udf);

// Access the records

for (const { record } of batchResult) {

  console.info("%o days between occurrence and post", record.bins.SUCCESS);

}

// Close the connection to the server

await client.close();
```

## Background query with a UDF

This example will use the single argument example from above, and apply it to all records that match a Filter Expression through a background query.

```js
// Create new write policy

const writePolicy = new Aerospike.WritePolicy({

  filterExpression: exp.binExists("posted"),

});

// Create query

const query = client.query("sandbox", "ufodata");

// Execute the query

const job = await query.background(

  "example",

  "setRecent",

  [20210101],

  writePolicy,

);

// Return the query status

const jobInfo = await job.info();

switch (jobInfo.status) {

  case 0:

    console.info("Query not found");

    break;

  case 1:

    console.info("Query in progress," + jobInfo.progressPct + "% complete");

    break;

  case 2:

    console.info("Query complete," + jobInfo.recordsRead + " records read");

    break;

}

// Close the connection to the server

await client.close();
```

## Code block

Expand this section for a single code block to apply a record UDF

```js
const Aerospike = await import("aerospike");

// Set hosts to your server's address and port

const config = { hosts: "YOUR_HOST_ADDRESS:YOUR_PORT" };

// Establishes a connection to the server

const client = await Aerospike.connect(config);

// Creates a key with the namespace "sandbox", set "ufodata", and user key 5000

const key = new Aerospike.Key("sandbox", "ufodata", 5000);

// Creates a UDF object

const udf = {

  module: "example",

  funcname: "getDaysBetween",

  args: ["occurred", "posted"],

};

// Execute the UDF

await client.apply(key, udf);

await client.close();
```