# Class: uci

## LuCI. uci

The `LuCI.uci` class utilizes `LuCI.rpc` to declare low level remote UCI `ubus` procedures and implements a local caching and data manipulation layer on top to allow for synchroneous operations on UCI configuration data.

#### new LuCI.uci()

uci.js, line 5

### Methods

#### add(config, type, name){string}

uci.js, line 276

  Adds a new section of the given type to the given configuration, optionally named according to the given name.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to add the section to.
  `type` | string | The type of the section to add.
  `name` | string | optional The name of the section to add. If the name is omitted, an anonymous section will be added instead.

  ##### Returns:

  Type | Description
  --- | ---
  string | Returns the section ID of the newly added section which is equivalent to the given name for non-anonymous sections.

#### apply(timeout){Promise.<number>}

uci.js, line 880

  Instructs the remote `ubus` UCI api to commit all saved changes with rollback protection and attempts to confirm the pending commit operation to cancel the rollback timer.

  Name | Type | Default | Description
  --- | --- | --- | ---
  `timeout` | number | 10 | optional Override the confirmation timeout after which a rollback is triggered.

  ##### Returns:

  Type | Description
  --- | ---
  Promise.<number> | Returns a promise resolving/rejecting with the `ubus` RPC status code.

#### changes(){Promise.<Object.<string, Array.<LuCI.uci.ChangeRecord>>>}

uci.js, line 960

  Fetches uncommitted UCI changes from the remote `ubus` RPC api.

  ##### Returns:

  Type | Description
  --- | ---
  Promise.<Object.<string, Array.<LuCI.uci.ChangeRecord>>> | Returns a promise resolving to an object containing the configuration names as keys and arrays of related change records as values.

#### createSID(config){string}

uci.js, line 95

  Generates a new, unique section ID for the given configuration.

  Note that the generated ID is temporary, it will get replaced by an identifier in the form `cfgXXXXXX` once the configuration is saved by the remote `ubus` UCI api.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The configuration to generate the new section ID for.

  ##### Returns:

  Type | Description
  --- | ---
  string | A newly generated, unique section ID in the form `newXXXXXX` where `X` denotes a hexadecimal digit.

#### get(config, sid, option){null|string|Array.<string>|LuCI.uci.SectionObject}

uci.js, line 451

  Gets the value of the given option within the specified section of the given configuration or the entire section object if the option name is omitted.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to read the value from.
  `sid` | string | The name or ID of the section to read.
  `option` | string | optional The option name to read the value from. If the option name is omitted or `null`, the entire section is returned instead.

  ##### Returns:

  Type | Description
  --- | ---

  null | string | Array.<string> | LuCI.uci.SectionObject

  - Returns a string containing the option value in case of a plain UCI option.
  - Returns an array of strings containing the option values in case of `option` pointing to an UCI list.
  - Returns a `section object` if the `option` argument has been omitted or is `null`.
  - Returns `null` if the config, section or option has not been found or if the corresponding configuration is not loaded.

#### get_first(config, type, option){null|string|Array.<string>|LuCI.uci.SectionObject}

uci.js, line 636

  Gets the value of the given option or the entire section object of the first found section of the specified type or the first found section of the entire configuration if no type is specfied.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to read the value from.
  `type` | string | optional The type of the first section to find. If it is `null`, the first section of the entire config is read, otherwise the first section matching the given type.
  `option` | string | optional The option name to read the value from. If the option name is omitted or `null`, the entire section is returned instead.

  ##### Returns:

  Type | Description
  --- | ---

  null | string | Array.<string> | LuCI.uci.SectionObject

  - Returns a string containing the option value in case of a plain UCI option.
  - Returns an array of strings containing the option values in case of `option` pointing to an UCI list.
  - Returns a `section object` if the `option` argument has been omitted or is `null`.
  - Returns `null` if the config, section or option has not been found or if the corresponding configuration is not loaded.

#### load(config){Promise.<Array.<string>>}

