js-apis-data-storage.md 24.1 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_wang 已提交
9 10


A
annie_wangli 已提交
11 12 13


## Modules to Import
Z
zengyawen 已提交
14

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

A
Annie_wang 已提交
19
## Constants
A
annie_wangli 已提交
20 21 22 23 24

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

| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
A
Annie_wang 已提交
25 26
| 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 已提交
27 28


A
Annie_wang 已提交
29
## data_storage.getStorageSync
A
annie_wangli 已提交
30 31 32

getStorageSync(path: string): Storage

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

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

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

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

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


A
Annie_wang 已提交
59
## data_storage.getStorage
A
annie_wangli 已提交
60 61 62

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

A
Annie_wang 已提交
63
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 已提交
64 65 66

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

A
Annie_wang 已提交
67
**Parameters**
A
Annie_wang 已提交
68 69 70 71
  | 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_wangli 已提交
72

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


A
Annie_wang 已提交
89
## data_storage.getStorage
A
annie_wangli 已提交
90 91 92

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

A
Annie_wang 已提交
93
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 已提交
94 95 96

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

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

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

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


A
Annie_wang 已提交
123
## data_storage.deleteStorageSync
A
annie_wangli 已提交
124 125 126

deleteStorageSync(path: string): void

A
Annie_wang 已提交
127
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 已提交
128 129 130

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

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

A
Annie_wang 已提交
136 137
**Example**
  ```js
A
Annie_wang 已提交
138
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
139
  data_storage.deleteStorageSync(path + '/mystore')
A
annie_wangli 已提交
140 141 142
  ```


A
Annie_wang 已提交
143
## data_storage.deleteStorage
A
annie_wangli 已提交
144 145 146

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

A
Annie_wang 已提交
147
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 已提交
148 149 150

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

A
Annie_wang 已提交
151
**Parameters**
A
Annie_wang 已提交
152 153 154 155
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
A
annie_wangli 已提交
156

A
Annie_wang 已提交
157 158
**Example**
  ```js
A
Annie_wang 已提交
159
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
160
  data_storage.deleteStorage(path + '/mystore', function (err) {
A
annie_wangli 已提交
161 162 163 164 165 166 167 168
      if (err) {
          console.info("Deleted failed with err: " + err)
          return
      }
      console.info("Deleted successfully.")
  })
  ```

Z
zengyawen 已提交
169

A
Annie_wang 已提交
170
## data_storage.deleteStorage
Z
zengyawen 已提交
171

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

A
Annie_wang 已提交
174
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 已提交
175 176 177

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

A
Annie_wang 已提交
178
**Parameters**
A
Annie_wang 已提交
179 180 181
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
A
annie_wangli 已提交
182

A
Annie_wang 已提交
183
**Return value**
A
Annie_wang 已提交
184 185 186
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
187

A
Annie_wang 已提交
188 189
**Example**
  ```js
A
Annie_wang 已提交
190
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
191
  let promisedelSt = data_storage.deleteStorage(path + '/mystore')
A
annie_wangli 已提交
192 193 194 195 196 197 198 199
  promisedelSt.then(() => {
      console.info("Deleted successfully.")
  }).catch((err) => {
      console.info("Deleted failed with err: " + err)
  })
  ```


A
Annie_wang 已提交
200
## data_storage.removeStorageFromCacheSync
A
annie_wangli 已提交
201 202 203 204

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 已提交
205

A
annie_wangli 已提交
206 207
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
208
**Parameters**
A
Annie_wang 已提交
209 210 211
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
A
annie_wangli 已提交
212

A
Annie_wang 已提交
213 214
**Example**
  ```js
A
Annie_wang 已提交
215
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
216
  data_storage.removeStorageFromCacheSync(path + '/mystore')
A
annie_wangli 已提交
217 218 219
  ```


A
Annie_wang 已提交
220
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
221 222 223

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

A
Annie_wang 已提交
224
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 已提交
225 226 227

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

A
Annie_wang 已提交
228
**Parameters**
A
Annie_wang 已提交
229 230 231 232
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
A
annie_wangli 已提交
233

A
Annie_wang 已提交
234 235
**Example**
  ```js
A
Annie_wang 已提交
236
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
237
  data_storage.removeStorageFromCache(path + '/mystore', function (err) {
A
annie_wangli 已提交
238 239 240 241 242 243 244 245 246
      if (err) {
          console.info("Removed storage from cache failed with err: " + err)
          return
      }
      console.info("Removed storage from cache successfully.")
  })
  ```


A
Annie_wang 已提交
247
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
248 249 250

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

A
Annie_wang 已提交
251
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 已提交
252 253 254

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

A
Annie_wang 已提交
255
**Parameters**
A
Annie_wang 已提交
256 257 258
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | path | string | Yes| Path of the target file.|
A
annie_wangli 已提交
259

A
Annie_wang 已提交
260
**Return value**
A
Annie_wang 已提交
261 262 263
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
264

