# Internal $D Methods

## Basic functions

### `$D.eval()` or `$D.getValueFromTemplate()` Getting the field value of the current object

```javascript
$D.getValueFromTemplate = function(fieldName[string])
// the same but shorter:
$D.eval = function(fieldName[string])

// examples 
var name = $D.getValueFromTemplate("{{name}}")  // name == Ivan

// you also can get values from linked objects 
var phone = $D.getValueFromTemplate("{{author_id.phone}}") // phone == 79141230000

// tip: for getting data from arrayLinks use $D.store.get()
```

### `$D.store.get()` Getting an object from a different structure

```javascript
$D.store.get = function(structName[string], objectID[string], fields[object])

// example 
var object = $D.store.get("WebUser", "7967", {fields: ["phone", "role"]});
// object == { role: "candidate,admin", phone: "79670190000", id: "7967" }
```

### `$D.store.save()` Saving an object

```javascript
$D.store.save = function(structName[string], obj[object], createEvent[boolean])

// example
var ID = "10341" 
$D.store.save("WebUser", {"id": ID, "stringF": "Changed string value" }, true)

// note that if createEvent == false, no event will be created, 
// so the scenarios will not trigger
```

### `$D.store.find` Searching for objects

`filters` (the second argument) syntax is the same for [API-filtering raw mode](/api-integrations/api-endpoints-security-layer/coding-mode-in-filters.md)

Result is an array of objects.

```javascript
var filters = [{
    "exp": "<=",
    "field": "x",
    "value": "10",
    "isExp": false
}]

$D.store.find('SystemMessage', filters, 
{
    "page":0, 
    "size":10, 
    "fields": ["id", "isError", "msg", "type", "userID"]
});

// EXAMPLE
// Important: use .forEach() here!
var data = $D.store.find("employees", "name = '" + first_name + "'", {fields: ["number"]});
  data.content.forEach(function(item){
      $D.console.log(item.number);
})
```

### `$D.store.export()` For exporting data to Excel

The contract is absolutely identical to `$D.store.find`&#x20;

Result = URL to the XLS-file.

### `$D.console.log()` Console logging

```javascript
$D.console.log = function(string)

// example
$D.console.log('a = ' + a)
```

### Dealing with context variables

```javascript
$D.context.get("test")
$D.context.set("test", "value111")
```

## Additional functions

### `$D.fs.download()` Saving files to the internal file storage&#x20;

This function downloads and saves files to the [File storage](/data/file-storage.md).

```javascript
$D.fs.download(fileUrl[string]) = function(string)

// example
var fileUrl = $D.fs.download("https://booble.com/files/logo.png")
// fileUrl == "https://api.directual.com/fileUploaded/uao/2a10d948-2e2e-49cb-9987-54f00cd6528d.png"
```

### `$D.fs.saveText()` Saving text to file

Function returns link to the file in Dirctual storage

```javascript
$D.fs.saveText(text[string], format[string]) = function(string, string)
//$D.fs.saveText('<html><body>test</body></html>', 'html')
//$D.fs.saveText('hello \n world', 'txt')
//$D.fs.saveText('1;2;3', 'csv')
```

### `$D.image.resize()` Resizing images&#x20;

This function resizes images and saves the result to the [File storage](/data/file-storage.md)

```javascript
$D.image.resize(fileUrl[string], params) = function(string)

//params is a json object like {"width": 100}, {"scale": .3}, or {"height": 130}
var resizedImageUrl = $D.image.resize("https://booble.com/files/logo.png", {"width": 100})
```

### `$D.concat()` Adding an element into array while avoiding duplication

```javascript
$D.concat = function(string, string)
// arrays in Directual are strings, comma separated

$D.concat('{{array}}', 'new element')
// or
$D.concat('{{array}}', '{{other_field}}')
// even or
$D.concat('{{array}}', '{{other_field_1}},{{other_field_2}}')
//
// e.g.
// $D.concat('', '3') returns 3
// $D.concat('1,2', '1,2,3') returns 1,2,3
// $D.concat('1,2,3', '3,4') returns 1,2,3,4
// $D.concat('1,2,3', '3') returns 1,2,3
```

