# Lightweight Storage Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value pairs. Keys are of the string type, and values can be of the numeric, string, or Boolean type. >![](../../public_sys-resources/icon-note.gif) **NOTE:** >The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import dataStorage from '@ohos.data.storage' ``` ## System Capabilities SystemCapability.DistributedDataManager.Preference.Core ## Required Permissions None ## Attributes

Name

Type

Readable

Writable

Description

MAX_KEY_LENGTH

string

Yes

No

Maximum length of a key. It is 80 bytes.

MAX_VALUE_LENGTH

string

Yes

No

Maximum length of a value of the string type. It is 8192 bytes.

## dataStorage.getStorageSync getStorageSync\(path: string\): Storage Reads a specified file and loads the data to the **Storage** instance for data operations in synchronous mode. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Return values

Type

Description

Storage

Storage instance used for data storage operations

- Example: ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' (async () => { var context = featureAbility.getContext() var path = await context.getFilesDir() let storage = dataStorage.getStorageSync(path + '/mystore') storage.putSync('startup', 'auto') storage.flushSync() })() ``` ## dataStorage.getStorage getStorage\(path: string, callback: AsyncCallback\): void Reads a specified file and loads the data to the **Storage** instance for data operations. This method uses a callback to return the execution result. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

callback

AsyncCallback<Storage>

Yes

Callback used to return the result.

- Example: ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' (async () => { var context = featureAbility.getContext() var path = await context.getFilesDir() dataStorage.getStorage(path + '/mystore', function (err, storage) { if (err) { console.info("Get the storage failed, path: " + path + '/mystore') return; } storage.putSync('startup', 'auto') storage.flushSync() }) })() ``` ## dataStorage.getStorage getStorage\(path: string\): Promise Reads a specified file and loads the data to the **Storage** instance for data operations. This method uses a promise to return the execution result. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Return values

Type

Description

Promise<Storage>

Promise used to return the result.

- Example: ``` import dataStorage from '@ohos.data.storage' import featureAbility from '@ohos.ability.featureAbility' (async () => { var context = featureAbility.getContext() var path = await context.getFilesDir() let promise = dataStorage.getStorage(path + '/mystore') promise.then((storage) => { storage.putSync('startup', 'auto') storage.flushSync() }).catch((err) => { console.info("Get the storage failed, path: " + path + '/mystore') }) }() ``` ## dataStorage.deleteStorageSync deleteStorageSync\(path: string\): void Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Example: ``` dataStorage.deleteStorageSync(path + '/mystore') ``` ## dataStorage.deleteStorage deleteStorage\(path: string, callback: AsyncCallback\) Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses an asynchronous callback to return the execution result. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

callback

AsyncCallback<void>

Yes

Callback used to return the result.

- Example: ``` dataStorage.deleteStorage(path + '/mystore', function (err) { if (err) { console.info("Deleted failed with err: " + err) return } console.info("Deleted successfully.") }) ``` ## dataStorage.deleteStorage deleteStorage\(path: string\): Promise Removes the singleton **Storage** instance of the specified file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified file is deleted, the **Storage** instance cannot be used to perform any data operation. Otherwise, data inconsistency will occur. This method uses a promise to return the execution result. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = dataStorage.deleteStorage(path + '/mystore') promise.then(() => { console.info("Deleted successfully.") }).catch((err) => { console.info("Deleted failed with err: " + err) }) ``` ## dataStorage.removeStorageFromCacheSync removeStorageFromCacheSync\(path: string\): void Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Example: ``` dataStorage.removeStorageFromCacheSync(path + '/mystore') ``` ## dataStorage.removeStorageFromCache removeStorageFromCache\(path: string, callback: AsyncCallback\): void Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

callback

AsyncCallback<Storage>

Yes

Callback used to return the result.

