js-apis-data-storage.md 30.2 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


6
> **NOTE**
A
annie_wangli 已提交
7 8 9 10
>
> - 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.
>
> - The APIs of this module are no longer maintained since API Version 9. You are advised to use [`@ohos.data.preferences`](js-apis-data-preferences.md).
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

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

23 24 25 26
| Name             | Type   | Readable | Writable | Description                                                 |
| ---------------- | ------ | -------- | -------- | ----------------------------------------------------------- |
| 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**
38 39 40 41

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
42

A
Annie_wang 已提交
43
**Return value**
44 45 46 47

| Type                | Description                                            |
| ------------------- | ------------------------------------------------------ |
| [Storage](#storage) | **Storage** instance used for data storage operations. |
A
annie_wangli 已提交
48

A
Annie_wang 已提交
49
**Example**
50 51 52 53 54 55 56 57

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
58
    console.info("======================>getFilesDirPromise====================>");
59 60 61 62 63 64
});  

let storage = data_storage.getStorageSync(path + '/mystore');
storage.putSync('startup', 'auto');
storage.flushSync();
```
A
annie_wangli 已提交
65 66


A
Annie_wang 已提交
67
## data_storage.getStorage
A
annie_wangli 已提交
68 69 70

getStorage(path: string, callback: AsyncCallback<Storage>): void

A
Annie_wang 已提交
71
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 已提交
72 73 74

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

A
Annie_wang 已提交
75
**Parameters**
76 77 78 79 80

| Name     | Type                                     | Mandatory | Description                                   |
| -------- | ---------------------------------------- | --------- | --------------------------------------------- |
| path     | string                                   | Yes       | Path of the target file.                      |
| callback | AsyncCallback<[Storage](#storage)> | Yes       | Callback used to return the execution result. |
A
annie_wangli 已提交
81

A
Annie_wang 已提交
82
**Example**
83 84 85 86 87 88 89 90

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
91
    console.info("======================>getFilesDirPromise====================>");
92 93 94 95 96 97 98 99 100 101 102
});  

data_storage.getStorage(path + '/mystore', function (err, storage) {
    if (err) {
        console.info("Failed to get the storage. path: " + path + '/mystore');
        return;
    }
    storage.putSync('startup', 'auto');
    storage.flushSync();
})
```
Z
zengyawen 已提交
103 104


A
Annie_wang 已提交
105
## data_storage.getStorage
A
annie_wangli 已提交
106 107 108

getStorage(path: string): Promise<Storage>

A
Annie_wang 已提交
109
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 已提交
110 111 112

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

A
Annie_wang 已提交
113
**Parameters**
114 115 116 117

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
118

A
Annie_wang 已提交
119
**Return value**
120 121 122 123

| Type                               | Description                        |
| ---------------------------------- | ---------------------------------- |
| Promise<[Storage](#storage)> | Promise used to return the result. |
A
annie_wangli 已提交
124

A
Annie_wang 已提交
125
**Example**
126 127 128 129 130 131 132 133

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
134
    console.info("======================>getFilesDirPromise====================>");
135 136 137 138 139 140 141 142 143 144
});  

let getPromise = data_storage.getStorage(path + '/mystore');
getPromise.then((storage) => {
    storage.putSync('startup', 'auto');
    storage.flushSync();
}).catch((err) => {
    console.info("Failed to get the storage. path: " + path + '/mystore');
})
```
Z
zengyawen 已提交
145 146


A
Annie_wang 已提交
147
## data_storage.deleteStorageSync
A
annie_wangli 已提交
148 149 150

deleteStorageSync(path: string): void

A
Annie_wang 已提交
151
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 已提交
152 153 154

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

A
Annie_wang 已提交
155
**Parameters**
156 157 158 159

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
160

A
Annie_wang 已提交
161
**Example**
162 163 164 165 166 167 168 169

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
170
    console.info("======================>getFilesDirPromise====================>");
171 172 173 174
});  

data_storage.deleteStorageSync(path + '/mystore');
```
A
annie_wangli 已提交
175 176


