# Class: BackendScriptApi

## BackendScriptApi

Source:
  - services/backend_script_api.js, line 23

### Members

#### currentNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | Note | where script is currently executing. Don't mix this up with concept of active note

Source:
  - services/backend_script_api.js, line 27

#### originEntity

##### Properties:

Name | Type | Description
--- | --- | ---
`entity` | Entity | whose event triggered this executions

Source:
  - services/backend_script_api.js, line 29

#### refreshTree

Trigger tree refresh in all connected clients. This is required when some tree change happens in the backend.

Source:
  - services/backend_script_api.js, line 310

#### startNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | Note | where script started executing

Source:
  - services/backend_script_api.js, line 25

### Methods

#### createNote(parentNoteId, title, contentopt, extraOptionsopt) → {Promise.<{note: Note, branch: Branch}>}

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`parentNoteId` | string |  |  | create new note under this parent
`title` | string |  |  |
`content` | string | <optional> | "" |
`extraOptions` | CreateNoteExtraOptions | <optional> | {} |

Source:
  - services/backend_script_api.js, line 198

##### Returns:

object contains newly created entities note and branch

Type
  Promise.<{note: Note, branch: Branch}>

#### createNoteAndRefresh(parentNoteId, title, contentopt, extraOptionsopt) → {Promise.<{note: Note, branch: Branch}>}

Creates new note according to given params and force all connected clients to refresh their tree.

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`parentNoteId` | string |  |  | create new note under this parent
`title` | string |  |  |
`content` | string | <optional> | "" |
`extraOptions` | CreateNoteExtraOptions | <optional> | {} |

Source:
  - services/backend_script_api.js, line 211

##### Returns:

object contains newly created entities note and branch

Type
  Promise.<{note: Note, branch: Branch}>

#### ensureNoteIsAbsentFromParent(noteId, parentNoteId) → {Promise.<void>}

If there's a branch between note and parent note, remove it. Otherwise do nothing.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`parentNoteId` | string |

Source:
  - services/backend_script_api.js, line 159

##### Returns:

Type
  Promise.<void>

#### ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) → {Promise.<void>}

If there's no branch between note and parent note, create one. Otherwise do nothing.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`parentNoteId` | string |
`prefix` | string | if branch will be create between note and parent note, set this prefix

Source:
  - services/backend_script_api.js, line 149

##### Returns:

Type
  Promise.<void>

#### getAppInfo() → {Object|*}

Source:
  - services/backend_script_api.js, line 315

##### Returns:

- object representing basic info about running Trilium version

Type
  Object | *

#### getAttribute(attributeId) → {Promise.<(Attribute|null)>}

##### Parameters:

Name | Type | Description
--- | --- | ---
`attributeId` | string |

Source:
  - services/backend_script_api.js, line 69

##### Returns:

Type
  Promise.<(Attribute|null)>

#### getBranch(branchId) → {Promise.<(Branch|null)>}

##### Parameters:

Name | Type | Description
--- | --- | ---
`branchId` | string |

Source:
  - services/backend_script_api.js, line 62

##### Returns:

Type
  Promise.<(Branch|null)>

#### getDateNote(date) → {Promise.<(Note|null)>}

Returns day note for given date. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM-DD format

Source:
  - services/backend_script_api.js, line 239

##### Returns:

Type
  Promise.<(Note|null)>

#### getEntities(SQL, array) → {Promise.<Array.<Entity>>}

##### Parameters:

Name | Type | Description
--- | --- | ---
`SQL` | string | query
`array` | Array.<?> | of params

Source:
  - services/backend_script_api.js, line 94

##### Returns:

Type
  Promise.<Array.<Entity>>

#### getEntity(SQL, array) → {Promise.<(Entity|null)>}

Retrieves first entity from the SQL's result set.

##### Parameters:

Name | Type | Description
--- | --- | ---
`SQL` | string | query
`array` | Array.<?> | of params

Source:
  - services/backend_script_api.js, line 86

##### Returns:

Type
  Promise.<(Entity|null)>

#### getImage(imageId) → {Promise.<(Image|null)>}

##### Parameters:

Name | Type | Description
--- | --- | ---
`imageId` | string |

Source:
  - services/backend_script_api.js, line 76

##### Returns:

Type
  Promise.<(Image|null)>

#### getInstanceName() → {string|null}

Instance name identifies particular Trilium instance. It can be useful for scripts if some action needs to happen on only one specific instance.