- Example: ``` dataStorage.removeStorageFromCache(path + '/mystore', function (err) { if (err) { console.info("Removed storage from cache failed with err: " + err) return } console.info("Removed storage from cache successfully.") }) ``` ## dataStorage.removeStorageFromCache removeStorageFromCache\(path: string\): Promise Removes the singleton **Storage** instance of the specified file from the memory. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

path

string

Yes

Storage path of the application internal data

- Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = dataStorage.removeStorageFromCache(path + '/mystore') promise.then(() => { console.info("Removed storage from cache successfully.") }).catch((err) => { console.info("Removed storage from cache failed with err: " + err) }) ``` ## Storage Provides APIs for obtaining and modifying storage data. ### getSync getSync\(key: string, defValue: ValueType\): ValueType Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

defValue

ValueType

Yes

Default value to be returned if the value of the specified key does not exist. The value can be a number, string, or Boolean value.

- Return values

Type

Description

ValueType

Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned.

- Example: ``` let value = storage.getSync('startup', 'default') console.info("The value of startup is " + value) ``` ### get get\(key: string, defValue: ValueType, callback: AsyncCallback\): void Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

defValue

ValueType

Yes

Default value to be returned. The value can be a number, string, or Boolean value.

callback

AsyncCallback<ValueType>

Yes

Callback used to return the result.

- Example: ``` storage.get('startup', 'default', function(err, value) { if (err) { console.info("Get the value of startup failed with err: " + err) return } console.info("The value of startup is " + value) }) ``` ### get get\(key: string, defValue: ValueType\): Promise Obtains the value corresponding to a key. If the value is null or not in the default value format, the default value is returned. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

defValue

ValueType

Yes

Default value to be returned. The value can be a number, string, or Boolean value.

- Return values

Type

Description

Promise<ValueType>

Promise used to return the result.

- Example: ``` let promise = storage.get('startup', 'default') promise.then((value) => { console.info("The value of startup is " + value) }).catch((err) => { console.info("Get the value of startup failed with err: " + err) }) ``` ### putSync putSync\(key: string, value: ValueType\): void Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data to be modified. It cannot be empty.

value

ValueType

Yes

New value. The value can be a number, string, or Boolean value.

- Example: ``` storage.putSync('startup', 'auto') ``` ### put put\(key: string, value: ValueType, callback: AsyncCallback\): void Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data to be modified. It cannot be empty.

value

ValueType

Yes

New value. The value can be a number, string, or Boolean value.

callback

AsyncCallback<void>

Yes

Callback used to return the result.

- Example: ``` storage.put('startup', 'auto', function (err) { if (err) { console.info("Put the value of startup failed with err: " + err) return } console.info("Put the value of startup successfully.") }) ``` ### put put\(key: string, value: ValueType\): Promise Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush\(\)** or **flushSync\(\)**. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data to be modified. It cannot be empty.

value

ValueType

Yes

New value. The value can be a number, string, or Boolean value.

- Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = storage.put('startup', 'auto') promise.then(() => { console.info("Put the value of startup successfully.") }).catch((err) => { console.info("Put the value of startup failed with err: " + err) }) ``` ### hasSync hasSync\(key: string\): boolean Checks whether the storage object contains data with the specified key. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

- Return values

Type

Description

boolean

Returns true if the storage object contains data with the specified key; returns false otherwise.

- Example: ``` let isExist = storage.hasSync('startup') if (isExist) { console.info("The key of startup is contained.") } ``` ### has has\(key: string, callback: AsyncCallback\): boolean Checks whether the storage object contains data with the specified key. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

callback

AsyncCallback<boolean>

Yes

Callback used to return the result.

- Return values

Type

Description

boolean

Returns true if the storage object contains data with the specified key; returns false otherwise.

- Example: ``` storage.has('startup', function (err, isExist) { if (err) { console.info("Check the key of startup failed with err: " + err) return } if (isExist) { console.info("The key of startup is contained.") } }) ``` ### has has\(key: string\): Promise Checks whether the storage object contains data with the specified key. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

- Return values

Type

Description

Promise<boolean>