A
Annie_wang 已提交
177
## data_storage.deleteStorage
A
annie_wangli 已提交
178 179 180

deleteStorage(path: string, callback: AsyncCallback<void>): void

A
Annie_wang 已提交
181
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 已提交
182 183 184

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

A
Annie_wang 已提交
185
**Parameters**
186 187 188 189 190

| Name     | Type                      | Mandatory | Description                     |
| -------- | ------------------------- | --------- | ------------------------------- |
| path     | string                    | Yes       | Path of the target file.        |
| callback | AsyncCallback<void> | Yes       | Callback that returns no value. |
A
annie_wangli 已提交
191

A
Annie_wang 已提交
192
**Example**
193 194 195 196 197 198 199 200

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
201
    console.info("======================>getFilesDirPromise====================>");
202 203 204 205 206 207 208 209 210 211
});  

data_storage.deleteStorage(path + '/mystore', function (err) {
    if (err) {
        console.info("Failed to delete the storage with err: " + err);
        return;
    }
    console.info("Succeeded in deleting the storage.");
})
```
A
annie_wangli 已提交
212

Z
zengyawen 已提交
213

A
Annie_wang 已提交
214
## data_storage.deleteStorage
Z
zengyawen 已提交
215

A
annie_wangli 已提交
216 217
deleteStorage(path: string): Promise<void>

A
Annie_wang 已提交
218
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 已提交
219 220 221

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

A
Annie_wang 已提交
222
**Parameters**
223 224 225 226

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
227

A
Annie_wang 已提交
228
**Return value**
229 230 231
| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
A
annie_wangli 已提交
232

A
Annie_wang 已提交
233
**Example**
234 235 236 237 238 239 240 241

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
242
    console.info("======================>getFilesDirPromise====================>");
243 244 245 246 247 248 249 250 251
});  

let promisedelSt = data_storage.deleteStorage(path + '/mystore');
promisedelSt.then(() => {
    console.info("Succeeded in deleting the storage.");
}).catch((err) => {
    console.info("Failed to delete the storage with err: " + err);
})
```
A
annie_wangli 已提交
252 253


A
Annie_wang 已提交
254
## data_storage.removeStorageFromCacheSync
A
annie_wangli 已提交
255 256 257 258

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

A
annie_wangli 已提交
260 261
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
262
**Parameters**
263 264 265 266

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
267

A
Annie_wang 已提交
268
**Example**
269 270 271 272 273 274 275 276

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
277
    console.info("======================>getFilesDirPromise====================>");
278 279 280 281
});  

data_storage.removeStorageFromCacheSync(path + '/mystore');
```
A
annie_wangli 已提交
282 283


A
Annie_wang 已提交
284
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
285 286 287

removeStorageFromCache(path: string, callback: AsyncCallback<void>): void

A
Annie_wang 已提交
288
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 已提交
289 290 291

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

A
Annie_wang 已提交
292
**Parameters**
293 294 295 296 297

| Name     | Type                      | Mandatory | Description                     |
| -------- | ------------------------- | --------- | ------------------------------- |
| path     | string                    | Yes       | Path of the target file.        |
| callback | AsyncCallback<void> | Yes       | Callback that returns no value. |
A
annie_wangli 已提交
298

A
Annie_wang 已提交
299
**Example**
300 301 302 303 304 305 306 307

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
308
    console.info("======================>getFilesDirPromise====================>");
309 310 311 312 313 314 315 316 317 318
});  

data_storage.removeStorageFromCache(path + '/mystore', function (err) {
    if (err) {
        console.info("Failed to remove storage from cache with err: " + err);
        return;
    }
    console.info("Succeeded in removing storage from cache.");
})
```
A
annie_wangli 已提交
319 320


A
Annie_wang 已提交
321
## data_storage.removeStorageFromCache
A
annie_wangli 已提交
322 323 324

removeStorageFromCache(path: string): Promise<void>

A
Annie_wang 已提交
325
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 已提交
326 327 328

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

A
Annie_wang 已提交
329
**Parameters**
330 331 332 333