Source:
  - services/backend_script_api.js, line 48

##### Returns:

Type
  string | null

#### getMonthNote(date) → {Promise.<(Note|null)>}

Returns month note for given date. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM format

Source:
  - services/backend_script_api.js, line 258

##### Returns:

Type
  Promise.<(Note|null)>

#### getNote(noteId) → {Promise.<(Note|null)>}

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |

Source:
  - services/backend_script_api.js, line 55

##### Returns:

Type
  Promise.<(Note|null)>

#### getNotesWithLabel(name, valueopt) → {Promise.<Array.<Note>>}

Retrieves notes with given label name & value

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | string |  | attribute name
`value` | string | <optional> | attribute value

Source:
  - services/backend_script_api.js, line 128

##### Returns:

Type
  Promise.<Array.<Note>>

#### getNoteWithLabel(name, valueopt) → {Promise.<(Note|null)>}

Retrieves first note with given label name & value

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`name` | string |  | attribute name
`value` | string | <optional> | attribute value

Source:
  - services/backend_script_api.js, line 138

##### Returns:

Type
  Promise.<(Note|null)>

#### getRootCalendarNote() → {Promise.<(Note|null)>}

Returns root note of the calendar.

Source:
  - services/backend_script_api.js, line 230

##### Returns:

Type
  Promise.<(Note|null)>

#### getWeekNote(date, options) → {Promise.<(Note|null)>}

Returns note for the first date of the week of the given date.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | in YYYY-MM-DD format
`options` | object | "startOfTheWeek" - either "monday" (default) or "sunday"

Source:
  - services/backend_script_api.js, line 249

##### Returns:

Type
  Promise.<(Note|null)>

#### getYearNote(year) → {Promise.<(Note|null)>}

Returns year note for given year. If such note doesn't exist, it is created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`year` | string | in YYYY format

Source:
  - services/backend_script_api.js, line 267

##### Returns:

Type
  Promise.<(Note|null)>

#### log(message)

Log given message to trilium logs.

##### Parameters:

Name | Type | Description
--- | --- | ---
`message` |  |

Source:
  - services/backend_script_api.js, line 222

#### searchForNote(searchString) → {Promise.<(Note|null)>}

This is a powerful search method - you can search by attributes and their values, e.g.: "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Description
--- | --- | ---
`searchString` | string |

Source:
  - services/backend_script_api.js, line 114

##### Returns:

Type
  Promise.<(Note|null)>

#### searchForNotes(searchString) → {Promise.<Array.<Note>>}

This is a powerful search method - you can search by attributes and their values, e.g.: "@dateModified =* MONTH AND @log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Description
--- | --- | ---
`searchString` | string |

Source:
  - services/backend_script_api.js, line 104

##### Returns:

Type
  Promise.<Array.<Note>>

#### setNoteToParent(noteId, prefix, parentNoteIdopt)

This method finds note by its noteId and prefix and either sets it to the given parentNoteId or removes the branch (if parentNoteId is not given). This method looks similar to toggleNoteInParent() but differs because we're looking up branch by prefix.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`noteId` | string |  |
`prefix` | string |  |
`parentNoteId` | string | <optional> |

Source:
  - services/backend_script_api.js, line 287

#### sortNotesAlphabetically(parentNoteId)

##### Parameters:

Name | Type | Description
--- | --- | ---
`parentNoteId` | string | this note's child notes will be sorted

Source:
  - services/backend_script_api.js, line 274

##### Returns:

Promise<void>

#### toggleNoteInParent(present, noteId, parentNoteId, prefix) → {Promise.<void>}

Based on the value, either create or remove branch between note and parent note.

##### Parameters:

Name | Type | Description
--- | --- | ---
`present` | boolean | true if we want the branch to exist, false if we want it gone
`noteId` | string |
`parentNoteId` | string |
`prefix` | string | if branch will be create between note and parent note, set this prefix

Source:
  - services/backend_script_api.js, line 171

##### Returns:

Type
  Promise.<void>

#### transactional(func) → {Promise.<?>}

This functions wraps code which is supposed to be running in transaction. If transaction already exists, then we'll use that transaction. This method is required only when script has label manualTransactionHandling, all other scripts are transactional by default.

##### Parameters:

Name | Type | Description
--- | --- | ---
`func` | function |

Source:
  - services/backend_script_api.js, line 300

##### Returns:

result of func callback

Type
  Promise.<?>