A
Annie_wang 已提交
265 266
**Example**
  ```js
A
Annie_wang 已提交
267
  let path = '/data/storage/el2/database'
A
Annie_wang 已提交
268
  let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
A
annie_wangli 已提交
269 270 271 272 273 274 275 276 277
  promiserevSt.then(() => {
      console.info("Removed storage from cache successfully.")
  }).catch((err) => {
      console.info("Removed storage from cache failed with err: " + err)
  })
  ```


## Storage
Z
zengyawen 已提交
278 279 280 281

Provides APIs for obtaining and modifying storage data.


A
annie_wangli 已提交
282 283 284
### getSync

getSync(key: string, defValue: ValueType): ValueType
Z
zengyawen 已提交
285 286 287

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 已提交
288 289
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
290
**Parameters**
A
Annie_wang 已提交
291 292 293 294
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | 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 已提交
295

A
Annie_wang 已提交
296
**Return value**
A
Annie_wang 已提交
297 298 299
  | 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_wangli 已提交
300

A
Annie_wang 已提交
301 302
**Example**
  ```js
A
annie_wangli 已提交
303 304 305 306 307 308 309 310
  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 已提交
311

A
Annie_wang 已提交
312
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 已提交
313 314 315

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

A
Annie_wang 已提交
316
**Parameters**
A
Annie_wang 已提交
317 318 319 320 321
  | 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.|
  | callback | AsyncCallback&lt;ValueType&gt; | Yes| Callback used to return the execution result.|
A
annie_wangli 已提交
322

A
Annie_wang 已提交
323 324
**Example**
  ```js
A
annie_wangli 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337
  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 已提交
338

A
Annie_wang 已提交
339
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 已提交
340 341 342

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

A
Annie_wang 已提交
343
**Parameters**
A
annie_wangli 已提交
344

A
Annie_wang 已提交
345 346 347 348 349 350
| 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_wang 已提交
351 352 353
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;ValueType&gt; | Promise used to return the result.|
A
annie_wangli 已提交
354

A
Annie_wang 已提交
355 356
**Example**
  ```js
A
annie_wangli 已提交
357 358 359 360 361 362 363
  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 已提交
364 365


A
annie_wangli 已提交
366 367 368 369 370
### 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 已提交
371

A
annie_wangli 已提交
372 373
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
374
**Parameters**
A
Annie_wang 已提交
375 376 377 378
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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 已提交
379

A
Annie_wang 已提交
380 381
**Example**
  ```js
A
annie_wangli 已提交
382 383 384 385 386 387 388 389
  storage.putSync('startup', 'auto')
  ```


### put

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

A
Annie_wang 已提交
390
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 已提交
391 392 393

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

A
Annie_wang 已提交
394
**Parameters**
A
Annie_wang 已提交
395 396 397 398 399
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
A
annie_wangli 已提交
400

A
Annie_wang 已提交
401 402
**Example**
  ```js
A
annie_wangli 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416
  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 已提交
417
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 已提交
418 419 420

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

A
Annie_wang 已提交
421
**Parameters**
A
Annie_wang 已提交
422 423 424 425
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | 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 已提交
426

A
Annie_wang 已提交
427
**Return value**
A
Annie_wang 已提交
428 429 430
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
431

A
Annie_wang 已提交
432 433
**Example**
  ```js
A
annie_wangli 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447
  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 已提交
448

A
annie_wangli 已提交
449 450
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
451
**Parameters**
A
Annie_wang 已提交
452 453 454
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
annie_wangli 已提交
455

A
Annie_wang 已提交
456
**Return value**
A
Annie_wang 已提交
457 458 459
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
A
annie_wangli 已提交
460

A
Annie_wang 已提交
461 462
**Example**
  ```js
A
annie_wangli 已提交
463 464 465 466 467 468 469 470 471 472 473
  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 已提交
474
Checks whether the storage object contains data with a given key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
475 476 477

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

A
Annie_wang 已提交
478
**Parameters**
A
Annie_wang 已提交
479 480 481 482
  | 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_wangli 已提交
483

A
Annie_wang 已提交
484
**Return value**
A
Annie_wang 已提交
485 486 487
  | Type| Description|
  | -------- | -------- |
  | boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise.|
A
annie_wangli 已提交
488

A
Annie_wang 已提交
489 490
**Example**
  ```js
A
annie_wangli 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  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 已提交
507
Checks whether the storage object contains data with a given key. This API uses a promise to return the result.
A
annie_wangli 已提交
508 509 510

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

A
Annie_wang 已提交
511
**Parameters**
A
Annie_wang 已提交
512 513 514
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
annie_wangli 已提交
515

A
Annie_wang 已提交
516
**Return value**
A
Annie_wang 已提交
517 518 519
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;boolean&gt; | Promise used to return the result.|
A
annie_wangli 已提交
520

A
Annie_wang 已提交
521 522
**Example**
  ```js