| Name | Type   | Mandatory | Description              |
| ---- | ------ | --------- | ------------------------ |
| path | string | Yes       | Path of the target file. |
A
annie_wangli 已提交
334

A
Annie_wang 已提交
335
**Return value**
336 337 338 339

| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
A
annie_wangli 已提交
340

A
Annie_wang 已提交
341
**Example**
342 343 344 345 346 347 348 349

```js
import featureAbility from '@ohos.ability.featureAbility';

var path;
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
    path = filePath;
350
    console.info("======================>getFilesDirPromise====================>");
351 352 353 354 355 356 357 358 359
});  

let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
promiserevSt.then(() => {
    console.info("Succeeded in removing storage from cache.");
}).catch((err) => {
    console.info("Failed to remove storage from cache with err: " + err);
})
```
A
annie_wangli 已提交
360 361 362


## Storage
Z
zengyawen 已提交
363 364 365 366

Provides APIs for obtaining and modifying storage data.


A
annie_wangli 已提交
367 368 369
### getSync

getSync(key: string, defValue: ValueType): ValueType
Z
zengyawen 已提交
370 371 372

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

A
Annie_wang 已提交
375
**Parameters**
376 377 378 379 380

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

A
Annie_wang 已提交
382
**Return value**
383 384 385 386

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

A
Annie_wang 已提交
388
**Example**
389 390 391 392 393

```js
let value = storage.getSync('startup', 'default');
console.info("The value of startup is " + value);
```
A
annie_wangli 已提交
394 395 396 397 398


### get

get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void
Z
zengyawen 已提交
399

A
Annie_wang 已提交
400
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 已提交
401 402 403

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

A
Annie_wang 已提交
404
**Parameters**
405 406 407 408 409 410

| 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<ValueType> | Yes       | Callback used to return the execution result.                |
A
annie_wangli 已提交
411

A
Annie_wang 已提交
412
**Example**
413 414 415 416 417 418

```js
storage.get('startup', 'default', function(err, value) {
    if (err) {
        console.info("Failed to get the value of startup with err: " + err);
        return;
A
annie_wangli 已提交
419
      }
420 421 422
    console.info("The value of startup is " + value);
})
```
A
annie_wangli 已提交
423 424 425 426 427


### get

get(key: string, defValue: ValueType): Promise<ValueType>
Z
zengyawen 已提交
428

A
Annie_wang 已提交
429
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 已提交
430 431 432

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

A
Annie_wang 已提交
433 434
**Parameters**

435 436 437 438
| 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. |
A
annie_wangli 已提交
439

A
Annie_wang 已提交
440
**Return value**
441 442 443 444

| Type                     | Description                        |
| ------------------------ | ---------------------------------- |
| Promise<ValueType> | Promise used to return the result. |
A
annie_wangli 已提交
445

A
Annie_wang 已提交
446
**Example**
447 448 449 450 451 452 453 454
```js
let promiseget = storage.get('startup', 'default');
promiseget.then((value) => {
    console.info("The value of startup is " + value)
}).catch((err) => {
    console.info("Failed to get the value of startup with err: " + err);
})
```
Z
zengyawen 已提交
455 456


A
annie_wangli 已提交
457 458 459 460 461
### 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 已提交
462

A
annie_wangli 已提交
463 464
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
465
**Parameters**
466 467 468 469 470

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

A
Annie_wang 已提交
472
**Example**
473 474 475 476

```js
storage.putSync('startup', 'auto')
```
A
annie_wangli 已提交
477 478 479 480 481 482


### put

put(key: string, value: ValueType, callback: AsyncCallback<void>): void

A
Annie_wang 已提交
483
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 已提交
484 485 486

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

A
Annie_wang 已提交
487
**Parameters**
488 489 490 491 492 493

| 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<void> | Yes       | Callback that returns no value.                              |
A
annie_wangli 已提交
494

A
Annie_wang 已提交
495
**Example**
496 497 498 499 500 501 502 503 504 505

```js
storage.put('startup', 'auto', function (err) {
    if (err) {
        console.info("Failed to put the value of startup with err: " + err);
        return;
    }
    console.info("Succeeded in putting the value of startup.");
})
```
A
annie_wangli 已提交
506 507 508 509 510 511