uci.js, line 212

  Loads the given UCI configurations from the remote `ubus` api.

  Loaded configurations are cached and only loaded once. Subsequent load operations of the same configurations will return the cached data.

  To force reloading a configuration, it has to be unloaded with `uci.unload()` first.

  Name | Type | Description
  --- | --- | ---
  `config` | string | Array.<string> | The name of the configuration or an array of configuration names to load.

  ##### Returns:

  Type | Description
  --- | ---
  Promise.<Array.<string>> | Returns a promise resolving to the names of the configurations that have been successfully loaded.

#### move(config, sid1, sid2, after){boolean}

uci.js, line 736

  Move the first specified section within the given configuration before or after the second specified section.

  Name | Type | Default | Description
  --- | --- | --- | ---
  `config` | string |  | The configuration to move the section within.
  `sid1` | string |  | The ID of the section to move within the configuration.

  `sid2`

  string

  optional

  The ID of the target section for the move operation. If the `after` argument is `false` or not specified, the section named by `sid1` will be moved before this target section, if the `after` argument is `true`, the `sid1` section will be moved after this section.

  When the `sid2` argument is `null`, the section specified by `sid1` is moved to the end of the configuration.

  `after` | boolean | false | optional When `true`, the section `sid1` is moved after the section `sid2`, when `false`, the section `sid1` is moved before `sid2`. If `sid2` is null, then this parameter has no effect and the section `sid1` is moved to the end of the configuration instead.

  ##### Returns:

  Type | Description
  --- | ---
  boolean | Returns `true` when the section was successfully moved, or `false` when either the section specified by `sid1` or by `sid2` is not found.

#### remove(config, sid)

uci.js, line 303

  Removes the section with the given ID from the given configuration.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to remove the section from.
  `sid` | string | The ID of the section to remove.

#### resolveSID(config, sid){string|null}

uci.js, line 126

  Resolves a given section ID in extended notation to the internal section ID value.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The configuration to resolve the section ID for.
  `sid` | string | The section ID to resolve. If the ID is in the form `@typename[#]`, it will get resolved to an internal anonymous ID in the forms `cfgXXXXXX`/`newXXXXXX` or to the name of a section in case it points to a named section. When the given ID is not in extended notation, it will be returned as-is.

  ##### Returns:

  Type | Description
  --- | ---
  string | null | Returns the resolved section ID or the original given ID if it was not in extended notation. Returns `null` when an extended ID could not be resolved to existing section ID.

#### save(){Array.<string>}

uci.js, line 790

  Submits all local configuration changes to the remove `ubus` api, adds, removes and reorders remote sections as needed and reloads all loaded configurations to resynchronize the local state with the remote configuration values.

  ##### Returns:

  Type | Description
  --- | ---
  Array.<string> | Returns a promise resolving to an array of configuration names which have been reloaded by the save operation.

#### sections(config, type, cb){Array.<LuCI.uci.SectionObject>}

uci.js, line 392

  Enumerates the sections of the given configuration, optionally filtered by type.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to enumerate the sections for.
  `type` | string | optional Enumerate only sections of the given type. If omitted, enumerate all sections.
  `cb` | LuCI.uci~sectionsFn | optional An optional callback to invoke for each enumerated section.

  ##### Returns:

  Type | Description
  --- | ---
  Array.<LuCI.uci.SectionObject> | Returns a sorted array of the section objects within the given configuration, filtered by type of a type has been specified.

#### set(config, sid, option, value)

uci.js, line 519

  Sets the value of the given option within the specified section of the given configuration.

  If either config, section or option is null, or if `option` begins with a dot, the function will do nothing.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to set the option value in.
  `sid` | string | The name or ID of the section to set the option value in.
  `option` | string | The option name to set the value for.
  `value` | null | string | Array.<string> | The option value to set. If the value is `null` or an empty string, the option will be removed, otherwise it will be set or overwritten with the given value.

#### set_first(config, type, option, value)