A
annie_wangli 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
  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 已提交
539

A
annie_wangli 已提交
540 541
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
542
**Parameters**
A
Annie_wang 已提交
543 544 545
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
A
annie_wangli 已提交
546

A
Annie_wang 已提交
547 548
**Example**
  ```js
A
annie_wangli 已提交
549 550 551 552
  storage.deleteSync('startup')
  ```


A
Annie_wang 已提交
553
### delete
A
annie_wangli 已提交
554 555 556

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

A
Annie_wang 已提交
557
Deletes data with the specified key from this storage object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
558 559 560

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

A
Annie_wang 已提交
561
**Parameters**
A
Annie_wang 已提交
562 563 564 565
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data. It cannot be empty.|
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
A
annie_wangli 已提交
566

A
Annie_wang 已提交
567 568
**Example**
  ```js
A
annie_wangli 已提交
569 570 571 572 573 574 575 576 577
  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 已提交
578

A
annie_wangli 已提交
579
### delete
Z
zengyawen 已提交
580

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

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

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

A
Annie_wang 已提交
587
**Parameters**
A
Annie_wang 已提交
588 589 590
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | key | string | Yes| Key of the data.|
Z
zengyawen 已提交
591

A
Annie_wang 已提交
592
**Return value**
A
Annie_wang 已提交
593 594 595
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
Z
zengyawen 已提交
596

A
Annie_wang 已提交
597 598
**Example**
  ```js
A
annie_wangli 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
  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 已提交
615

A
Annie_wang 已提交
616 617
**Example**
  ```js
A
annie_wangli 已提交
618 619 620 621 622
  storage.flushSync()
  ```


### flush
Z
zengyawen 已提交
623

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

A
Annie_wang 已提交
626
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 已提交
627

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

A
Annie_wang 已提交
630
**Parameters**
A
Annie_wang 已提交
631 632 633
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
Z
zengyawen 已提交
634

A
Annie_wang 已提交
635 636
**Example**
  ```js
A
annie_wangli 已提交
637 638 639 640 641 642 643 644
  storage.flush(function (err) {
      if (err) {
          console.info("Flush to file failed with err: " + err)
          return
      }
      console.info("Flushed to file successfully.")
  })
  ```
Z
zengyawen 已提交
645 646


A
annie_wangli 已提交
647
### flush
Z
zengyawen 已提交
648

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

A
Annie_wang 已提交
651
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 已提交
652

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

A
Annie_wang 已提交
655
**Return value**
A
Annie_wang 已提交
656 657 658
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
659

A
Annie_wang 已提交
660 661
**Example**
  ```js
A
annie_wangli 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675
  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 已提交
676

A
annie_wangli 已提交
677 678
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
679 680
**Example**
  ```js
A
annie_wangli 已提交
681 682 683 684 685 686 687 688
  storage.clearSync()
  ```


### clear

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

A
Annie_wang 已提交
689
Clears this **Storage** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
690 691 692

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

A
Annie_wang 已提交
693
**Parameters**
A
Annie_wang 已提交
694 695 696
  | Name| Type| Mandatory| Description|
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback&lt;void&gt; | Yes| Callback that returns no value.|
Z
zengyawen 已提交
697

A
Annie_wang 已提交
698 699
**Example**
  ```js
A
annie_wangli 已提交
700 701 702 703 704 705 706 707 708 709 710 711 712 713
  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 已提交
714
Clears this **Storage** object. This API uses a promise to return the result.
A
annie_wangli 已提交
715 716 717

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

A
Annie_wang 已提交
718
**Return value**
A
Annie_wang 已提交
719 720 721
  | Type| Description|
  | -------- | -------- |
  | Promise&lt;void&gt; | Promise that returns no value.|
A
annie_wangli 已提交
722

A
Annie_wang 已提交
723 724
**Example**
  ```js
A
annie_wangli 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738
  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 已提交
739

A
annie_wangli 已提交
740 741
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
742
**Parameters**
A
Annie_wang 已提交
743 744 745 746
  | 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_wangli 已提交
747

A
Annie_wang 已提交
748 749
**Example**
  ```js
A
annie_wangli 已提交
750 751 752 753 754 755 756 757 758 759 760 761
  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 已提交
762 763 764

Unsubscribes from data changes.

A
annie_wangli 已提交
765 766
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
767
**Parameters**
A
Annie_wang 已提交
768 769 770 771
  | 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_wangli 已提交
772

A
Annie_wang 已提交
773 774
**Example**
  ```js
A
annie_wangli 已提交
775 776 777 778 779 780 781 782 783 784 785 786 787 788
  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 已提交
789 790 791 792 793 794 795

## ValueType

Enumerates the value types.

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

A
Annie_wang 已提交
796
| Type   | Description                |
A
Annie_wang 已提交
797 798 799 800
| ------- | -------------------- |
| number  | The value is a number.  |
| string  | The value is a string.  |
| boolean | The value is of Boolean type.|