### put

put(key: string, value: ValueType): Promise<void>

A
Annie_wang 已提交
512
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 已提交
513 514 515

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

A
Annie_wang 已提交
516
**Parameters**
517 518 519 520 521

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

A
Annie_wang 已提交
523
**Return value**
524 525 526 527

| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
A
annie_wangli 已提交
528

A
Annie_wang 已提交
529
**Example**
530 531 532 533 534 535 536 537
```js
let promiseput = storage.put('startup', 'auto');
promiseput.then(() => {
    console.info("Succeeded in putting the value of startup.");
}).catch((err) => {
    console.info("Failed to put the value of startup with err: " + err);
})
```
A
annie_wangli 已提交
538 539 540 541 542 543 544


### hasSync

hasSync(key: string): boolean

Checks whether the storage object contains data with a given key.
Z
zengyawen 已提交
545

A
annie_wangli 已提交
546 547
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
548
**Parameters**
549 550 551 552

| Name | Type   | Mandatory | Description                          |
| ---- | ------ | --------- | ------------------------------------ |
| key  | string | Yes       | Key of the data. It cannot be empty. |
A
annie_wangli 已提交
553

A
Annie_wang 已提交
554
**Return value**
555 556 557 558

| Type    | Description                                                  |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
A
annie_wangli 已提交
559

A
Annie_wang 已提交
560
**Example**
561 562 563 564 565 566 567

```js
let isExist = storage.hasSync('startup');
if (isExist) {
    console.info("The key of startup is contained.");
}
```
A
annie_wangli 已提交
568 569 570 571 572 573


### has

has(key: string, callback: AsyncCallback<boolean>): boolean

A
Annie_wang 已提交
574
Checks whether the storage object contains data with a given key. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
575 576 577

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

A
Annie_wang 已提交
578
**Parameters**
579 580 581 582 583

| Name     | Type                         | Mandatory | Description                                   |
| -------- | ---------------------------- | --------- | --------------------------------------------- |
| key      | string                       | Yes       | Key of the data. It cannot be empty.          |
| callback | AsyncCallback<boolean> | Yes       | Callback used to return the execution result. |
A
annie_wangli 已提交
584

A
Annie_wang 已提交
585
**Return value**
586 587 588 589

| Type    | Description                                                  |
| ------- | ------------------------------------------------------------ |
| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
A
annie_wangli 已提交
590

A
Annie_wang 已提交
591
**Example**
592 593 594 595 596 597 598 599 600 601 602 603

```js
storage.has('startup', function (err, isExist) {
    if (err) {
        console.info("Failed to check the key of startup with err: " + err);
        return;
    }
    if (isExist) {
        console.info("The key of startup is contained.");
    }
})
```
A
annie_wangli 已提交
604 605 606 607 608 609


### has

has(key: string): Promise<boolean>

A
Annie_wang 已提交
610
Checks whether the storage object contains data with a given key. This API uses a promise to return the result.
A
annie_wangli 已提交
611 612 613

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

A
Annie_wang 已提交
614
**Parameters**
615 616 617 618

| Name | Type   | Mandatory | Description                          |
| ---- | ------ | --------- | ------------------------------------ |
| key  | string | Yes       | Key of the data. It cannot be empty. |
A
annie_wangli 已提交
619

A
Annie_wang 已提交
620
**Return value**
621 622 623 624

| Type                   | Description                        |
| ---------------------- | ---------------------------------- |
| Promise<boolean> | Promise used to return the result. |
A
annie_wangli 已提交
625

A
Annie_wang 已提交
626
**Example**
627 628 629 630 631 632 633 634 635 636 637

```js
let promisehas = storage.has('startup')
promisehas.then((isExist) => {
    if (isExist) {
        console.info("The key of startup is contained.");
    }
}).catch((err) => {
    console.info("Failed to check the key of startup with err: " + err);
})
```
A
annie_wangli 已提交
638 639 640 641 642 643 644


### deleteSync

deleteSync(key: string): void

Deletes data with the specified key from this storage object.
Z
zengyawen 已提交
645

