storages

October 22, 2022

storages

Stability: 2 - Stable

The storages module provides support for saving simple data, user configurations, etc. Saved data is retained unless the application is uninstalled or actively deleted.

storages supports data types such as number, boolean, string and serialized access to Object, Array with JSON.stringify.

The data stored in storages is shared between scripts, and any script that knows the name of the storage can access the corresponding data, so it cannot be used for storing sensitive data. storages cannot provide domain-independent storage like LocalStorage in web development, because the path to the script can change at any time.

storages.create(name)

  • name {string} LocalStorageName

Creates a local storage and returns a Storage object. Data from local stores with different names are separated, while data from local stores with the same name are shared.

For example, in a script that creates a storage with the name ABC and stores a=123:

var storage = storages.create("ABC");
storage.put("a", 123);

And in another script it is possible to get the value of ABC as well as a: ```

var storage = storages.create("ABC");
log("a = " + storage.get("a"));

Therefore, the name of the local storage is more important, try to use a name that contains unique information such as domain name, author email to avoid conflicts, for example

var storage = storages.create("2732014414@qq.com:ABC");

storages.remove(name)

  • name {string} local storage name

Removes a local store and all of its data. Returns false if the store does not exist; otherwise returns true.

Storages

Storage.get(key[, defaultValue])

  • key {string} key value
  • defaultValue {any} Optional, default value

Retrieve the data with key value key from the local storage and return it.

If the store does not contain the data, then the default value is returned if the default value parameter is specified, otherwise undefined is returned.

The data returned may be of any data type, depending on the data type when the key is stored using Storage.put.

Storage.put(key, value)

  • key {string} key value
  • value {any} value

Saves the value value to local storage. value can be any data type other than undefined. If value is undefined then TypeError is thrown.

The storage process actually uses JSON.stringify to convert the value to a string before saving it, so the value must be JSON-able to be accepted.

Storage.remove(key)

  • key {string} key

Removes the data whose key value is key. Does not return any value.

Storage.contains(key)

  • key {string} key

Returns whether this local storage contains data whose key is key. Returns true if it does, otherwise false.

Storage.clear()

Remove all data from this local storage. Does not return any value.

Last update:
Contributors: Bruce