# Data Storage

Local app database management.

# Manifest Declaration

You need to declare the use of this API in the manifest's features member:

{"name": "system.storage"}

# Module Import

Before using this service in a component, you need to import the module in the script section of the UX document.

import storage from '@system.storage' 

Or

let storage = require("@system.storage")

# Methods

This service has the following methods:

# get({key,default,success,fail,complete})

Method to read the value of a key-value pair stored in the app database.

# Arguments

This method requires an object with the following attributes:

  • key (string). Mandatory attribute with the index key.
  • default (string). Optional attribute with the value that will be returned by default if the key does not exist in the database.
  • success (function(res)). Optional callback function corresponding to the successful execution. The function has an argument that is a string with the value corresponding to the key in the database.
  • fail (function). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

TIP

In case default is not specified and key doesn't exist, the system will return the empty string.

Example:

storage.get({ 
  key: 'A1', 
  success:function(data){console.log("handling success");}, 
  fail: function(data, code) { 
    console.log("handling fail, code=" + code); 
  } 
})

# set({key,value,success,fail,complete})

Method to write and update the value of a key-value pair stored in the app database.

# Arguments

This method requires an object with the following attributes:

  • key (string). Mandatory attribute with the index key.
  • value (string). Optional attribute with the value that will be stored in the database. It cannot exceed 2 MB.
  • success (function(res)). Optional callback function corresponding to the successful execution.
  • fail (function). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Usage

If the new value is an empty string, the key-value pair will be removed from the database.

Example:

storage.set({ 
  key: 'A1', 
  value: 'V1', 
  success:function(data){console.log("handling success");}, 
  fail: function(data, code) { 
    console.log("handling fail, code=" + code); 
  } 
})

# clear({success,fail,complete})

Method to remove all the key-value pairs stored in the app database.

# Arguments

This method requires an object with the following attributes:

  • success (function(res)). Optional callback function corresponding to the successful execution.
  • fail (function). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Example:

storage.clear({ 
  success:function(data){console.log("handling success");}, 
  fail: function(data, code) { 
    console.log("handling fail, code=" + code); 
  } 
})

# delete({key,success,fail,complete})

Method to delete a key-value pair stored in the app database.

# Arguments

This method requires an object with the following attributes:

  • key (string). Mandatory attribute with the index key.
  • success (function(res)). Optional callback function corresponding to the successful execution.
  • fail (function). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Example:

storage.delete({ 
  key: 'A1', 
  success:function(data){console.log("handling success");}, 
  fail: function(data, code) { 
    console.log("handling fail, code=" + code); 
  } 
})

# key({index,success,fail,complete})

Method to get the key of a key-value pair stored in the app database by its numeric index.

# Arguments

This method requires an object with the following attributes:

  • index (number). Mandatory attribute with the index of the key.
  • success (function(res)). Optional callback function corresponding to the successful execution. The callback function has an argument that will contain the key identifier corresponding to the index.
  • fail (function). Optional callback function corresponding to a failed execution.
  • complete (function). Optional callback function corresponding to the end of the execution.

Usage

If the new value is an empty string, the key-value pair will be removed from the database.

Example:

storage.key({ 
    index: parseIndex, 
    success: function (ret) { 
        console.info('storage.key(): ', JSON.stringify(ret)) 
        prompt.showToast({message: 'key: ' + ret,}) 
    }, 
    fail: function (erromsg, errocode) { 
        prompt.showToast({ message: 'key fail --- ' + errocode + ':' + erromsg }); 
        console.info('key fail --- ' + errocode + ':' + erromsg) 
    }
})