A
annie_wangli 已提交
646 647
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
648
**Parameters**
649 650 651 652

| Name | Type   | Mandatory | Description                          |
| ---- | ------ | --------- | ------------------------------------ |
| key  | string | Yes       | Key of the data. It cannot be empty. |
A
annie_wangli 已提交
653

A
Annie_wang 已提交
654
**Example**
655 656 657 658

```js
storage.deleteSync('startup')
```
A
annie_wangli 已提交
659 660


A
Annie_wang 已提交
661
### delete
A
annie_wangli 已提交
662 663 664

delete(key: string, callback: AsyncCallback<void>): void

A
Annie_wang 已提交
665
Deletes data with the specified key from this storage object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
666 667 668

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

A
Annie_wang 已提交
669
**Parameters**
670 671 672 673 674

| Name     | Type                      | Mandatory | Description                          |
| -------- | ------------------------- | --------- | ------------------------------------ |
| key      | string                    | Yes       | Key of the data. It cannot be empty. |
| callback | AsyncCallback<void> | Yes       | Callback that returns no value.      |
A
annie_wangli 已提交
675

A
Annie_wang 已提交
676
**Example**
677 678 679 680 681 682 683 684 685 686

```js
storage.delete('startup', function (err) {
    if (err) {
        console.info("Failed to delete startup key failed err: " + err);
        return;
    }
    console.info("Succeeded in deleting startup key.");
})
```
A
annie_wangli 已提交
687

Z
zengyawen 已提交
688

A
annie_wangli 已提交
689
### delete
Z
zengyawen 已提交
690

A
annie_wangli 已提交
691
delete(key: string): Promise<void>
Z
zengyawen 已提交
692

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

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

A
Annie_wang 已提交
697
**Parameters**
698 699 700 701

| Name | Type   | Mandatory | Description      |
| ---- | ------ | --------- | ---------------- |
| key  | string | Yes       | Key of the data. |
Z
zengyawen 已提交
702

A
Annie_wang 已提交
703
**Return value**
704 705 706 707

| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
Z
zengyawen 已提交
708

A
Annie_wang 已提交
709
**Example**
710 711 712 713 714 715 716 717 718

```js
let promisedel = storage.delete('startup')
promisedel.then(() => {
    console.info("Succeeded in deleting startup key.");
}).catch((err) => {
    console.info("Failed to delete startup key failed err: " + err);
})
```
A
annie_wangli 已提交
719 720 721 722 723 724 725 726 727


### 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 已提交
728

A
Annie_wang 已提交
729
**Example**
730 731 732 733

```js
storage.flushSync()
```
A
annie_wangli 已提交
734 735 736


### flush
Z
zengyawen 已提交
737

A
annie_wangli 已提交
738
flush(callback: AsyncCallback<void>): void
Z
zengyawen 已提交
739

A
Annie_wang 已提交
740
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 已提交
741

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

A
Annie_wang 已提交
744
**Parameters**
745 746 747 748

| Name     | Type                      | Mandatory | Description                     |
| -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback<void> | Yes       | Callback that returns no value. |
Z
zengyawen 已提交
749

A
Annie_wang 已提交
750
**Example**
751 752 753 754 755 756 757 758 759 760

```js
storage.flush(function (err) {
    if (err) {
        console.info("Failed to flush to file with err: " + err);
        return;
    }
    console.info("Succeeded in flushing to file.");
})
```
Z
zengyawen 已提交
761 762


A
annie_wangli 已提交
763
### flush
Z
zengyawen 已提交
764

A
annie_wangli 已提交
765
flush(): Promise<void>
Z
zengyawen 已提交
766

A
Annie_wang 已提交
767
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 已提交
768

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

A
Annie_wang 已提交
771
**Return value**
772 773 774 775

| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
A
annie_wangli 已提交
776

A
Annie_wang 已提交
777
**Example**
778 779 780 781 782 783 784 785 786

```js
let promiseflush = storage.flush();
promiseflush.then(() => {
    console.info("Succeeded in flushing to file.");
}).catch((err) => {
    console.info("Failed to flush to file with err: " + err);
})
```
A
annie_wangli 已提交
787 788 789 790 791 792 793