### `$D.splice()` Removing elements from an array&#x20;

```javascript
$D.splice = function(string, string)
// arrays in Directual are strings, comma separated

$D.splice('{{array}}', 'removed element')
// or
$D.splice('{{array}}', '{{other_field}}')
// even or
$D.splice('{{array}}', '{{other_field_1}},{{other_field_2}}')
//
// e.g.
// $D.splice('', '3') returns ''
// $D.splice('1,2', '1,2,3') returns 3
// $D.splice('1,2,3', '3,4') returns 1,2
// $D.splice('1,2,3', '3') returns 1,2
```

### `$D.hash.md5()`, `$D.hash.bcrypt()`, `$D.hash.sha256()`

```javascript
function(string)

$D.hash.md5('hello world')
// returns 5eb63bbbe01eeed093cb22bb8f5acdc3

$D.hash.bcrypt('hello world')
// returns $2a$10$H.NnoiFyqDicSv8ufvW9iutzoHvLs0MsXWQk0ZVRhUcj9Mpgv.0/.

$D.hash.sha256('hello world')
// returns b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
```

{% hint style="info" %}
You can select the encryption method for user passwords in the [API security settings](/api-integrations/security-features.md#password-encryption).
{% endhint %}

### `$D.arrayContainsAny()` **Checking if an array contains at least one element**&#x20;

```javascript
$D.arrayContainsAny = function(string, string)

$D.arrayContainsAny("1,2,3","3,4") // returns true
$D.arrayContainsAny("1,2,3","4,5") // returns false
```

### `$D.arrayContainsAll()` **Checking if an array contains all elements**

```javascript
$D.arrayContainsAll = function(string, string)

$D.arrayContainsAll("1,2,3","3,4") // returns false
$D.arrayContainsAll("1,2,3","2,3") // returns true
```

### **`$D.json.fromXML()` Converting XML to JSON**

```javascript
$D.json.fromXML("{{xml}}")

// often there is a need to use the following templating features:
$D.json.fromXML("{‌{#escape}‌}{{#stripNewLine}‌}{{xml}}{‌{/stripNewLine}}{‌{/escape}‌}")
```

#### `$D.system.refreshIndex("servers")` for reindexing fields

## JWT Methods

Encode

If the 'expiration' (exp field) is undefined, set the current timestamp in seconds based on UTC time.

```
 //example code
DirectualEngine.addEventListener(AppEvents.START, function(context){
 var secret = $D.eval("{{secret}}") // get RS256 secret key
 var token = {
    "data": "",
    "kid":"b3fdfe5ebb9b60500eb96b05ece80d0ed294ed7f",
    "iss":"app@directual.com",
    "sub":"app@directual.com",  
    "aud":"https://api.directual.com/",
    "exp": 1628690000,
    "iat": 1628686000
  }
 return $D.jwt.encode(token, secret, "RS256");
 });

```

```
 $D.jwt.encode({
      "sub": "Directual",
      "iat": 1516239022
  }, "dTkJvdM0DroUP43X1faVKydVT6FpAUlaqur+y+r14mo=", "HS256")
```

```
 $D.jwt.encode({
      "sub": "Directual",
      "iat": 1516239022,
      "data": {"email":"test"}
  }, "dTkJvdM0DroUP43X1faVKydVT6FpAUlaqur+y+r14mo=", "HS256")
```

Decode

If the expiration date is earlier than the current time, this function will throw an exception.

```
var decodeResutl = $D.jwt.decode(payload, secret, "HS256")

$D.console.log(decodeResult.claim.subject)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://readme.directual.com/javascript-sdk/internal-usdd-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