uci.js, line 671

  Sets the value of the given option within the first found section of the given configuration matching the specified type or within the first section of the entire config when no type has is specified.

  If either config, type or option is null, or if `option` begins with a dot, the function will do nothing.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to set the option value in.
  `type` | string | optional The type of the first section to find. If it is `null`, the first section of the entire config is written to, otherwise the first section matching the given type is used.
  `option` | string | The option name to set the value for.
  `value` | null | string | Array.<string> | The option value to set. If the value is `null` or an empty string, the option will be removed, otherwise it will be set or overwritten with the given value.

#### unload(config)

uci.js, line 244

  Unloads the given UCI configurations from the local cache.

  Name | Type | Description
  --- | --- | ---
  `config` | string | Array.<string> | The name of the configuration or an array of configuration names to unload.

#### unset(config, sid, option)

uci.js, line 605

  Remove the given option within the specified section of the given configuration.

  This function is a convenience wrapper around `uci.set(config, section, option, null)`.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to remove the option from.
  `sid` | string | The name or ID of the section to remove the option from.
  `option` | string | The name of the option to remove.

#### unset_first(config, type, option)

uci.js, line 701

  Removes the given option within the first found section of the given configuration matching the specified type or within the first section of the entire config when no type has is specified.

  This function is a convenience wrapper around `uci.set_first(config, type, option, null)`.

  Name | Type | Description
  --- | --- | ---
  `config` | string | The name of the configuration to set the option value in.
  `type` | string | optional The type of the first section to find. If it is `null`, the first section of the entire config is written to, otherwise the first section matching the given type is used.
  `option` | string | The option name to set the value for.

### Type Definitions

#### LuCI.uci.ChangeRecordArray.<string>

  An UCI change record is a plain array containing the change operation name as first element, the affected section ID as second argument and an optional third and fourth argument whose meanings depend on the operation.

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `0` | string | The operation name - may be one of `add`, `set`, `remove`, `order`, `list-add`, `list-del` or `rename`.
  `1` | string | The section ID targeted by the operation.

  `2`

  string

  The meaning of the third element depends on the operation.
  - For `add` it is type of the section that has been added
  - For `set` it either is the option name if a fourth element exists, or the type of a named section which has been added when the change entry only contains three elements.
  - For `remove` it contains the name of the option that has been removed.
  - For `order` it specifies the new sort index of the section.
  - For `list-add` it contains the name of the list option a new value has been added to.
  - For `list-del` it contains the name of the list option a value has been removed from.
  - For `rename` it contains the name of the option that has been renamed if a fourth element exists, else it contains the new name a section has been renamed to if the change entry only contains three elements.

  `4`

  string

  The meaning of the fourth element depends on the operation.
  - For `set` it is the value an option has been set to.
  - For `list-add` it is the new value that has been added to a list option.
  - For `rename` it is the new name of an option that has been renamed.

#### LuCI.uci.SectionObjectObject.<string, (boolean|number|string|Array.<string>)>

  A section object represents the options and their corresponding values enclosed within a configuration section, as well as some additional meta data such as sort indexes and internal ID.

  Any internal metadata fields are prefixed with a dot which is isn't an allowed character for normal option names.

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `.anonymous` | boolean | The `.anonymous` property specifies whether the configuration is anonymous (`true`) or named (`false`).
  `.index` | number | The `.index` property specifes the sort order of the section.
  `.name` | string | The `.name` property holds the name of the section object. It may be either an anonymous ID in the form `cfgXXXXXX` or `newXXXXXX` with `X` being a hexadecimal digit or a string holding the name of the section.
  `.type` | string | The `.type` property contains the type of the corresponding uci section.
  `*` | string | Array.<string> | A section object may contain an arbitrary number of further properties representing the uci option enclosed in the section. All option property names will be in the form `[A-Za-z0-9_]+` and either contain a string value or an array of strings, in case the underlying option is an UCI list.

#### sectionsFn(section, sid)

uci.js, line 360

  The sections callback is invoked for each section found within the given configuration and receives the section object and its associated name as arguments.

  Name | Type | Description
  --- | --- | ---
  `section` | LuCI.uci.SectionObject | The section object.
  `sid` | string | The name or ID of the section.