### clearSync

clearSync(): void

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

A
annie_wangli 已提交
795 796
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
797
**Example**
798 799 800 801

```js
storage.clearSync()
```
A
annie_wangli 已提交
802 803 804 805 806 807


### clear

clear(callback: AsyncCallback<void>): void

A
Annie_wang 已提交
808
Clears this **Storage** object. This API uses an asynchronous callback to return the result.
A
annie_wangli 已提交
809 810 811

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

A
Annie_wang 已提交
812
**Parameters**
813 814 815 816

| Name     | Type                      | Mandatory | Description                     |
| -------- | ------------------------- | --------- | ------------------------------- |
| callback | AsyncCallback<void> | Yes       | Callback that returns no value. |
Z
zengyawen 已提交
817

A
Annie_wang 已提交
818
**Example**
819 820 821 822 823 824 825 826 827 828

```js
storage.clear(function (err) {
    if (err) {
        console.info("Failed to clear the storage with err: " + err);
        return;
    }
    console.info("Succeeded in clearing the storage.");
})
```
A
annie_wangli 已提交
829 830 831 832 833 834


### clear

clear(): Promise<void>

A
Annie_wang 已提交
835
Clears this **Storage** object. This API uses a promise to return the result.
A
annie_wangli 已提交
836 837 838

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

A
Annie_wang 已提交
839
**Return value**
840 841 842 843

| Type                | Description                    |
| ------------------- | ------------------------------ |
| Promise<void> | Promise that returns no value. |
A
annie_wangli 已提交
844

A
Annie_wang 已提交
845
**Example**
846 847 848 849 850 851 852 853 854

```js
let promiseclear = storage.clear();
promiseclear.then(() => {
    console.info("Succeeded in clearing the storage.");
}).catch((err) => {
    console.info("Failed to clear the storage with err: " + err);
})
```
A
annie_wangli 已提交
855 856 857 858 859 860 861


### on('change')

on(type: 'change', callback: Callback<StorageObserver>): 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 已提交
862

A
annie_wangli 已提交
863 864
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
865
**Parameters**
866 867 868 869 870

| Name     | Type                                                | Description                                                  |
| -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| type     | string                                              | Event type. The value **change** indicates data change events. |
| callback | Callback<[StorageObserver](#storageobserver)> | Callback used to return data changes.                        |
A
annie_wangli 已提交
871

A
Annie_wang 已提交
872
**Example**
873 874 875 876 877 878 879 880 881

```js
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.
```
A
annie_wangli 已提交
882 883 884 885 886


### off('change')

off(type: 'change', callback: Callback<StorageObserver>): void
Z
zengyawen 已提交
887 888 889

Unsubscribes from data changes.

A
annie_wangli 已提交
890 891
**System capability**: SystemCapability.DistributedDataManager.Preferences.Core

A
Annie_wang 已提交
892
**Parameters**
893 894 895 896 897

| Name     | Type                                                | Description                                                  |
| -------- | --------------------------------------------------- | ------------------------------------------------------------ |
| type     | string                                              | Event type. The value **change** indicates data change events. |
| callback | Callback<[StorageObserver](#storageobserver)> | Callback used to return data changes.                        |
A
annie_wangli 已提交
898

A
Annie_wang 已提交
899
**Example**
900 901 902 903 904 905 906

```js
var observer = function (key) {
    console.info("The key of " + key + " changed.");
}
storage.off('change', observer);
```
A
annie_wangli 已提交
907 908 909 910 911 912


## StorageObserver

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

913 914 915
| Name | Type   | Mandatory | Description   |
| ---- | ------ | --------- | ------------- |
| key  | string | No        | Data changed. |
A
Annie_wang 已提交
916 917 918 919 920 921 922

## ValueType

Enumerates the value types.

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

923 924 925 926 927
| Type    | Description                   |
| ------- | ----------------------------- |
| number  | The value is a number.        |
| string  | The value is a string.        |
| boolean | The value is of Boolean type. |