Promise used to return the result.

- Example: ``` let promise = storage.has('startup') promise.then((isExist) => { if (isExist) { console.info("The key of startup is contained.") } }).catch((err) => { console.info("Check the key of startup failed with err: " + err) }) ``` ### deleteSync deleteSync\(key: string\): void Deletes the data with the specified key from this storage object. This method uses a synchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

- Example: ``` storage.deleteSync('startup') ``` ### delete delete\(key: string, callback: AsyncCallback\): void Deletes the data with the specified key from this storage object. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data. It cannot be empty.

callback

AsyncCallback<void>

Yes

Callback used to return the result.

- Example: ``` storage.delete('startup', function (err) { if (err) { console.info("Delete startup key failed with err: " + err) return } console.info("Deleted startup key successfully.") }) ``` ### delete delete\(key: string\): Promise Deletes the data with the specified key from this storage object. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

key

string

Yes

Key of the data.

- Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = storage.delete('startup') promise.then(() => { console.info("Deleted startup key successfully.") }).catch((err) => { console.info("Delete startup key failed with err: " + err) }) ``` ### flushSync flushSync\(\): void Saves the modification of the current object to the current **Storage** instance and synchronizes the modification to the file. This method uses a synchronous mode. - Example: ``` storage.flushSync() ``` ### flush flush\(callback: AsyncCallback\): void Saves the modification of the current object to the current **Storage** instance and stores the modification to the file in an asynchronous mode. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

callback

AsyncCallback<void>

Yes

Callback used to return the result.

- Example: ``` storage.flush(function (err) { if (err) { console.info("Flush to file failed with err: " + err) return } console.info("Flushed to file successfully.") }) ``` ### flush flush\(\): Promise Saves the modification of the current object to the current **Storage** instance and stores the modification to the file in an asynchronous mode. This method uses an asynchronous mode. - Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = storage.flush() promise.then(() => { console.info("Flushed to file successfully.") }).catch((err) => { console.info("Flush to file failed with err: " + err) }) ``` ### clearSync clearSync\(\): void Clears all data in a storage object. This method uses a synchronous mode. - Example: ``` storage.clearSync() ``` ### clear clear\(callback: AsyncCallback\): void Clears all data in a storage object. This method uses an asynchronous mode. - Parameters

Name

Type

Mandatory

Description

callback

AsyncCallback<void>

Yes

Callback used to return the result.

- Example: ``` storage.clear(function (err) { if (err) { console.info("Clear to file failed with err: " + err) return } console.info("Cleared to file successfully.") }) ``` ### clear clear\(\): Promise Clears all data in a storage object. This method uses an asynchronous mode. - Return values

Type

Description

Promise<void>

Promise used to return the result.

- Example: ``` let promise = storage.clear() promise.then(() => { console.info("Cleared to file successfully.") }).catch((err) => { console.info("Clear to file failed with err: " + err) }) ``` ### on \('change'\) on\(type: 'change', callback: Callback\): void Subscribes to data changes. The **StorageObserver** needs to be implemented. When the value of the key subscribed to is changed, a callback will be invoked after **flush\(\)** or **flushSync\(\)** is executed. - Parameters

Name

Type

Description

type

string

Event type. The value change indicates a data change event.

callback

Callback<StorageObserver>

Callback object.

- Example: ``` var observer = function (key) { console.info("The key of " + key + " changed.") } storage.on('change', observer) storage.putSync('startup', 'auto') storage.flushSync() // observer will be called. ``` ### off \('change'\) off\(type: 'change', callback: Callback\): void Unsubscribes from data changes. - Parameters

Name

Type

Description

type

string

Event type. The value change indicates a data change event.

callback

Callback<StorageObserver>

Callback object to be canceled.

- Example: ``` var observer = function (key) { console.info("The key of " + key + " changed.") } storage.off('change', observer) ``` ## StorageObserver

Name

Type

Mandatory

Description

key

string

No

Data changed