js-apis-data-storage.md 24.0 KB
Newer Older
A
annie_wangli 已提交
1
# Lightweight Storage
Z
zengyawen 已提交
2

A
annie_wangli 已提交
3
Lightweight storage provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
Z
zengyawen 已提交
4 5


A
Annie_wang 已提交
6
> **NOTE**<br/>
Z
zengyawen 已提交
7
>
A
Annie_wang 已提交
8
> 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.
A
annie_wangli 已提交
9 10 11


## Modules to Import
Z
zengyawen 已提交
12

A
Annie_wang 已提交
13
```js
A
Annie_wang 已提交
14
import data_storage from '@ohos.data.storage';
Z
zengyawen 已提交
15 16
```

A
Annie_wang 已提交
17
## Constants
A
annie_wangli 已提交
18 19 20 21 22

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
23 24
| MAX_KEY_LENGTH | string | Yes| No| Maximum length of a key. It must be less than 80 bytes.|
| MAX_VALUE_LENGTH | string | Yes| No| Maximum length of a value. It must be less than 8192 bytes.|
A
annie_wangli 已提交
25 26


A
Annie_wang 已提交
27
## data_storage.getStorageSync
A
annie_wangli 已提交
28 29 30

getStorageSync(path: string): Storage

A
Annie_wang 已提交
31
Reads the specified file and loads its data to the **Storage** instance for data operations.
A
annie_wangli 已提交
32 33 34

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
35
**Parameters**
A
annie_wangli 已提交
36 37 38 39
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
40
**Return value**
A
annie_wangli 已提交
41 42 43 44
  | Type| Description|
  | -------- | -------- |
  | [Storage](#storage) | **Storage** instance used for data storage operations.|

A
Annie_wang 已提交
45 46
**Example**
  ```js
A
Annie_wang 已提交
47 48 49 50 51 52
  import data_storage from '@ohos.data.storage'
  
  var path = '/data/storage/el2/database/test_storage'
  let storage = data_storage.getStorageSync(path + '/mystore')
  storage.putSync('startup', 'auto')
  storage.flushSync()
G
ge-yafang 已提交
53
  
A
annie_wangli 已提交
54 55 56
  ```


A
Annie_wang 已提交
57
## data_storage.getStorage
A
annie_wangli 已提交
58 59 60

getStorage(path: string, callback: AsyncCallback&lt;Storage&gt;): void

A
Annie_wang 已提交
61
Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
62 63 64

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
65
**Parameters**
A
annie_wangli 已提交
66 67 68 69 70
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;[Storage](#storage)&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
71 72
**Example**
  ```js
A
Annie_wang 已提交
73
  import data_storage from '@ohos.data.storage'
G
ge-yafang 已提交
74
  
A
Annie_wang 已提交
75 76
  var path = '/data/storage/el2/database/test_storage'
  data_storage.getStorage(path + '/mystore', function (err, storage) {
A
annie_wangli 已提交
77
      if (err) {
A
Annie_wang 已提交
78
          console.info("Get the storage failed, path: " + path + '/mystore')
A
annie_wangli 已提交
79 80
          return;
      }
A
Annie_wang 已提交
81 82 83
      storage.putSync('startup', 'auto')
      storage.flushSync()
  })
A
annie_wangli 已提交
84
  ```
Z
zengyawen 已提交
85 86


A
Annie_wang 已提交
87
## data_storage.getStorage
A
annie_wangli 已提交
88 89 90

getStorage(path: string): Promise&lt;Storage&gt;

A
Annie_wang 已提交
91
Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses a promise to return the result.
A
annie_wangli 已提交
92 93 94

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
95
**Parameters**
A
annie_wangli 已提交
96 97 98 99
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
100
**Return value**
A
annie_wangli 已提交
101 102 103 104
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;[Storage](#storage)&gt; | Promise used to return the result.|

A
Annie_wang 已提交
105 106
**Example**
  ```js
A
Annie_wang 已提交
107
  import data_storage from '@ohos.data.storage'
G
ge-yafang 已提交
108
  
A
Annie_wang 已提交
109 110 111 112 113 114 115 116 117
  var path = '/data/storage/el2/database/test_storage'
  
  let getPromise = data_storage.getStorage(path + '/mystore')
  getPromise.then((storage) => {
      storage.putSync('startup', 'auto')
      storage.flushSync()
  }).catch((err) => {
      console.info("Get the storage failed, path: " + path + '/mystore')
  })
A
annie_wangli 已提交
118
  ```
Z
zengyawen 已提交
119 120


A
Annie_wang 已提交
121
## data_storage.deleteStorageSync
A
annie_wangli 已提交
122 123 124

deleteStorageSync(path: string): void

A
Annie_wang 已提交
125
Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur.
A
annie_wangli 已提交
126 127 128

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
129
**Parameters**
A
annie_wangli 已提交
130 131 132 133
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
134 135
**Example**
  ```js
A
Annie_wang 已提交
136
  data_storage.deleteStorageSync(path + '/mystore')
A
annie_wangli 已提交
137 138 139
  ```


A
Annie_wang 已提交
140
## data_storage.deleteStorage
A
annie_wangli 已提交
141 142 143

deleteStorage(path: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
144
Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
145 146 147

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
148
**Parameters**
A
annie_wangli 已提交
149 150 151 152 153
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
154 155
**Example**
  ```js
A
Annie_wang 已提交
156
  data_storage.deleteStorage(path + '/mystore', function (err) {
A
annie_wangli 已提交
157 158 159 160 161 162 163 164
      if (err) {
          console.info("Deleted failed with err: " + err)
          return
      }
      console.info("Deleted successfully.")
  })
  ```

Z
zengyawen 已提交
165

A
Annie_wang 已提交
166
## data_storage.deleteStorage
Z
zengyawen 已提交
167

A
annie_wangli 已提交
168 169
deleteStorage(path: string): Promise&lt;void&gt;

A
Annie_wang 已提交
170
Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result.
A
annie_wangli 已提交
171 172 173

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
174
**Parameters**
A
annie_wangli 已提交
175 176 177 178
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
179
**Return value**
A
annie_wangli 已提交
180 181 182 183
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
184 185
**Example**
  ```js
A
Annie_wang 已提交
186
  let promisedelSt = data_storage.deleteStorage(path + '/mystore')
A
annie_wangli 已提交
187 188 189 190 191 192 193 194
  promisedelSt.then(() => {
      console.info("Deleted successfully.")
  }).catch((err) => {
      console.info("Deleted failed with err: " + err)
  })
  ```


A
Annie_wang 已提交
195
## data_storage.removeStorageFromCacheSync
A
annie_wangli 已提交
196 197 198 199

removeStorageFromCacheSync(path: string): void

Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
Z
zengyawen 已提交
200

A
annie_wangli 已提交
201 202
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
203
**Parameters**
A
annie_wangli 已提交
204 205 206 207
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
208 209
**Example**
  ```js
A
Annie_wang 已提交
210
  data_storage.removeStorageFromCacheSync(path + '/mystore')
A
annie_wangli 已提交
211 212 213
  ```


A
Annie_wang 已提交
214
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
215 216 217

removeStorageFromCache(path: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
218
Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
219 220 221

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
222
**Parameters**
A
annie_wangli 已提交
223 224 225 226 227
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
228 229
**Example**
  ```js
A
Annie_wang 已提交
230
  data_storage.removeStorageFromCache(path + '/mystore', function (err) {
A
annie_wangli 已提交
231 232 233 234 235 236 237 238 239
      if (err) {
          console.info("Removed storage from cache failed with err: " + err)
          return
      }
      console.info("Removed storage from cache successfully.")
  })
  ```


A
Annie_wang 已提交
240
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
241 242 243

removeStorageFromCache(path: string): Promise&lt;void&gt;

A
Annie_wang 已提交
244
Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result.
A
annie_wangli 已提交
245 246 247

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
248
**Parameters**
A
annie_wangli 已提交
249 250 251 252
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|

A
Annie_wang 已提交
253
**Return value**
A
annie_wangli 已提交
254 255 256 257
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
258 259
**Example**
  ```js
A
Annie_wang 已提交
260
  let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
A
annie_wangli 已提交
261 262 263 264 265 266 267 268 269
  promiserevSt.then(() => {
      console.info("Removed storage from cache successfully.")
  }).catch((err) => {
      console.info("Removed storage from cache failed with err: " + err)
  })
  ```


## Storage
Z
zengyawen 已提交
270 271 272 273

Provides APIs for obtaining and modifying storage data.


A
annie_wangli 已提交
274 275 276
### getSync

getSync(key: string, defValue: ValueType): ValueType
Z
zengyawen 已提交
277 278 279

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

A
annie_wangli 已提交
280 281
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
282
**Parameters**
A
annie_wangli 已提交
283 284 285
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
Annie_wang 已提交
286
  | defValue | [ValueType](#valuetype) | Yes| Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
287

A
Annie_wang 已提交
288
**Return value**
A
annie_wangli 已提交
289 290 291 292
  | 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.|

A
Annie_wang 已提交
293 294
**Example**
  ```js
A
annie_wangli 已提交
295 296 297 298 299 300 301 302
  let value = storage.getSync('startup', 'default')
  console.info("The value of startup is " + value)
  ```


### get

get(key: string, defValue: ValueType, callback: AsyncCallback&lt;ValueType&gt;): void
Z
zengyawen 已提交
303

A
Annie_wang 已提交
304
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 API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
305 306 307

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
308
**Parameters**
A
annie_wangli 已提交
309 310 311
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
Annie_wang 已提交
312
  | defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
313 314
  | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
315 316
**Example**
  ```js
A
annie_wangli 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
  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&lt;ValueType&gt;
Z
zengyawen 已提交
330

A
Annie_wang 已提交
331
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 API uses a promise to return the result.
A
annie_wangli 已提交
332 333 334

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
335
**Parameters**
A
annie_wangli 已提交
336

A
Annie_wang 已提交
337 338 339 340 341 342
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | Yes| Key of the data. It cannot be empty.|
| defValue | [ValueType](#valuetype) | Yes| Default value to be returned. It can be a number, string, or Boolean value.|

**Return value**
A
annie_wangli 已提交
343 344 345 346
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;ValueType&gt; | Promise used to return the result.|

A
Annie_wang 已提交
347 348
**Example**
  ```js
A
annie_wangli 已提交
349 350 351 352 353 354 355
  let promiseget = storage.get('startup', 'default')
  promiseget.then((value) => {
      console.info("The value of startup is " + value)
  }).catch((err) => {
      console.info("Get the value of startup failed with err: " + err)
  })
  ```
Z
zengyawen 已提交
356 357


A
annie_wangli 已提交
358 359 360 361 362
### 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()**.
Z
zengyawen 已提交
363

A
annie_wangli 已提交
364 365
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
366
**Parameters**
A
annie_wangli 已提交
367 368
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
369 370
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
371

A
Annie_wang 已提交
372 373
**Example**
  ```js
A
annie_wangli 已提交
374 375 376 377 378 379 380 381
  storage.putSync('startup', 'auto')
  ```


### put

put(key: string, value: ValueType, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
382
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 API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
383 384 385

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
386
**Parameters**
A
annie_wangli 已提交
387 388
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
389 390
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
391 392
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
393 394
**Example**
  ```js
A
annie_wangli 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408
  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&lt;void&gt;

A
Annie_wang 已提交
409
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 API uses a promise to return the result.
A
annie_wangli 已提交
410 411 412

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
413
**Parameters**
A
annie_wangli 已提交
414 415
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
416 417
  | key | string | Yes| Key of the data. It cannot be empty.|
  | value | [ValueType](#valuetype) | Yes| New value to store. It can be a number, string, or Boolean value.|
A
annie_wangli 已提交
418

A
Annie_wang 已提交
419
**Return value**
A
annie_wangli 已提交
420 421 422 423
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
424 425
**Example**
  ```js
A
annie_wangli 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439
  let promiseput = storage.put('startup', 'auto')
  promiseput.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 a given key.
Z
zengyawen 已提交
440

A
annie_wangli 已提交
441 442
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
443
**Parameters**
A
annie_wangli 已提交
444 445 446 447
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
448
**Return value**
A
annie_wangli 已提交
449 450 451 452
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|

A
Annie_wang 已提交
453 454
**Example**
  ```js
A
annie_wangli 已提交
455 456 457 458 459 460 461 462 463 464 465
  let isExist = storage.hasSync('startup')
  if (isExist) {
      console.info("The key of startup is contained.")
  }
  ```


### has

has(key: string, callback: AsyncCallback&lt;boolean&gt;): boolean

A
Annie_wang 已提交
466
Checks whether the storage object contains data with a given key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
467 468 469

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
470
**Parameters**
A
annie_wangli 已提交
471 472 473 474 475
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | callback | AsyncCallback&lt;boolean&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
476
**Return value**
A
annie_wangli 已提交
477 478 479 480
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|

A
Annie_wang 已提交
481 482
**Example**
  ```js
A
annie_wangli 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
  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&lt;boolean&gt;

A
Annie_wang 已提交
499
Checks whether the storage object contains data with a given key. This API uses a promise to return the result.
A
annie_wangli 已提交
500 501 502

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
503
**Parameters**
A
annie_wangli 已提交
504 505 506 507
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
508
**Return value**
A
annie_wangli 已提交
509 510 511 512
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise used to return the result.|

A
Annie_wang 已提交
513 514
**Example**
  ```js
A
annie_wangli 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
  let promisehas = storage.has('startup')
  promisehas.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 data with the specified key from this storage object.
Z
zengyawen 已提交
531

A
annie_wangli 已提交
532 533
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
534
**Parameters**
A
annie_wangli 已提交
535 536 537 538
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|

A
Annie_wang 已提交
539 540
**Example**
  ```js
A
annie_wangli 已提交
541 542 543 544
  storage.deleteSync('startup')
  ```


A
Annie_wang 已提交
545
### delete
A
annie_wangli 已提交
546 547 548

delete(key: string, callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
549
Deletes data with the specified key from this storage object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
550 551 552

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
553
**Parameters**
A
annie_wangli 已提交
554 555 556 557 558
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|

A
Annie_wang 已提交
559 560
**Example**
  ```js
A
annie_wangli 已提交
561 562 563 564 565 566 567 568 569
  storage.delete('startup', function (err) {
      if (err) {
          console.info("Delete startup key failed with err: " + err)
          return
      }
      console.info("Deleted startup key successfully.")
  })
  ```

Z
zengyawen 已提交
570

A
annie_wangli 已提交
571
### delete
Z
zengyawen 已提交
572

A
annie_wangli 已提交
573
delete(key: string): Promise&lt;void&gt;
Z
zengyawen 已提交
574

A
Annie_wang 已提交
575
Deletes data with the specified key from this storage object. This API uses a promise to return the result.
Z
zengyawen 已提交
576

A
annie_wangli 已提交
577
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
578

A
Annie_wang 已提交
579
**Parameters**
A
annie_wangli 已提交
580 581 582
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data.|
Z
zengyawen 已提交
583

A
Annie_wang 已提交
584
**Return value**
A
annie_wangli 已提交
585 586 587
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|
Z
zengyawen 已提交
588

A
Annie_wang 已提交
589 590
**Example**
  ```js
A
annie_wangli 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
  let promisedel = storage.delete('startup')
  promisedel.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 this object to the **Storage** instance and synchronizes the modification to the file.

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
607

A
Annie_wang 已提交
608 609
**Example**
  ```js
A
annie_wangli 已提交
610 611 612 613 614
  storage.flushSync()
  ```


### flush
Z
zengyawen 已提交
615

A
annie_wangli 已提交
616
flush(callback: AsyncCallback&lt;void&gt;): void
Z
zengyawen 已提交
617

A
Annie_wang 已提交
618
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses an asynchronous callback to return the result.
Z
zengyawen 已提交
619

A
annie_wangli 已提交
620
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
621

A
Annie_wang 已提交
622
**Parameters**
A
annie_wangli 已提交
623 624 625
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
Z
zengyawen 已提交
626

A
Annie_wang 已提交
627 628
**Example**
  ```js
A
annie_wangli 已提交
629 630 631 632 633 634 635 636
  storage.flush(function (err) {
      if (err) {
          console.info("Flush to file failed with err: " + err)
          return
      }
      console.info("Flushed to file successfully.")
  })
  ```
Z
zengyawen 已提交
637 638


A
annie_wangli 已提交
639
### flush
Z
zengyawen 已提交
640

A
annie_wangli 已提交
641
flush(): Promise&lt;void&gt;
Z
zengyawen 已提交
642

A
Annie_wang 已提交
643
Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses a promise to return the result.
Z
zengyawen 已提交
644

A
annie_wangli 已提交
645
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
Z
zengyawen 已提交
646

A
Annie_wang 已提交
647
**Return value**
A
annie_wangli 已提交
648 649 650 651
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
652 653
**Example**
  ```js
A
annie_wangli 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667
  let promiseflush = storage.flush()
  promiseflush.then(() => {
      console.info("Flushed to file successfully.")
  }).catch((err) => {
      console.info("Flush to file failed with err: " + err)
  })
  ```


### clearSync

clearSync(): void

Clears this **Storage** object.
Z
zengyawen 已提交
668

A
annie_wangli 已提交
669 670
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
671 672
**Example**
  ```js
A
annie_wangli 已提交
673 674 675 676 677 678 679 680
  storage.clearSync()
  ```


### clear

clear(callback: AsyncCallback&lt;void&gt;): void

A
Annie_wang 已提交
681
Clears this **Storage** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
682 683 684

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
685
**Parameters**
A
annie_wangli 已提交
686 687 688
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the execution result.|
Z
zengyawen 已提交
689

A
Annie_wang 已提交
690 691
**Example**
  ```js
A
annie_wangli 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705
  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&lt;void&gt;

A
Annie_wang 已提交
706
Clears this **Storage** object. This API uses a promise to return the result.
A
annie_wangli 已提交
707 708 709

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
710
**Return value**
A
annie_wangli 已提交
711 712 713 714
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise used to return the result.|

A
Annie_wang 已提交
715 716
**Example**
  ```js
A
annie_wangli 已提交
717 718 719 720 721 722 723 724 725 726 727 728 729 730
  let promiseclear = storage.clear()
  promiseclear.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&lt;StorageObserver&gt;): 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.
Z
zengyawen 已提交
731

A
annie_wangli 已提交
732 733
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
734
**Parameters**
A
annie_wangli 已提交
735 736 737 738 739
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|

A
Annie_wang 已提交
740 741
**Example**
  ```js
A
annie_wangli 已提交
742 743 744 745 746 747 748 749 750 751 752 753
  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&lt;StorageObserver&gt;): void
Z
zengyawen 已提交
754 755 756

Unsubscribes from data changes.

A
annie_wangli 已提交
757 758
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
759
**Parameters**
A
annie_wangli 已提交
760 761 762 763 764
  | Name| Type| Description|
  | -------- | -------- | -------- |
  | type | string | Event type. The value **change** indicates data change events.|
  | callback | Callback&lt;[StorageObserver](#storageobserver)&gt; | Callback used to return data changes.|

A
Annie_wang 已提交
765 766
**Example**
  ```js
A
annie_wangli 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780
  var observer = function (key) {
      console.info("The key of " + key + " changed.")
  }
  storage.off('change', observer)
  ```


## StorageObserver

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| key | string | No| Data changed.|
A
Annie_wang 已提交
781 782 783 784 785 786 787 788 789 790 791 792

## ValueType

Enumerates the value types.

**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

| Name   | Description                |
| ------- | -------------------- |
| number  | The value is a number.  |
| string  | The value is a string.  |
| boolean | The value is of Boolean type.|