js-apis-distributed-data.md 140.8 KB
Newer Older
A
annie_wangli 已提交
1
# Distributed Data Management
Z
zengyawen 已提交
2

A
annie_wangli 已提交
3
>![](../../public_sys-resources/icon-note.gif) **NOTE**
A
annie_wangli 已提交
4
>
Z
zengyawen 已提交
5 6
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

A
annie_wangli 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9 10 11 12

```
import distributedData from '@ohos.data.distributedData';
```
A
annie_wangli 已提交
13 14
## System Capabilities
SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
15 16 17 18




A
annie_wangli 已提交
19
## distributedData.createKVManager
Z
zengyawen 已提交
20

A
annie_wangli 已提交
21 22 23 24 25 26 27 28 29 30
createKVManager(config: KVManagerConfig, callback: AsyncCallback<KVManager>): void

Creates a **KVManager** object to manage key-value (KV) stores. This method uses an asynchronous callback to return the result.

- Parameters

| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| config | [KVManagerConfig](#kvmanagerconfig) | Yes| Configuration of the **KVManager** object, including the bundle name and user information of the caller.|
| callback | AsyncCallback<[KVManager](#kvmanager)> | Yes| Callback invoked to return the **KVManager** object created.|
Z
zengyawen 已提交
31 32 33 34 35 36 37 38 39 40 41


-   Example

    ```
    let kvManager;
    try {
        const kvManagerConfig = {
            bundleName : 'com.example.datamanagertest',
            userInfo : {
                userId : '0',
A
annie_wangli 已提交
42
                userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
            }
        }
        distributedData.createKVManager(kvManagerConfig, function (err, manager) {
            if (err) {
                console.log("createKVManager err: "  + JSON.stringify(err));
                return;
            }
            console.log("createKVManager success");
            kvManager = manager;
        });
    } catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```

A
annie_wangli 已提交
58
## distributedData.createKVManager
Z
zengyawen 已提交
59

A
annie_wangli 已提交
60
createKVManager(config: KVManagerConfig): Promise<KVManager>
Z
zengyawen 已提交
61

A
annie_wangli 已提交
62
Creates a **KVManager** object to manage KV stores. This method uses a promise to return the result.
Z
zengyawen 已提交
63 64

-   Parameters
A
annie_wangli 已提交
65

A
annie_wangli 已提交
66 67 68
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| config |[KVManagerConfig](#kvmanager) | Yes| Configuration of the **KVManager** object, including the bundle name and user information of the caller.|
A
annie_wangli 已提交
69

A
annie_wangli 已提交
70
-   Return value
Z
zengyawen 已提交
71

A
annie_wangli 已提交
72 73 74
| Type| Description|
| -------- | -------- |
| Promise<[KVManager](#kvmanager)> | Promise used to return the **KVManager** object created.|
A
annie_wangli 已提交
75

Z
zengyawen 已提交
76 77 78 79 80 81 82 83 84
-   Example

    ```
    let kvManager;
    try {
        const kvManagerConfig = {
            bundleName : 'com.example.datamanagertest',
            userInfo : {
                userId : '0',
A
annie_wangli 已提交
85
                userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99
            }
        }
        distributedData.createKVManager(kvManagerConfig).then((manager) => {
            console.log("createKVManager success");
            kvManager = manager;
        }).catch((err) => {
            console.log("createKVManager err: "  + JSON.stringify(err));
        });
    } catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


A
annie_wangli 已提交
100 101 102 103 104 105 106 107 108 109 110 111
## KVManagerConfig

Provides configuration of the **KVManager** object, including the bundle name and user information of the caller.

| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| userInfo | [UserInfo](#userinfo) | Yes| User information.|
| bundleName | string | Yes| Bundle name.|



## UserInfo
Z
zengyawen 已提交
112 113 114

Defines user information.

A
annie_wangli 已提交
115 116 117 118 119 120 121
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| userId | string | Yes| User ID.|
| userType | [UserType](#usertype) | Yes| User type.|


## UserType
Z
zengyawen 已提交
122 123 124

Defines the user type.

A
annie_wangli 已提交
125 126 127 128 129 130 131 132 133 134 135 136
| Name| Default Value| Description|
| ----- | ------ | ------ |
| SAME\_USER_ID| 0 | User who logs in to different devices using the same account.|


## KVManager

Creates a **KVManager** object to obtain KV store information. Before calling any method in **KVManager**, you must use **createKVManager** to create a **KVManager** object.

### getKVStore

getKVStore<T extends KVStore>(storeId: string, options: Options, callback: AsyncCallback&lt;T&gt;): void
Z
zengyawen 已提交
137 138 139

Creates and obtains a KV store. This method uses an asynchronous callback to return the result.

A
annie_wangli 已提交
140
- Parameters
Z
zengyawen 已提交
141

A
annie_wangli 已提交
142 143 144 145 146
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| storeId | string | Yes| Unique identifier of the KV store. The length cannot exceed [MAX\_STORE\_ID\_LENGTH](#constants).|
| options | [Options](#options) | Yes| Configuration of the KV store.|
| callback | AsyncCallback&lt;T&gt;, &lt;T extends KVStore&gt;| Yes| Callback invoked to return the KV store created.|
Z
zengyawen 已提交
147 148 149 150 151

-   Example

    ```
    let kvStore;
A
annie_wangli 已提交
152
    let kvManager;
Z
zengyawen 已提交
153 154 155 156 157 158
    try {
        const options = {
            createIfMissing : true,
            encrypt : false,
            backup : false,
            autoSync : true,
A
annie_wangli 已提交
159 160
            kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
            securityLevel : distributedData.SecurityLevel.S2,
Z
zengyawen 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
        };
        kvManager.getKVStore('storeId', options, function (err, store) {
            if (err) {
                console.log("getKVStore err: "  + JSON.stringify(err));
                return;
            }
            console.log("getKVStore success");
            kvStore = store;
        });
    } catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


A
annie_wangli 已提交
176
### getKVStore
Z
zengyawen 已提交
177

A
annie_wangli 已提交
178
getKVStore&lt;T extends KVStore&gt;(storeId: string, options: Options): Promise&lt;T&gt;
Z
zengyawen 已提交
179 180 181

Creates and obtains a KV store. This method uses a promise to return the result.

A
annie_wangli 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
- Parameters

| Name| Type| Mandatory| Description|
| ------- | ---------------------- | ---- | -------------------- |
| storeId  | string      | Yes| Unique identifier of the KV store. The length cannot exceed [MAX\_STORE\_ID\_LENGTH](#constants).|
| options  | [Options](#options)   | Yes| Configuration of the KV store.|


-   Return value

| Type| Description|
| -------------------------------------- | ------------------------ |
| Promise&lt;T&gt; &lt;T extends KVStore&gt; | Promise used to return the KV store created.|


- Example

  ```
  let kvStore;
  let kvManager;
  try {
      const options = {
          createIfMissing : true,
          encrypt : false,
          backup : false,
          autoSync : true,
          kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
          securityLevel : distributedData.SecurityLevel.S2,
      };
      kvManager.getKVStore('storeId', options).then((store) => {
          console.log("getKVStore success");
          kvStore = store;
      }).catch((err) => {
          console.log("getKVStore err: "  + JSON.stringify(err));
      });
  } catch (e) {
      console.log("An unexpected error occurred. Error:" + e);
  }
  ```

### closeKVStore<sup>8+</sup> ###

closeKVStore(appId: string, storeId: string, kvStore: KVStore, callback: AsyncCallback&lt;void&gt;): void;

Closes a KV store. This method uses an asynchronous callback to return the result.

- Parameters


| Name| Type| Mandatory| Description|
| ------- | -----------------   | ---- | --------------------------- |
A
annie_wangli 已提交
233
| appId    | string              | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
234 235 236
| storeId  | string  | Yes| Unique identifier of the KV store to close. The length cannot exceed the value of [MAX\_STORE\_ID_LENGTH](#constants).|
| kvStore  | [KVStore](#kvstore) | Yes| KV store to close.|
| callback | AsyncCallback&lt;void&gt; | Yes| Callback used to return the result. If the KV store is closed, **true** will be returned. Otherwise, **false** will be returned.|
Z
zengyawen 已提交
237 238 239 240 241 242


-   Example

    ```
    let kvStore;
A
annie_wangli 已提交
243 244 245 246 247 248 249 250 251 252
    let kvManager;
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        schema : '',
        securityLevel : distributedData.SecurityLevel.S2,
    }
Z
zengyawen 已提交
253
    try {
A
annie_wangli 已提交
254 255
        kvManager.getKVStore('storeId', options, async function (err, store) {
            console.log('getKVStore success');
Z
zengyawen 已提交
256
            kvStore = store;
A
annie_wangli 已提交
257 258 259
            await kvManager.closeKVStore('appId', 'storeId', kvStore, function (err, data) {
                console.log('closeKVStore success');
            });
Z
zengyawen 已提交
260 261
        });
    } catch (e) {
A
annie_wangli 已提交
262
        console.log('closeKVStore e ' + e);
Z
zengyawen 已提交
263 264 265
    }
    ```

A
annie_wangli 已提交
266
### closeKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
267

A
annie_wangli 已提交
268
closeKVStore(appId: string, storeId: string, kvStore: KVStore): Promise&lt;void&gt;;
Z
zengyawen 已提交
269

A
annie_wangli 已提交
270
Closes a KV store. This method uses a promise to return the result.
Z
zengyawen 已提交
271

A
annie_wangli 已提交
272
- Parameters
Z
zengyawen 已提交
273

A
annie_wangli 已提交
274 275
| Name| Type| Mandatory| Description|
| -----  | ------  | ---- | ----------------------------- |
A
annie_wangli 已提交
276
| appId  | string  | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
277 278
| storeId | string | Yes| Unique identifier of the KV store to close. The length cannot exceed the value of [MAX\_STORE\_ID_LENGTH](#constants).|
| kvStore | [KVStore](#kvstore)  | Yes| KV store to close.|
Z
zengyawen 已提交
279

A
annie_wangli 已提交
280
- Return value
Z
zengyawen 已提交
281

A
annie_wangli 已提交
282 283 284
| Type| Description|
| ------------- | -------------- |
| Promise<void> | Promise used to return the result. If the KV store is closed, **true** will be returned. Otherwise, **false** will be returned.|
Z
zengyawen 已提交
285

A
annie_wangli 已提交
286
-   Example
Z
zengyawen 已提交
287

A
annie_wangli 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    ```
    let kvManager;
    let kvStore;
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        schema : '',
        securityLevel : distributedData.SecurityLevel.S2,
    }
    try {
        kvManager.getKVStore('storeId', options).then(async (store) => {
            console.log('getKVStore success');
            kvStore = store;
            await kvManager.closeKVStore('appId', 'storeId', kvStore).then(() => {
                console.log('closeKVStore success');
            }).catch((err) => {
                console.log('closeKVStore err ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('CloseKVStore getKVStore err ' + JSON.stringify(err));
        });
    } catch (e) {
        console.log('closeKVStore e ' + e);
    }
    ```


### deleteKVStore<sup>8+</sup> ###

deleteKVStore(appId: string, storeId: string, callback: AsyncCallback&lt;void&gt;): void;

Deletes a KV store. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
328
| appId  | string  | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
329 330
| storeId | string | Yes| Unique identifier of the KV store to delete. The length cannot exceed the value of [MAX\_STORE\_ID_LENGTH](#constants).|
| callback | AsyncCallback&lt;void&gt;  | Yes| Callback used to return the result. If the KV store is deleted, **true** will be returned. Otherwise, **false** will be returned.|
Z
zengyawen 已提交
331 332 333 334 335


-   Example

    ```
A
annie_wangli 已提交
336 337 338 339 340 341 342 343 344 345 346
    let kvManager;
    let kvStore;
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        schema : '',
        securityLevel : distributedData.SecurityLevel.S2,
    }
Z
zengyawen 已提交
347
    try {
A
annie_wangli 已提交
348 349 350 351 352 353
        kvManager.getKVStore('store', options, async function (err, store) {
            console.log('getKVStore success');
            kvStore = store;
            await kvManager.deleteKVStore('appId', 'storeId', function (err, data) {
                console.log('deleteKVStore success');
            });
Z
zengyawen 已提交
354
        });
A
annie_wangli 已提交
355 356
    } catch (e) {
        console.log('DeleteKVStore e ' + e);
Z
zengyawen 已提交
357 358 359
    }
    ```

A
annie_wangli 已提交
360
### deleteKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
361

A
annie_wangli 已提交
362
deleteKVStore(appId: string, storeId: string): Promise&lt;void&gt;;
Z
zengyawen 已提交
363

A
annie_wangli 已提交
364
Deletes a KV store. This method uses a promise to return the result.
Z
zengyawen 已提交
365 366

-   Parameters
A
annie_wangli 已提交
367

A
annie_wangli 已提交
368 369
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
370
| appId  | string  | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
371
| storeId | string | Yes| Unique identifier of the KV store to delete. The length cannot exceed the value of [MAX\_STORE\_ID_LENGTH](#constants).|
Z
zengyawen 已提交
372 373


A
annie_wangli 已提交
374
-   Return value
Z
zengyawen 已提交
375

A
annie_wangli 已提交
376 377 378
| Type| Description|
| ------------- | -------------- |
| Promise&lt;void&gt; | Promise used to return the result. If the KV store is deleted, **true** will be returned. Otherwise, **false** will be returned.|
Z
zengyawen 已提交
379 380 381 382

-   Example

    ```
A
annie_wangli 已提交
383 384 385 386 387 388 389 390 391 392 393
    let kvManager;
    let kvStore;
    const options = {
        createIfMissing : true,
        encrypt : false,
        backup : false,
        autoSync : true,
        kvStoreType : distributedData.KVStoreType.SINGLE_VERSION,
        schema : '',
        securityLevel : distributedData.SecurityLevel.S2,
    }
Z
zengyawen 已提交
394
    try {
A
annie_wangli 已提交
395 396 397 398 399 400 401 402
        kvManager.getKVStore('storId', options).then(async (store) => {
            console.log('getKVStore success');
            kvStore = store;
            await kvManager.deleteKVStore('appId', 'storeId').then(() => {
                console.log('deleteKVStore success');
            }).catch((err) => {
                console.log('deleteKVStore err ' + JSON.stringify(err));
            });
Z
zengyawen 已提交
403
        }).catch((err) => {
A
annie_wangli 已提交
404
            console.log('getKVStore err ' + JSON.stringify(err));
Z
zengyawen 已提交
405
        });
A
annie_wangli 已提交
406 407
    } catch (e) {
        console.log('deleteKVStore e ' + e);
Z
zengyawen 已提交
408 409 410 411
    }
    ```


A
annie_wangli 已提交
412
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
413

A
annie_wangli 已提交
414
getAllKVStoreId(appId: string, callback: AsyncCallback&lt;string[]&gt;): void;
Z
zengyawen 已提交
415

A
annie_wangli 已提交
416
Obtains the IDs of all the KV stores that are created using **getKvStore** and have not been deleted using **deleteKvStore**. This method uses an asynchronous callback to return the result.
Z
zengyawen 已提交
417 418 419

-   Parameters

A
annie_wangli 已提交
420 421
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
422
| appId  | string  | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
423
| callback | AsyncCallback&lt;void&gt; | Yes|Callback used to return the KV store IDs obtained. |
Z
zengyawen 已提交
424 425 426 427

-   Example

    ```
A
annie_wangli 已提交
428
    let kvManager;
Z
zengyawen 已提交
429
    try {
A
annie_wangli 已提交
430 431 432
        kvManager.getAllKVStoreId('appId', function (err, data) {
            console.log('GetAllKVStoreId success');
            console.log('GetAllKVStoreId size = ' + data.length);
Z
zengyawen 已提交
433
        });
A
annie_wangli 已提交
434 435
    } catch (e) {
        console.log('GetAllKVStoreId e ' + e);
Z
zengyawen 已提交
436 437 438 439
    }
    ```


A
annie_wangli 已提交
440
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
441

A
annie_wangli 已提交
442
getAllKVStoreId(appId: string): Promise&lt;string[]&gt;;
Z
zengyawen 已提交
443

A
annie_wangli 已提交
444
Obtains the IDs of all the KV stores that are created using **getKvStore** and have not been deleted using **deleteKvStore**. This method uses a promise to return the result.
Z
zengyawen 已提交
445 446 447

-   Parameters

A
annie_wangli 已提交
448 449
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
450
| appId  | string  | Yes| Bundle name of the app that invokes the KV store.|
A
annie_wangli 已提交
451 452 453


-   Return value
A
annie_wangli 已提交
454

A
annie_wangli 已提交
455 456 457
| Type| Description|
| ------------- | -------------- |
| Promise&lt;string[]&gt;| Promise used to return the KV store IDs obtained.|
Z
zengyawen 已提交
458 459 460 461

-   Example

    ```
A
annie_wangli 已提交
462
    let kvManager;
Z
zengyawen 已提交
463
    try {
A
annie_wangli 已提交
464 465 466 467
        console.log('GetAllKVStoreId');
        kvManager.getAllKVStoreId('apppId').then((data) => {
            console.log('getAllKVStoreId success');
            console.log('size = ' + data.length);
Z
zengyawen 已提交
468
        }).catch((err) => {
A
annie_wangli 已提交
469
            console.log('getAllKVStoreId err ' + JSON.stringify(err));
Z
zengyawen 已提交
470
        });
A
annie_wangli 已提交
471 472
    } catch(e) {
        console.log('getAllKVStoreId e ' + e);
Z
zengyawen 已提交
473 474 475 476
    }
    ```


A
annie_wangli 已提交
477
### on<sup>8+</sup> ###
Z
zengyawen 已提交
478

A
annie_wangli 已提交
479
on(event: 'distributedDataServiceDie', deathCallback: Callback&lt;void&gt;): void;
Z
zengyawen 已提交
480

A
annie_wangli 已提交
481
Subscribes to the **distributedDataServiceDie** events. This method uses a synchronous callback to return the result.
Z
zengyawen 已提交
482 483 484

-   Parameters

A
annie_wangli 已提交
485 486 487 488 489 490
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| event  | 'distributedDataServiceDie'  | Yes| Type of events to subscribe to. |
| deathCallback  | Callback&lt;void&gt;  | Yes| Callback invoked when the distributed data service is dead.|


Z
zengyawen 已提交
491 492 493 494

-   Example

    ```
A
annie_wangli 已提交
495 496 497 498 499 500 501 502 503 504 505
    let kvManager;
    try {
        
        console.log('KVManagerOn');
        const deathCallback = function () {
            console.log('death callback call');
        }
        kvManager.on('distributedDataServiceDie', deathCallback);
    } catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
Z
zengyawen 已提交
506 507 508
    ```


A
annie_wangli 已提交
509
### off<sup>8+</sup> ###
Z
zengyawen 已提交
510

A
annie_wangli 已提交
511
off(event: 'distributedDataServiceDie', deathCallback?: Callback&lt;void&gt;): void;
Z
zengyawen 已提交
512

A
annie_wangli 已提交
513
Unsubscribes from the **distributedDataServiceDie** events. This method uses a synchronous callback to return the result.
Z
zengyawen 已提交
514 515 516

-   Parameters

A
annie_wangli 已提交
517 518 519 520
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| event  | 'distributedDataServiceDie'  | Yes| Type of events to unsubscribe from. |
| deathCallback  | Callback&lt;void&gt;  | No| Callback used to return the **distributedDataServiceDie** events.|
A
annie_wangli 已提交
521

Z
zengyawen 已提交
522 523 524 525

-   Example

    ```
A
annie_wangli 已提交
526 527 528 529 530 531 532 533 534 535 536
    let kvManager;
    try {
        console.log('KVManagerOff');
        const deathCallback = function () {
            console.log('death callback call');
        }
        kvManager.off('distributedDataServiceDie', deathCallback);
    } catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    
Z
zengyawen 已提交
537 538
    ```

A
annie_wangli 已提交
539 540 541
## Options

Provides KV store configuration.
Z
zengyawen 已提交
542

A
annie_wangli 已提交
543 544 545 546 547
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| createIfMissing  | boolean | No| Whether to create a KV store if no database file exists. By default, a KV store is created.|
| encrypt  | boolean | No|Whether to encrypt database files. By default, database files are not encrypted.|
| backup  | boolean | No|Whether to back up database files. By default, database files are backed up. |
A
annie_wangli 已提交
548
| autoSync  | boolean | No|Whether to automatically synchronize database files. By default, database files are not automatically synchronized.|
A
annie_wangli 已提交
549 550
| kvStoreType | [KVStoreType](#kvstoretype) | No|Type of the KV store to create. By default, a device KV store is created. The device KV store stores data for multiple devices that collaborate with each other.|
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set.|
Z
zengyawen 已提交
551 552


A
annie_wangli 已提交
553
## KVStoreType
Z
zengyawen 已提交
554

A
annie_wangli 已提交
555
Defines the KV store types.
Z
zengyawen 已提交
556

A
annie_wangli 已提交
557 558 559 560 561
| Name| Default Value| Description|
| ---   | ----  | ----------------------- |
| DEVICE_COLLABORATION  | 0 | Device KV store.|
| SINGLE_VERSION  | 1 | Single KV store.|
| MULTI_VERSION   | 2 | Multi-version KV store. This type is not supported currently.|
Z
zengyawen 已提交
562 563 564



A
annie_wangli 已提交
565
## SecurityLevel
Z
zengyawen 已提交
566

A
annie_wangli 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
Defines the KV store security levels.

| Name| Default Value| Description|
| ---   | ----  | ----------------------- |
| NO_LEVEL  | 0 | No security level is set for the KV store.|
| S0  | 1 | The KV store security level is public.|
| S1  | 2 | The KV store security level is low. If data leakage occurs, minor impact will be caused on the database.|
| S2  | 3 | The KV store security level is medium. If data leakage occurs, moderate impact will be caused on the database.|
| S3  | 5 | The KV store security level is high. If data leakage occurs, major impact will be caused on the database.|
| S4  | 6 | The KV store security level is critical. If data leakage occurs, severe impact will be caused on the database.|


## Constants

Defines the KV store constants.

| Name| Default Value| Description|
| ---   | ----  | ----------------------- |
| MAX\_KEY_LENGTH| 1024 | Maximum length (in bytes) of a key in the KV store.|
| MAX\_VALUE_LENGTH| 4194303 | Maximum length (in bytes) of a value in the KV store.|
| MAX\_KEY\_LENGTH\_DEVICE| 896 | Maximum length of the device coordinate key.|
A
annie_wangli 已提交
588
| MAX\_STORE\_ID\_LENGTH| 128 | Maximum length (in bytes) of a KV store ID.|
A
annie_wangli 已提交
589 590 591 592 593
| MAX\_QUERY_LENGTH| 512000 | Maximum query length.|
| MAX\_BATCH_SIZE| 128 | Maximum size of a batch operation.|

## Schema<sup>8+</sup> ##

A
annie_wangli 已提交
594
Defines a database schema. When creating or opening a KV store, you can create a **Schema** object and put it into **Options**.
Z
zengyawen 已提交
595

A
annie_wangli 已提交
596 597 598 599 600 601 602 603 604 605 606
### toJsonString<sup>8+</sup> ###

toJsonString():string;

Obtains the schema in JSON format.

-   Return value

| Type| Description|
| ------------- | -------------- |
| string |Schema in JSON format obtained.|
Z
zengyawen 已提交
607 608 609 610

-   Example

    ```
A
annie_wangli 已提交
611
    import ddm from '@ohos.data.distributedData';
Z
zengyawen 已提交
612
    try {
A
annie_wangli 已提交
613 614 615 616 617
        let schema = new ddm.Schema();
        const str = schema.toJsonString();
        console.log("schema: " + str);
    } catch (e) {
       console.log("toJsonString " + e);
Z
zengyawen 已提交
618 619 620 621
    }
    ```


A
annie_wangli 已提交
622
## FieldNode<sup>8+</sup> ##
Z
zengyawen 已提交
623

A
annie_wangli 已提交
624
Defines a node of a **Schema** instance. It provides methods for defining values stored in the KV store.
Z
zengyawen 已提交
625

A
annie_wangli 已提交
626 627 628 629 630
### appendChild<sup>8+</sup> ###

appendChild(child: FieldNode): boolean;

Adds a child node to this **FieldNode**.
Z
zengyawen 已提交
631 632 633

-   Parameters

A
annie_wangli 已提交
634 635
| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
636
| child  | [FieldNode](#FieldNode)  | Yes| Child node to add.|
A
annie_wangli 已提交
637

A
annie_wangli 已提交
638 639

-   Return value
Z
zengyawen 已提交
640

A
annie_wangli 已提交
641 642 643
| Type| Description|
| ------------- | -------------- |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
644 645 646 647

-   Example

    ```
A
annie_wangli 已提交
648
    import ddm from '@ohos.data.distributedData';
Z
zengyawen 已提交
649
    try {
A
annie_wangli 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
        let node = new ddm.FieldNode("root");
        let child1 = new ddm.FieldNode("child1");
        let child2 = new ddm.FieldNode("child2");
        let child3 = new ddm.FieldNode("child3");
        node.appendChild(child1);
        node.appendChild(child2);
        node.appendChild(child3);
        console.log("appendNode " + node.toJson());
        child1 = null;
        child2 = null;
        child3 = null;
        node = null;
    } catch (e) {
        console.log("AppendChild " + e);
    }
    ```


### toJson<sup>8+</sup> ###

toJson(): string;

Obtains the field name.

-   Return value

| Type| Description|
| ------ | --------------    |
| string |Field name obtained.|

-   Example

    ```
	import ddm from '@ohos.data.distributedData';
    try {
        let node = new ddm.FieldNode("root");
        let child = new ddm.FieldNode("child");
        node.appendChild(child);
        console.log("appendNode " + node.toJson());
    } catch (e) {
       console.log("ToJson " + e);
    }
    ```


## KvStoreResultSet<sup>8+</sup> ##

A
annie_wangli 已提交
697
Provides methods to obtain the KV store result set and query or move the data read position. Before calling any method in **KvStoreResultSet**, you must use **KvStore** to create a **KvStore** instance.
A
annie_wangli 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719

### getCount<sup>8+</sup> ###

getCount(): number;

Obtains the number of rows in the result set.

-   Return value

| Type| Description|
| ------ | --------------    |
| number |Number of rows obtained.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
Z
zengyawen 已提交
720
        }).catch((err) => {
A
annie_wangli 已提交
721
            console.log('getResultSet fail ' + err);
Z
zengyawen 已提交
722
        });
A
annie_wangli 已提交
723 724 725 726
        const count = resultSet.getCount();
        console.log("GetCount " + count);
    } catch (e) {
        console.log("GetCount fail " + e);
Z
zengyawen 已提交
727 728 729 730
    }
    ```


A
annie_wangli 已提交
731
### getPosition<sup>8+</sup> ###
Z
zengyawen 已提交
732

A
annie_wangli 已提交
733
getPosition(): number;
Z
zengyawen 已提交
734

A
annie_wangli 已提交
735
Obtains the current data read position (position from which data is read) in the result set.
Z
zengyawen 已提交
736

A
annie_wangli 已提交
737
-   Return value
Z
zengyawen 已提交
738

A
annie_wangli 已提交
739 740 741
| Type| Description|
| ------ | --------------    |
| number |Current data read position obtained.|
Z
zengyawen 已提交
742 743 744 745

-   Example

    ```
A
annie_wangli 已提交
746 747 748 749 750 751 752 753 754 755 756 757 758 759
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const positon = resultSet.getPosition();
        console.log("getPosition " + positon);
    } catch (e) {
        console.log("GetPosition fail " + e);
    }
Z
zengyawen 已提交
760 761 762
    ```


A
annie_wangli 已提交
763
### moveToFirst<sup>8+</sup> ###
Z
zengyawen 已提交
764

A
annie_wangli 已提交
765
moveToFirst(): boolean;
Z
zengyawen 已提交
766

A
annie_wangli 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
Moves the data read position to the first row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToFirst();
        console.log("moveToFirst " + moved);
    } catch (e) {
        console.log("MoveToFirst fail " + e);
    }
    ```


### moveToLast<sup>8+</sup> ###

moveToLast(): boolean;

Moves the data read position to the last row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToLast();
        console.log("moveToLast " + moved);
    } catch (e) {
        console.log("moveToLast fail " + e);
    }
    ```


### moveToNext<sup>8+</sup> ###

moveToNext(): boolean;

Moves the data read position to the next row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToNext();
        console.log("moveToNext " + moved);
    } catch (e) {
        console.log("moveToNext fail " + e);
    }
    ```


### moveToPrevious<sup>8+</sup> ###

moveToPrevious(): boolean;

Moves the data read position to the previous row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToPrevious();
        console.log("moveToPrevious " + moved);
    } catch (e) {
        console.log("moveToPrevious fail " + e);
    }
    ```


### move<sup>8+</sup> ###

move(offset: number): boolean;

Moves the data read position with the specified offset from the current position.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| offset  | number  | Yes| Offset to move the data read position. A negative value means to move backward, and a positive value means to move forward.|

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.move();
        console.log("move " + moved);
    } catch (e) {
        console.log("move fail " + e);
    }
    ```


### moveToPosition<sup>8+</sup> ###

moveToPosition(position: number): boolean;

Moves the data read position from 0 to an absolute position.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
939
| position  | number  | Yes|Absolute position to move to.|
A
annie_wangli 已提交
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToPosition();
        console.log("moveToPosition " + moved);
    } catch (e) {
        console.log("moveToPosition fail " + e);
    }
    ```


### isFirst<sup>8+</sup> ###

isFirst(): boolean;

Checks whether the data read position is the first row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the data read position is the first row; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.isFirst();
        console.log("isFirst " + moved);
    } catch (e) {
        console.log("isFirst fail " + e);
    }
    ```


### isLast<sup>8+</sup> ###

isLast(): boolean;

Checks whether the data read position is the last row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the data read position is the last row; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.isLast();
        console.log("isLast " + moved);
    } catch (e) {
        console.log("isLast fail " + e);
    }
    ```


### isBeforeFirst<sup>8+</sup> ###

isBeforeFirst(): boolean;

Checks whether the data read position is before the first row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the read position is before the first row; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.isBeforeFirst();
        console.log("isBeforeFirst " + moved);
    } catch (e) {
        console.log("isBeforeFirst fail " + e);
    }
    ```


### isAfterLast<sup>8+</sup> ###

isAfterLast(): boolean;

Checks whether the data read position is after the last row.

-   Return value

| Type| Description|
| ------  | --------------    |
| boolean |Returns **true** if the data read position is after the last row; returns **false** otherwise.|

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.isAfterLast();
        console.log("isAfterLast " + moved);
    } catch (e) {
        console.log("isAfterLast fail " + e);
    }
    ```


### getEntry<sup>8+</sup> ###

getEntry(): Entry;

A
annie_wangli 已提交
1099
Obtains a KV pair.
A
annie_wangli 已提交
1100 1101 1102 1103 1104

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
1105
| Entry   |KV pair obtained.|
A
annie_wangli 已提交
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

-   Example

    ```
    let kvStore;
    try {
	    let resultSet;
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + err);
        });
        const moved = resultSet.moveToNext();
        const entry  = resultSet.getEntry();
        console.log("getEntry " + JSON.stringify(entry));
    } catch (e) {
        console.log("getEntry fail " + e);
    }
    ```


## Query<sup>8+</sup> ##

Provides methods to create a **Query** object, which defines different data query criteria.

### reset<sup>8+</sup> ###

reset(): Query;

Resets the **Query** object that contains common query options.

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1142
| [Query](#query8) |**Query** object reset.|
A
annie_wangli 已提交
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.equalTo("key", "value");
        console.log("query is " + query.getSqlLike());
        query.reset();
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("simply calls should be ok :" + e);
    }
    ```


### equalTo<sup>8+</sup> ###

equalTo(field: string, value: number|string|boolean): Query;

A
annie_wangli 已提交
1164
Creates a **Query** object to match the specified field whose value is equal to the specified value.
A
annie_wangli 已提交
1165 1166 1167 1168 1169

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1170 1171
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
| value  | number/string/boolean  | Yes| Value specified.|
A
annie_wangli 已提交
1172 1173 1174 1175 1176

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1177
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.equalTo("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### notEqualTo<sup>8+</sup> ###

notEqualTo(field: string, value: number|string|boolean): Query;

A
annie_wangli 已提交
1197
Creates a **Query** object to match the specified field whose value is not equal to the specified value.
A
annie_wangli 已提交
1198 1199 1200 1201 1202

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1203
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1204 1205 1206 1207 1208 1209
| value  | number/string/boolean  | Yes| Value specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1210
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### greaterThan<sup>8+</sup> ###

greaterThan(field: string, value: number|string|boolean): Query;

A
annie_wangli 已提交
1230
Creates a **Query** object to match the specified field whose value is greater than the specified value.
A
annie_wangli 已提交
1231 1232 1233 1234 1235

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1236
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1237 1238 1239 1240 1241 1242
| value  | number/string/boolean  | Yes| Value specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1243
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.greaterThan("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### lessThan<sup>8+</sup> ###

lessThan(field: string, value: number|string): Query;

A
annie_wangli 已提交
1263
Creates a **Query** object to match the specified field whose value is less than the specified value.
A
annie_wangli 已提交
1264 1265 1266 1267 1268

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1269 1270
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
| value  | number/string/boolean  | Yes| Value specified.|
A
annie_wangli 已提交
1271 1272 1273 1274 1275

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1276
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.lessThan("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### greaterThanOrEqualTo<sup>8+</sup> ###

greaterThanOrEqualTo(field: string, value: number|string): Query;

A
annie_wangli 已提交
1296
Creates a **Query** object to match the specified field whose value is greater than or equal to the specified value.
A
annie_wangli 已提交
1297 1298 1299 1300 1301

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1302
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1303 1304 1305 1306 1307 1308
| value  | number/string/boolean  | Yes| Value specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1309
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.greaterThanOrEqualTo("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### lessThanOrEqualTo<sup>8+</sup> ###

lessThanOrEqualTo(field: string, value: number|string): Query;

A
annie_wangli 已提交
1329
Creates a **Query** object to match the specified field whose value is less than or equal to the specified value.
A
annie_wangli 已提交
1330 1331 1332 1333 1334

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1335 1336
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
| value  | number/string/boolean  | Yes| Value specified.|
A
annie_wangli 已提交
1337 1338 1339 1340 1341

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1342
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.lessThanOrEqualTo("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### isNull<sup>8+</sup> ###

isNull(field: string): Query;

Creates a **Query** object to match the specified field whose value is **null**.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1368
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1369 1370 1371 1372 1373

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1374
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.isNull("field");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### inNumber<sup>8+</sup> ###

inNumber(field: string, valueList: number[]): Query;

Creates a **Query** object to match the specified field whose value is within the specified list of numbers.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1400
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1401 1402 1403 1404 1405 1406
| valueList  | number[]  | Yes| List of numbers.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1407
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433


-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.inNumber("field", [0, 1]);
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### inString<sup>8+</sup> ###

inString(field: string, valueList: string[]): Query;

Creates a **Query** object to match the specified field whose value is within the specified list of strings.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1434
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1435 1436 1437 1438 1439 1440
| valueList  | string[]  | Yes| List of strings.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1441
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.inString("field", ['test1', 'test2']);
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### notInNumber<sup>8+</sup> ###

notInNumber(field: string, valueList: number[]): Query;

Creates a **Query** object to match the specified field whose value is not within the specified list of numbers.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1467
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1468 1469 1470 1471 1472 1473
| valueList  | number[]  | Yes| List of numbers.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1474
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notInNumber("field", [0, 1]);
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### notInString<sup>8+</sup> ###

notInString(field: string, valueList: string[]): Query;

Creates a **Query** object to match the specified field whose value is not within the specified list of strings.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1500
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1501 1502 1503 1504 1505 1506
| valueList  | string[]  | Yes| List of strings.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1507
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notInString("field", ['test1', 'test2']);
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### like<sup>8+</sup> ###

like(field: string, value: string): Query;

A
annie_wangli 已提交
1527
Creates a **Query** object to match the specified field whose value is similar to the specified string.
A
annie_wangli 已提交
1528 1529 1530 1531 1532

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1533
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1534 1535 1536 1537 1538 1539
| valueList  | string  | Yes| String specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1540
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.like("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### unlike<sup>8+</sup> ###

unlike(field: string, value: string): Query;

A
annie_wangli 已提交
1560
Creates a **Query** object to match the specified field whose value is not similar to the specified string.
A
annie_wangli 已提交
1561 1562 1563 1564 1565

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1566
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1567 1568 1569 1570 1571 1572
| valueList  | string  | Yes| String specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1573
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.unlike("field", "value");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### and<sup>8+</sup> ###

and(): Query;

Creates a **Query** object with the AND condition.

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1599
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value1");
        query.and();
        query.notEqualTo("field", "value2");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### or<sup>8+</sup> ###

or(): Query;

Creates a **Query** object with the OR condition.

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1627
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value1");
        query.or();
        query.notEqualTo("field", "value2");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### orderByAsc<sup>8+</sup> ###

orderByAsc(field: string): Query;

Creates a **Query** object to sort the query results in ascending order.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1655
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1656 1657 1658 1659 1660

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1661
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value");
        query.orderByAsc("field");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### orderByDesc<sup>8+</sup> ###

orderByDesc(field: string): Query;

Creates a **Query** object to sort the query results in descending order.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1688
| fieId  | string  | Yes|Field to match. It must start with $ and cannot contain ^.|
A
annie_wangli 已提交
1689 1690 1691 1692 1693

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1694
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721


-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value");
        query.orderByDesc("field");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### limit<sup>8+</sup> ###

limit(total: number, offset: number): Query;

Creates a **Query** object to specify the number of results and where to start.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1722 1723
| total  | number  | Yes|Number of results to query.|
| offset | number  | Yes|Start position for query.|
A
annie_wangli 已提交
1724 1725 1726 1727 1728

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1729
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.notEqualTo("field", "value");
        query.limit("total", "offset");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### isNotNull<sup>8+</sup> ###

isNotNull(field: string): Query;

Creates a **Query** object with a specified field that is not null.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| fieId  | string  | Yes|Field specified.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1762
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.isNotNull("field");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### beginGroup<sup>8+</sup> ###

beginGroup(): Query;

A
annie_wangli 已提交
1782
Creates a **Query** object for a query condition group with a left parenthesis.
A
annie_wangli 已提交
1783 1784 1785 1786 1787

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1788
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.beginGroup();
        query.isNotNull("field");
        query.endGroup();
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### endGroup<sup>8+</sup> ###

endGroup(): Query;

A
annie_wangli 已提交
1810
Creates a **Query** object for a query condition group with a right parenthesis.
A
annie_wangli 已提交
1811 1812 1813 1814 1815

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1816
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.beginGroup();
        query.isNotNull("field");
        query.endGroup();
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### prefixKey<sup>8+</sup> ###

prefixKey(prefix: string): Query;

Creates a **Query** object with a specified key prefix.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| prefix | string  | Yes|Key prefix.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1850
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.prefixKey("$.name");
        query.prefixKey("0");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
        console.log("dumplicated calls should be ok :" + e);
    }
    ```


### setSuggestIndex<sup>8+</sup> ###

setSuggestIndex(index: string): Query;

Creates a **Query** object with an index preferentially used for query.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1877
| index  | string  | Yes|Index preferentially used for query.|
A
annie_wangli 已提交
1878 1879 1880 1881 1882

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1883
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.setSuggestIndex("$.name");
        query.setSuggestIndex("0");
        console.log("query is " + query.getSqlLike());
        query = null;
    } catch (e) {
       console.log("dumplicated calls should be ok :" + e);
    }
    ```


### deviceId<sup>8+</sup> ###

deviceId(deviceId:string):Query;

Creates a **Query** object with the device ID as the key prefix.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| deviceId | string  | Yes|Device ID.|


-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1917
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941

-   Example

    ```
    try {
        let query = new distributedData.Query();
        query.deviceId("deviceId");
        console.log("query is " + query.getSqlLike());
    } catch (e) {
        console.log("should be ok on Method Chaining : " + e);
    }
    ```


### getSqlLike<sup>8+</sup> ###

getSqlLike():string;

Obtains the query statement of this **Query** object.

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
1942
| [Query](#query8) |**Query** object.|
A
annie_wangli 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958

-   Example

    ```
    try {
        let query = new distributedData.Query();
        let sql1 = query.getSqlLike();
        console.log("GetSqlLike sql=" + sql1);
    } catch (e) {
        console.log("dumplicated calls should be ok : " + e);
    }
    ```


## KVStore

A
annie_wangli 已提交
1959
Provides methods to manage data in a KV store, for example, adding or deleting data and subscribing to data changes or completion of data synchronization. Before calling any method in **KVStore**, you must use **getKVStore** to obtain a **KVStore** object.
A
annie_wangli 已提交
1960 1961 1962 1963 1964

### put

put(key: string, value: Uint8Array | string | number | boolean, callback: AsyncCallback&lt;void&gt;): void

A
annie_wangli 已提交
1965
Adds a KV pair of the specified type to this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    | string  | Yes|Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|
| value  | Uint8Array / string / number / boolean  | Yes|Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX\_VALUE\_LENGTH](#constants).|


-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
            if (err != undefined) {
                console.log("put err: " + JSON.stringify(err));
                return;
            }
            console.log("put success");
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


### put

put(key: string, value: Uint8Array | string | number | boolean): Promise&lt;void&gt;

A
annie_wangli 已提交
1999
Adds a KV pair of the specified type to this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    | string  | Yes|Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|
| value  | Uint8Array / string / number / boolean  | Yes|Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX\_VALUE\_LENGTH](#constants).|

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|


-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
            console.log("put success: " + JSON.stringify(data));
        }).catch((err) => {
            console.log("put err: " + JSON.stringify(err));
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


### delete

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

A
annie_wangli 已提交
2037
Deletes a KV pair from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    | string  | Yes|Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|
| callback  | AsyncCallback&lt;void&gt;  | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
            if (err != undefined) {
                console.log("put err: " + JSON.stringify(err));
                return;
            }
            console.log("put success");
            kvStore.delete(KEY_TEST_STRING_ELEMENT, function (err,data) {
                if (err != undefined) {
                    console.log("delete err: " + JSON.stringify(err));
                    return;
                }
                console.log("delete success");
            });
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


### delete

delete(key: string): Promise&lt;void&gt;

A
annie_wangli 已提交
2077
Deletes a KV pair from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    | string  | Yes|Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
            console.log("put success: " + JSON.stringify(data));
            kvStore.delete(KEY_TEST_STRING_ELEMENT).then((data) => {
                console.log("delete success");
            }).catch((err) => {
                console.log("delete err: " + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log("put err: " + JSON.stringify(err));
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


### on

on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;ChangeNotification&gt;): void

Subscribes to data changes of the specified type. This method uses a synchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2124
| event  |'dataChange'  | Yes|Type of the events to subscribe to.|
Z
zengyawen 已提交
2125 2126
| type  |[SubscribeType](#subscribetype) | Yes|Type of data changes.|
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes|Callback used to return the result.|
A
annie_wangli 已提交
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142


-   Example

    ```
    let kvStore;
    kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
        console.log("dataChange callback call data: " + JSON.stringify(data));
    });
    ```


### on

on(event: 'syncComplete', syncCallback: Callback<Array&lt;[string, number]&gt;): void

A
annie_wangli 已提交
2143
Subscribes to data synchronization completion events. This method uses a synchronous callback to return the result.
A
annie_wangli 已提交
2144 2145 2146 2147 2148

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2149
| event  |'syncComplete' | Yes|Type of the events to subscribe to.|
A
annie_wangli 已提交
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt; | Yes|Callback used to return the result.|


-   Example

    ```
    let kvStore;
    kvStore.on('syncComplete', function (data) {
        console.log("syncComplete callback call data: " + data);
    });
    ```

### off<sup>8+</sup>

off(event:'dataChange', observer?: Callback&lt;ChangeNotification&gt;): void;

A
annie_wangli 已提交
2166
Unsubscribes from data change events. This method uses a synchronouscallback to return the result.
A
annie_wangli 已提交
2167 2168 2169 2170 2171

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2172
| event  |'dataChange'  | Yes|Type of the events to unsubscribe from.|
Z
zengyawen 已提交
2173
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; |No|Callback used to return the result.|
A
annie_wangli 已提交
2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

-   Example

    ```
    let kvStore;
    kvStore.on('dataChange', function (data) {
        console.log("syncComplete callback call data: " + data);
    });
    kvStore.off('dataChange', function (data) {
        console.log("syncComplete callback call data: " + data);
    });
    ```


### putBatch<sup>8+</sup>

putBatch(entries: Entry[], callback: AsyncCallback&lt;void&gt;): void;

A
annie_wangli 已提交
2192
Inserts KV pairs in batches to this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| entries  |[Entry](#entry)[] | Yes|KV pairs to insert in batches.|
| callback |Asyncallback&lt;void&gt; |Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            await kvStore.getEntries('batch_test_string_key', function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            });
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    
    ```


### putBatch<sup>8+</sup>

putBatch(entries: Entry[]): Promise&lt;void&gt;;

A
annie_wangli 已提交
2238
Inserts KV pairs in batches to this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| entries  |[Entry](#entry)[] | Yes|KV pairs to insert in batches.|

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            await kvStore.getEntries('batch_test_string_key').then((entrys) => {
                console.log('getEntries success');
                console.log('PutBatch ' + JSON.stringify(entries));
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    ```


### deleteBatch<sup>8+</sup>

deleteBatch(keys: string[], callback: AsyncCallback&lt;void&gt;): void;

A
annie_wangli 已提交
2291
Deletes KV pairs in batches from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keys  |string[] | Yes|KV pairs to delete in batches.|
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        let keys = [];
        for (var i = 0; i < 5; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
            keys.push(key + i);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            await kvStore.deleteBatch(keys, async function (err,data) {
                console.log('deleteBatch success');
            });
        });
    }catch(e) {
        console.log('DeleteBatch e ' + e);
    }
    ```


### deleteBatch<sup>8+</sup> ###

deleteBatch(keys: string[]): Promise&lt;void&gt;;

A
annie_wangli 已提交
2336
Deletes KV pairs in batches from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keys   |string[] | Yes|KV pairs to delete in batches.|

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        let keys = [];
        for (var i = 0; i < 5; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
            keys.push(key + i);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            await kvStore.deleteBatch(keys).then((err) => {
                console.log('deleteBatch success');
            }).catch((err) => {
                console.log('deleteBatch fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('DeleteBatch e ' + e);
    }
    ```


### startTransaction<sup>8+</sup> ###

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

A
annie_wangli 已提交
2390
Starts the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    function putBatchString(len, prefix) {
        let entries = [];
        for (var i = 0; i < len; i++) {
            var entry = {
                key : prefix + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        return entries;
    }
    try {
        var count = 0;
        kvStore.on('dataChange', 0, function (data) {
            console.log('startTransaction 0' + data)
            count++;
        });
        kvStore.startTransaction(async function (err,data) {
            console.log('startTransaction success');
            let entries = putBatchString(10, 'batch_test_string_key');
            console.log('entries: ' + JSON.stringify(entries));
            await kvStore.putBatch(entries, async function (err,data) {
                console.log('putBatch success');
            });
        });
    }catch(e) {
        console.log('startTransaction e ' + e);
    }
    ```


### startTransaction<sup>8+</sup> ###

startTransaction(): Promise&lt;void&gt;;

A
annie_wangli 已提交
2440
Starts the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        var count = 0;
        kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) {
            console.log('startTransaction ' + JSON.stringify(data));
            count++;
        });
        kvStore.startTransaction().then(async (err) => {
            console.log('startTransaction success');
        }).catch((err) => {
            console.log('startTransaction fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('startTransaction e ' + e);
    }
    ```


### commit<sup>8+</sup> ###

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

A
annie_wangli 已提交
2473
Commits the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.commit(function (err,data) {
            if (err == undefined) {
                console.log('commit success');
            } else {
                console.log('commit fail');
            }
        });
    }catch(e) {
        console.log('Commit e ' + e);
    }
    ```


### commit<sup>8+</sup> ###

commit(): Promise&lt;void&gt;;

A
annie_wangli 已提交
2503
Commits the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.commit().then(async (err) => {
            console.log('commit success');
        }).catch((err) => {
            console.log('commit fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('Commit e ' + e);
    }
    ```


### rollback<sup>8+</sup> ###

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

A
annie_wangli 已提交
2531
Rolls back the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.rollback(function (err,data) {
            if (err == undefined) {
                console.log('commit success');
            } else {
                console.log('commit fail');
            }
        });
    }catch(e) {
        console.log('Rollback e ' + e);
    }
    ```


### rollback<sup>8+</sup> ###

rollback(): Promise&lt;void&gt;;

A
annie_wangli 已提交
2561
Rolls back the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.rollback().then(async (err) => {
            console.log('rollback success');
        }).catch((err) => {
            console.log('rollback fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('Rollback e ' + e);
    }
    ```


### enableSync<sup>8+</sup> ###

enableSync(enabled: boolean, callback: AsyncCallback&lt;void&gt;): void;

Sets data synchronization, which can be enabled or disable. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2595
| enabled  |boolean | Yes|Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite.|
A
annie_wangli 已提交
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.enableSync(true, function (err,data) {
            if (err == undefined) {
                console.log('enableSync success');
            } else {
                console.log('enableSync fail');
            }
        });
    }catch(e) {
        console.log('EnableSync e ' + e);
    }
    ```


### enableSync<sup>8+</sup> ###

enableSync(enabled: boolean): Promise&lt;void&gt;;

Sets data synchronization, which can be enabled or disable. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2626
| enabled  |boolean | Yes|Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite.|
A
annie_wangli 已提交
2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659

-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        kvStore.enableSync(true).then((err) => {
            console.log('enableSync success');
        }).catch((err) => {
            console.log('enableSync fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('EnableSync e ' + e);
    }
    ```


### setSyncRange<sup>8+</sup> ###

setSyncRange(localLabels: string[], remoteSupportLabels: string[], callback: AsyncCallback&lt;void&gt;): void;

Sets the data synchronization range. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2660
| localLabels  |string[] | Yes|Synchronization labels set for the local device.|
A
annie_wangli 已提交
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
| remoteSupportLabels  |string[] | Yes|Synchronization labels set for remote devices.|
| callback  |AsyncCallback&lt;void&gt; | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        const localLabels = ['A', 'B'];
        const remoteSupportLabels = ['C', 'D'];
        kvStore.setSyncRange(localLabels, remoteSupportLabels, function (err,data) {
            console.log('SetSyncRange put success');
        });
    }catch(e) {
        console.log('SetSyncRange e ' + e);
    }
    ```


### setSyncRange<sup>8+</sup> ###

setSyncRange(localLabels: string[], remoteSupportLabels: string[]): Promise&lt;void&gt;;

Sets the data synchronization range. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2690
| localLabels  |string[] | Yes|Synchronization labels set for the local device.|
A
annie_wangli 已提交
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724
| remoteSupportLabels  |string[] | Yes|Synchronization labels set for remote devices.|


-   Return value

| Type| Description|
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        const localLabels = ['A', 'B'];
        const remoteSupportLabels = ['C', 'D'];
        kvStore.setSyncRange(localLabels, remoteSupportLabels).then((err) => {
            console.log('setSyncRange success');
        }).catch((err) => {
            console.log('delete fail ' + err);
        });
    }catch(e) {
        console.log('SetSyncRange e ' + e);
    }
    ```


## SubscribeType

Defines the subscription type.

| Name| Default Value| Description|
| -----  | ------   | ----------------------- |
| SUBSCRIBE_TYPE_LOCAL  |0 |Local data changes.|
A
annie_wangli 已提交
2725 2726
| SUBSCRIBE_TYPE_REMOTE |1 |Remote data changes.|
| SUBSCRIBE_TYPE_ALL  |2 |Local and remote data changes.|
A
annie_wangli 已提交
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740

## ChangeNotification

Defines the content of data change notifications, including inserted data, updated data, deleted data, and device ID.

| Name| Type|Readable|Writable| Description|
| ----- | -------   | -----| ------|------------------------ |
| insertEntries | [Entry](#entry)[]   | Yes|  Yes|Data inserted.|
| updateEntries | [Entry](#entry)[]   | Yes|  Yes|Data updated.|
| deleteEntries | [Entry](#entry)[]   | Yes|  Yes|Data deleted.|
| deviceId | string   | Yes|  Yes|UUID of the device.|

## Entry

A
annie_wangli 已提交
2741
Defines the KV pairs stored in the KV store.
A
annie_wangli 已提交
2742 2743 2744

| Name| Type|Readable|Writable| Description|
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
2745 2746
| key | string   | Yes|  Yes|Key of the KV pair stored in the KV store.|
| value | [Value](#value) | Yes|  Yes|Value of the KV pair stored in the KV store.|
A
annie_wangli 已提交
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759


## Value

Defines the value in a KV pair.

| Name| Type|Readable|Writable| Description|
| ----- | -------   | -----| ------|------------------------ |
| type | [ValueType](#value)   | Yes|  Yes|Type of the value.|
| value | Uint8Array / string / number / boolean| Yes|  Yes|Specific value.|

## ValueType

A
annie_wangli 已提交
2760
Enumerates the types of values in KV pairs.
A
annie_wangli 已提交
2761

A
annie_wangli 已提交
2762
These value types can be used only by internal applications.
A
annie_wangli 已提交
2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774

| Name| Default Value| Description|
| -----  | ------   | ----------------------- |
| STRING  |0 |String.|
| INTEGER |1 |Integer.|
| FLOAT   |2 |Float (single-precision floating point).|
| BYTE_ARRAY   |3 |Byte array.|
| BOOLEAN   |4 |Boolean.|
| DOUBLE   |5 |Double (double-precision floating point).|

## SingleKVStore

A
annie_wangli 已提交
2775
Provides methods to query and synchronize data in a single KV store. This class inherits from **KVStore**. Before calling any method in **SingleKVStore**, you must use **getKVStore** to obtain a **SingleKVStore** object.
A
annie_wangli 已提交
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787

### get

get(key: string, callback: AsyncCallback&lt;Uint8Array | string | boolean | number&gt;): void

Obtains the value of a specified key. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    |string   | Yes|Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|
A
annie_wangli 已提交
2788
| callback  |AsyncCallback&lt;Uint8Array / string / boolean / number&gt;) | Yes|Callback used to return the value obtained.|
A
annie_wangli 已提交
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) {
            if (err != undefined) {
                console.log("put err: " + JSON.stringify(err));
                return;
            }
            console.log("put success");
            kvStore.get(KEY_TEST_STRING_ELEMENT, function (err,data) {
                console.log("get success data: " + data);
            });
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```


### get

get(key: string): Promise&lt;Uint8Array | string | boolean | number&gt;

Obtains the value of a specified key. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| key    |string   | Yes|Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX\_KEY\_LENGTH](#constants).|


-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
2830
|Promise&lt;Uint8Array / string / boolean / number&gt; |Promise used to return the value obtained.|
A
annie_wangli 已提交
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858


-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-test-string';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((data) => {
            console.log("put success: " + JSON.stringify(data));
            kvStore.get(KEY_TEST_STRING_ELEMENT).then((data) => {
                console.log("get success data: " + data);
            }).catch((err) => {
                console.log("get err: " + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log("put err: " + JSON.stringify(err));
        });
    }catch (e) {
        console.log("An unexpected error occurred. Error:" + e);
    }
    ```

### getEntries<sup>8+</sup> ###

getEntries(keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void;

A
annie_wangli 已提交
2859
Obtains the KV pairs that match the specified key prefix. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keyPrefix    |string   | Yes|Key prefix to match.|
| callback    |AsyncCallback&lt;Entry[]&gt;   | Yes|Callback used to return the KV pairs obtained.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_number_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.INTEGER,
                    value : 222
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            await kvStore.getEntries('batch_test_number_key', function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            });
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(keyPrefix: string): Promise&lt;Entry[]&gt;;

A
annie_wangli 已提交
2903
Obtains the KV pairs that match the specified key prefix. This method uses a promise to return the result.
A
annie_wangli 已提交
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keyPrefix    |string   | Yes|Key prefix to match.|

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
2915
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + entries);
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            await kvStore.getEntries('batch_test_string_key').then((entrys) => {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
                console.log('entrys[0].value: ' + JSON.stringify(entrys[0].value));
                console.log('entrys[0].value.value: ' + entrys[0].value.value);
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void;

A
annie_wangli 已提交
2959
Obtains the KV pairs that match the specified **Query** object. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2960 2961 2962 2963 2964

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
Z
zengyawen 已提交
2965
| query  |[Query](#query8)   | Yes|**Query** object to match.|
A
annie_wangli 已提交
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007
| callback  |AsyncCallback&lt;Entry[]&gt;   | Yes|Callback used to return the KV pairs obtained.|

-   Example

    ```
    let kvStore;
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getEntries(query, function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            });
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(query: Query): Promise&lt;Entry[]&gt;;

A
annie_wangli 已提交
3008
Obtains the KV pairs that match the specified **Query** object. This method uses a promise to return the result.
A
annie_wangli 已提交
3009 3010 3011 3012 3013

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
Z
zengyawen 已提交
3014
| query  |[Query](#query8)   | Yes|**Query** object to match.|
A
annie_wangli 已提交
3015 3016 3017 3018 3019

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3020
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062

-   Example

    ```
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getEntries(query).then((entrys) => {
                console.log('getEntries success');
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('GetEntries putBatch fail ' + JSON.stringify(err))
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void;

A
annie_wangli 已提交
3063
Obtains the result set with the specified key prefix from this single KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3064 3065 3066 3067 3068 3069

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keyPrefix  |string   | Yes|Key prefix to match.|
Z
zengyawen 已提交
3070
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes|Callback used to return the result set obtained.|
A
annie_wangli 已提交
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('GetResultSet putBatch success');
            await kvStore.getResultSet('batch_test_string_key', async function (err, result) {
                console.log('GetResultSet getResultSet success');
                resultSet = result;
                kvStore.closeResultSet(resultSet, function (err, data) {
                    console.log('GetResultSet closeResultSet success');
                })
            });
        });
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(keyPrefix: string): Promise&lt;KvStoreResultSet&gt;;

A
annie_wangli 已提交
3110
Obtains the result set with the specified key prefix from this single KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------  | ----  | ----------------------- |
| keyPrefix  |string   | Yes|Key prefix to match.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
3122
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the result set obtained.|
A
annie_wangli 已提交
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('PutBatch putBatch fail ' + JSON.stringify(err));
        });
        kvStore.getResultSet('batch_test_string_key').then((result) => {
            console.log('GetResult getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
        kvStore.closeResultSet(resultSet).then((err) => {
            console.log('GetResult closeResultSet success');
        }).catch((err) => {
            console.log('closeResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResult e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3174
| query  |Query    | Yes|**Query** object to match.|
Z
zengyawen 已提交
3175
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes|Callback used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getResultSet(query, async function (err, result) {
                console.log('getResultSet success');
                resultSet = result;
            });
        });
    } catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3220
| query  |[Query](#query8)    | Yes|**Query** object to match.|
A
annie_wangli 已提交
3221 3222 3223 3224 3225

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
3226
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        kvStore.getResultSet(query).then((result) => {
            console.log(' getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```

### closeResultSet<sup>8+</sup> ###

closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void;

Closes the **KvStoreResultSet** object obtained by **getResultSet**. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3274
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes|**KvStoreResultSet** object to close.|
A
annie_wangli 已提交
3275
| callback  |AsyncCallback&lt;void&gt;   | Yes|Callback used to return the execution result.|
A
annie_wangli 已提交
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305

-   Example

    ```
    let kvStore;
    try {
        let resultSet = null;
        kvStore.closeResultSet(resultSet, function (err, data) {
            if (err == undefined) {
                console.log('closeResultSet success');
            } else {
                console.log('closeResultSet fail');
            }
        });
    }catch(e) {
        console.log('CloseResultSet e ' + e);
    }
    ```


### closeResultSet<sup>8+</sup> ###

closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;;

Closes the **KvStoreResultSet** object obtained by **getResultSet**. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3306
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes|**KvStoreResultSet** object to close.|
A
annie_wangli 已提交
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340

-   Return value

| Type| Description|
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        let resultSet = null;
        kvStore.closeResultSet(resultSet).then(() => {
            console.log('closeResultSet success');
        }).catch((err) => {
            console.log('closeResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('CloseResultSet e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void;

Obtains the number of results matching the specified **Query** object. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3341
| query  |[Query](#query8)   | Yes|**Query** object to match.|
A
annie_wangli 已提交
3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384
| callback  |AsyncCallback&lt;number&gt;   | Yes|Callback used to return the number of results obtained.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getResultSize(query, async function (err, resultSize) {
                console.log('getResultSet success');
            });
        });
    } catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(query: Query): Promise&lt;number&gt;;

Obtains the number of results matching the specified **Query** object. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3385
| query  |[Query](#query8)   | Yes|**Query** object to match.|
A
annie_wangli 已提交
3386 3387 3388 3389 3390

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3391
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        kvStore.getResultSize(query).then((resultSize) => {
            console.log('getResultSet success');
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### removeDeviceData<sup>8+</sup> ###

removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void;

Deletes data of a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3438
| deviceId  |string   | Yes|ID of the target device.|
A
annie_wangli 已提交
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
| callback  |AsyncCallback&lt;void&gt;   | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
            console.log('put success');
            const deviceid = 'no_exist_device_id';
            await kvStore.removeDeviceData(deviceid, async function (err,data) {
                if (err == undefined) {
                    console.log('removeDeviceData success');
                } else {
                    console.log('removeDeviceData fail');
                    await kvStore.get(KEY_TEST_STRING_ELEMENT, async function (err,data) {
                        console.log('RemoveDeviceData get success');
                    });
                }
            });
        });
    }catch(e) {
        console.log('RemoveDeviceData e ' + e);
    }
    ```


### removeDeviceData<sup>8+</sup> ###

removeDeviceData(deviceId: string): Promise&lt;void&gt;;

Deletes data of a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3478
| deviceId  |string   | Yes|ID of the target device.|
A
annie_wangli 已提交
3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588

-   Return value

| Type| Description|
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((err) => {
            console.log('removeDeviceData put success');
        }).catch((err) => {
            console.log('put fail ' + JSON.stringify(err));
        });
        const deviceid = 'no_exist_device_id';
        kvStore.removeDeviceData(deviceid).then((err) => {
            console.log('removeDeviceData success');
        }).catch((err) => {
            console.log('removeDeviceData fail ' + JSON.stringify(err));
        });
        kvStore.get(KEY_TEST_STRING_ELEMENT).then((data) => {
            console.log('get success data:' + data);
        }).catch((err) => {
            console.log('RemoveDeviceData get fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('RemoveDeviceData e ' + e);
    }
    ```


### on<sup>8+</sup> ###

on(event: 'syncComplete', syncCallback: Callback<Array&lt;[string, number]&gt;): void;

Subscribes to the synchronization completion events. This method uses a synchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| event  |'syncComplete'   | Yes|Event triggered when the synchronization is complete.|
| syncCallback  |Callback<Array&lt;[string, number]&gt;   | Yes|Callback used to return the synchronization result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
    const VALUE_TEST_FLOAT_ELEMENT = 321.12;
    try {
        kvStore.on('syncComplete', function (data) {
            console.log('syncComplete ' + data)
        });
        kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then((data) => {
            console.log('syncComplete put success');
        }).catch((error) => {
            console.log('syncComplete put fail ' + error);
        });
    }catch(e) {
        console.log('syncComplete put e ' + e);
    }
    ```


### off<sup>8+</sup> ###

off(event: 'syncComplete', syncCallback?: Callback<Array&lt;[string, number]&gt;): void;

Unsubscribes from the synchronization completion events. This method uses a synchronization callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| event  |'syncComplete'   | Yes|Event triggered when the synchronization is complete.|
| syncCallback  |Callback<Array&lt;[string, number]&gt;   | No|Callback used to return the synchronization result.|


-   Example

    ```
    let kvStore;
    try {
        const func = function (data) {
            console.log('syncComplete ' + data)
        };
        kvStore.on('syncComplete', func);
        kvStore.off('syncComplete', func);
    }catch(e) {
        console.log('syncComplete e ' + e);
    }
    ```


### sync

sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void

Manually triggers KV store synchronization synchronously.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3589
| deviceIdList  |string[]  | Yes|IDs of the devices to be synchronized. These devices must be in the same networking environment.|
A
annie_wangli 已提交
3590
| mode  |[SyncMode](#syncmode)   | Yes|Data synchronization mode.|
A
annie_wangli 已提交
3591
| allowedDelayMs  |number   | No|Allowed synchronization delay time, in ms.|
A
annie_wangli 已提交
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668

-   Example

    ```
    let kvStore;
    kvStore.sync('deviceIds', distributedData.SyncMode.PULL_ONLY, 1000);
    ```

### setSyncParam<sup>8+</sup> ###

setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback&lt;void&gt;): void;

Sets the default delay of database synchronization. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| defaultAllowedDelayMs  |number  | Yes|Default delay allowed for database synchronization, in ms.|
| callback  |AsyncCallback&lt;void&gt;  | Yes|Callback used to return the result.|


-   Example

    ```
    let kvStore;
    try {
        const defaultAllowedDelayMs = 500;
        kvStore.setSyncParam(defaultAllowedDelayMs, function (err,data) {
            console.log('SetSyncParam put success');
        });
    }catch(e) {
        console.log('testSingleKvStoreSetSyncParam e ' + e);
    }
    ```


### setSyncParam<sup>8+</sup> ###

setSyncParam(defaultAllowedDelayMs: number): Promise&lt;void&gt;;

Sets the default delay of database synchronization. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| defaultAllowedDelayMs  |number  | Yes|Default delay allowed for database synchronization, in ms.|


-   Return value

| Type| Description|
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        const defaultAllowedDelayMs = 500;
        kvStore.setSyncParam(defaultAllowedDelayMs).then((err) => {
            console.log('SetSyncParam put success');
        }).catch((err) => {
            console.log('SetSyncParam put fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('SetSyncParam e ' + e);
    }
    ```


### getSecurityLevel<sup>8+</sup> ###

getSecurityLevel(callback: AsyncCallback&lt;SecurityLevel&gt;): void;

A
annie_wangli 已提交
3669
Obtains the security level of this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3670 3671 3672 3673 3674

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3675
| callback  |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt;  | Yes|Callback used to return the security level obtained.|
A
annie_wangli 已提交
3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694

-   Example

    ```
    let kvStore;
    try {
        kvStore.getSecurityLevel(function (err,data) {
            console.log('getSecurityLevel success');
        });
    }catch(e) {
        console.log('GetSecurityLeve e ' + e);
    }
    ```


### getSecurityLevel<sup>8+</sup> ###

getSecurityLevel(): Promise&lt;SecurityLevel&gt;;

A
annie_wangli 已提交
3695
Obtains the security level of this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
3696 3697 3698 3699 3700

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3701
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the security level obtained.|
A
annie_wangli 已提交
3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721


-   Example

    ```
    let kvStore;
    try {
        kvStore.getSecurityLevel().then((data) => {
            console.log(' getSecurityLevel success');
        }).catch((err) => {
            console.log('getSecurityLevel fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetSecurityLeve e ' + e);
    }
    ```


## DeviceKVStore<sup>8+</sup> ##

A
annie_wangli 已提交
3722
Provides methods to manage distributed data by device in the distributed system. This class inherits from **KvStore** and provides data query and synchronization methods. Before calling any method in **DeviceKVStore**, you must use **getKVStore** to obtain a **DeviceKVStore** object.
A
annie_wangli 已提交
3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774

### get<sup>8+</sup> ###

get(deviceId: string, key: string, callback: AsyncCallback&lt;boolean|string|number|Uint8Array&gt;): void;

Obtains the string value that matches the specified key for a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| key       |string  | Yes|Key to match.|
| callback  |AsyncCallback&lt;boolean/string/number/Uint8Array&gt;  | Yes|Callback used to return the value obtained.|


-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
    try{
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
            console.log('put success');
            kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, function (err,data) {
                console.log('get success');
            });
        })
    }catch(e) {
        console.log('get e' + e);
    }
    ```


### get<sup>8+</sup> ###

get(deviceId: string, key: string): Promise&lt;boolean|string|number|Uint8Array&gt;;

Obtains the string value that matches the specified key for a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| key       |string  | Yes|Key to match.|

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3775
|Promise&lt;boolean/string/number/Uint8Array&gt; |Promise used to return the value obtained.|
A
annie_wangli 已提交
3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string_2';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-002';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then(async (data) => {
            console.log(' put success');
            kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data) => {
                console.log('get success');
            }).catch((err) => {
                console.log('get fail ' + JSON.stringify(err));
            });
        }).catch((error) => {
            console.log('put error' + error);
        });
    } catch (e) {
        console.log('Get e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void;

A
annie_wangli 已提交
3804
Obtains the KV pairs that match the specified key prefix for a device. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| keyPrefix |string  | Yes|Key prefix to match.|
| callback  |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes|Callback used to return the KV pairs obtained.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + entries);
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            await kvStore.getEntries('localDeviceId', 'batch_test_string_key', function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            });
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(deviceId: string, keyPrefix: string): Promise&lt;Entry[]&gt;;

A
annie_wangli 已提交
3850
Obtains the KV pairs that match the specified key prefix for a device. This method uses a promise to return the result.
A
annie_wangli 已提交
3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| keyPrefix |string  | Yes|Key prefix to match.|

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3863
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + entries);
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            await kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entrys) => {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
                console.log('entrys[0].value: ' + JSON.stringify(entrys[0].value));
                console.log('entrys[0].value.value: ' + entrys[0].value.value);
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('PutBatch e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void;

Obtains the KV pairs matching the specified **Query** object. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3913
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes|Callback used to return the KV pairs obtained.|

-   Example

    ```
    let kvStore;
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            expect(err == undefined).assertTrue();
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            query.deviceId('localDeviceId');
            await kvStore.getEntries(query, function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            });
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(query: Query): Promise&lt;Entry[]&gt;;

Obtains the KV pairs matching the specified **Query** object. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
3964
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
3965 3966 3967 3968 3969

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
3970
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020

-   Example

    ```
    let kvStore;
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getEntries(query).then((entrys) => {
                console.log('getEntries success');
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('GetEntries putBatch fail ' + JSON.stringify(err))
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(deviceId: string, query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void;

Obtains the KV pairs matching the specified **Query** object for a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
Z
zengyawen 已提交
4021
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes|Callback used to return the KV pairs obtained.|

-   Example

    ```
    let kvStore;
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries, async function (err,data) {
            console.log('putBatch success');
            expect(err == undefined).assertTrue();
            var query = new distributedData.Query();
            query.deviceId('localDeviceId');
            query.prefixKey("batch_test");
            await kvStore.getEntries('localDeviceId', query, function (err,entrys) {
                console.log('getEntries success');
                console.log('entrys.length: ' + entrys.length);
                console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
            })
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getEntries<sup>8+</sup> ###

getEntries(deviceId: string, query: Query): Promise&lt;Entry[]&gt;;

Obtains the KV pairs matching the specified **Query** object for a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
Z
zengyawen 已提交
4073
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
4074 4075 4076 4077 4078

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
4079
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131

-   Example

    ```
    let kvStore;
    try {
        var arr = new Uint8Array([21,31]);
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_bool_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.BYTE_ARRAY,
                    value : arr
                }
            }
            entries.push(entry);
        }
        console.log('entries: ' + JSON.stringify(entries));
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
            var query = new distributedData.Query();
            query.deviceId('localDeviceId');
            query.prefixKey("batch_test");
            await kvStore.getEntries('localDeviceId', query).then((entrys) => {
                console.log('getEntries success');
            }).catch((err) => {
                console.log('getEntries fail ' + JSON.stringify(err));
            });
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
        console.log('GetEntries success');
    }catch(e) {
        console.log('GetEntries e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void;

Obtains the **KvStoreResultSet** object that matches the specified key prefix for a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| keyPrefix |string  | Yes|Key prefix to match.|
Z
zengyawen 已提交
4132
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes|Callback used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async function (err, result) {
            console.log('getResultSet success');
            resultSet = result;
            await kvStore.closeResultSet(resultSet, function (err, data) {
                console.log('closeResultSet success');
            })
        });
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(deviceId: string, keyPrefix: string): Promise&lt;KvStoreResultSet&gt;;

Obtains the **KvStoreResultSet** object that matches the specified key prefix for a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
| keyPrefix |string  | Yes|Key prefix to match.|

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
4170
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        kvStore.getResultSet('localDeviceId', 'batch_test_string_key').then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
        kvStore.closeResultSet(resultSet).then((err) => {
            console.log('closeResultSet success');
        }).catch((err) => {
            console.log('closeResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4205 4206
| query  |[Query](#query8)  | Yes|**Query** object to match.|
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes|Callback used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            query.deviceId('localDeviceId');
            await kvStore.getResultSet(query, async function (err, result) {
                console.log('getResultSet success');
                resultSet = result;
                await kvStore.closeResultSet(resultSet, function (err, data) {
                    console.log('closeResultSet success');
                })
            });
        });
    } catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4255
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
4256 4257 4258 4259 4260

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
4261
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('putBatch fail ' + err);
        });
        const query = new distributedData.Query();
        query.deviceId('localDeviceId');
        query.prefixKey("batch_test");
        console.log("GetResultSet " + query.getSqlLike());
        kvStore.getResultSet(query).then((result) => {
            console.log('getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
        kvStore.closeResultSet(resultSet).then((err) => {
            console.log('closeResultSet success');
        }).catch((err) => {
            console.log('closeResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(deviceId: string, query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object for a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
Z
zengyawen 已提交
4318 4319
| query  |[Query](#query8)  | Yes|**Query** object to match.|
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes|Callback used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getResultSet('localDeviceId', query, async function (err, result) {
                console.log('getResultSet success');
                resultSet = result;
                await kvStore.closeResultSet(resultSet, function (err, data) {
                    console.log('closeResultSet success');
                })
            });
        });
    } catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### getResultSet<sup>8+</sup> ###

getResultSet(deviceId: string, query: Query): Promise&lt;KvStoreResultSet&gt;;

Obtains the **KvStoreResultSet** object that matches the specified **Query** object for a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|
Z
zengyawen 已提交
4368
| query  |[Query](#query8)  | Yes|**Query** object to match.|
A
annie_wangli 已提交
4369 4370 4371 4372 4373

-   Return value

| Type| Description|
| ------  | -------   |
Z
zengyawen 已提交
4374
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430

-   Example

    ```
    let kvStore;
    try {
        let resultSet;
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('GetResultSet putBatch success');
        }).catch((err) => {
            console.log('PutBatch putBatch fail ' + JSON.stringify(err));
        });
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        kvStore.getResultSet('localDeviceId', query).then((result) => {
            console.log('GetResultSet getResultSet success');
            resultSet = result;
        }).catch((err) => {
            console.log('GetResultSet getResultSet fail ' + JSON.stringify(err));
        });
        query.deviceId('localDeviceId');
        console.log("GetResultSet " + query.getSqlLike());
        kvStore.closeResultSet(resultSet).then((err) => {
            console.log('GetResultSet closeResultSet success');
        }).catch((err) => {
            console.log('GetResultSet closeResultSet fail ' + JSON.stringify(err));
        });
    
    }catch(e) {
        console.log('GetResultSet e ' + e);
    }
    ```


### closeResultSet<sup>8+</sup> ###

closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void;

Closes the **KvStoreResultSet** object obtained by **getResultSet**. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4431
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes|**KvStoreResultSet** object to close.|
A
annie_wangli 已提交
4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463
| callback   |AsyncCallback&lt;void&gt;                 | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        console.log('CloseResultSet success');
        let resultSet = null;
        kvStore.closeResultSet(resultSet, function (err, data) {
            if (err == undefined) {
                console.log('closeResultSet success');
            } else {
                console.log('closeResultSet fail');
            }
        });
    }catch(e) {
        console.log('CloseResultSet e ' + e);
    }
    ```


### closeResultSet<sup>8+</sup> ###

closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;;

Closes the **KvStoreResultSet** object obtained by **getResultSet**. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4464
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes|**KvStoreResultSet** object to close.|
A
annie_wangli 已提交
4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499

-   Return value

| Type| Description|
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    try {
        console.log('CloseResultSet success');
        let resultSet = null;
        kvStore.closeResultSet(resultSet).then(() => {
            console.log('closeResultSet success');
        }).catch((err) => {
            console.log('closeResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('CloseResultSet e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void;

Obtains the number of results that matches the specified **Query** object. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4500
| query     |[Query](#query8)       | Yes|**Query** object to match.|
A
annie_wangli 已提交
4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544
| callback  |AsyncCallback&lt;number&gt;  | Yes|Callback used to return the number of results obtained.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            query.deviceId('localDeviceId');
            await kvStore.getResultSize(query, async function (err, resultSize) {
                console.log('getResultSet success');
            });
        });
    } catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(query: Query): Promise&lt;number&gt;;

Obtains the number of results that matches the specified **Query** object. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
Z
zengyawen 已提交
4545
| query     |[Query](#query8)       | Yes|**Query** object to match.|
A
annie_wangli 已提交
4546 4547 4548 4549 4550

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
4551
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
        const query = new distributedData.Query();
        query.prefixKey("batch_test");
        query.deviceId('localDeviceId');
        kvStore.getResultSize(query).then((resultSize) => {
            console.log('getResultSet success');
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(deviceId: string, query: Query, callback: AsyncCallback&lt;number&gt;): void;

Obtains the number of results that matches the specified **Query** object for a device. This method uses an asynchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string                       | Yes|ID of the target device.|
Z
zengyawen 已提交
4600
| query     |[Query](#query8)       | Yes|**Query** object to match.|
A
annie_wangli 已提交
4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644
| callback  |AsyncCallback&lt;number&gt;  | Yes|Callback used to return the number of results obtained.|

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries, async function (err, data) {
            console.log('putBatch success');
            const query = new distributedData.Query();
            query.prefixKey("batch_test");
            await kvStore.getResultSize('localDeviceId', query, async function (err, resultSize) {
                console.log('getResultSet success');
            });
        });
    } catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### getResultSize<sup>8+</sup> ###

getResultSize(deviceId: string, query: Query): Promise&lt;number&gt;;

Obtains the number of results that matches the specified **Query** object for a device. This method uses a promise to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string                       | Yes|ID of the target device.|
Z
zengyawen 已提交
4645
| query     |[Query](#query8)       | Yes|**Query** object to match.|
A
annie_wangli 已提交
4646 4647 4648 4649 4650

-   Return value

| Type| Description|
| ------  | -------   |
A
annie_wangli 已提交
4651
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691

-   Example

    ```
    let kvStore;
    try {
        let entries = [];
        for (var i = 0; i < 10; i++) {
            var key = 'batch_test_string_key';
            var entry = {
                key : key + i,
                value : {
                    type : distributedData.ValueType.STRING,
                    value : 'batch_test_string_value'
                }
            }
            entries.push(entry);
        }
        kvStore.putBatch(entries).then(async (err) => {
            console.log('putBatch success');
        }).catch((err) => {
            console.log('putBatch fail ' + JSON.stringify(err));
        });
        var query = new distributedData.Query();
        query.prefixKey("batch_test");
        kvStore.getResultSize('localDeviceId', query).then((resultSize) => {
            console.log('getResultSet success');
        }).catch((err) => {
            console.log('getResultSet fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('GetResultSize e ' + e);
    }
    ```


### removeDeviceData<sup>8+</sup> ###

removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void;

A
annie_wangli 已提交
4692
Removes data of a device from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string                       | Yes|ID of the target device.|
| callback  |AsyncCallback&lt;void&gt;    | Yes|Callback used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, async function (err,data) {
            console.log('RemoveDeviceData  put success');
            const deviceid = 'no_exist_device_id';
            await kvStore.removeDeviceData(deviceid, async function (err,data) {
                if (err == undefined) {
                    console.log('removeDeviceData success');
                } else {
                    console.log('removeDeviceData fail');
                    await kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, async function (err,data) {
                        console.log('RemoveDeviceData get success');
                    });
                }
            });
        });
    }catch(e) {
        console.log('RemoveDeviceData e ' + e);
    }
    ```


### removeDeviceData<sup>8+</sup> ###

removeDeviceData(deviceId: string): Promise&lt;void&gt;;

A
annie_wangli 已提交
4732
Removes data of a device from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceId  |string  | Yes|ID of the target device.|

-   Return value

| Type| Description|
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

-   Example

    ```
    let kvStore;
    const KEY_TEST_STRING_ELEMENT = 'key_test_string';
    const VALUE_TEST_STRING_ELEMENT = 'value-string-001';
    try {
        kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT).then((err) => {
            console.log('RemoveDeviceData put success');
        }).catch((err) => {
            console.log('RemoveDeviceData put fail ' + JSON.stringify(err));
        });
        const deviceid = 'no_exist_device_id';
        kvStore.removeDeviceData(deviceid).then((err) => {
            console.log('removeDeviceData success');
        }).catch((err) => {
            console.log('removeDeviceData fail ' + JSON.stringify(err));
        });
        kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT).then((data) => {
            console.log('RemoveDeviceData get success data:' + data);
        }).catch((err) => {
            console.log('RemoveDeviceData get fail ' + JSON.stringify(err));
        });
    }catch(e) {
        console.log('RemoveDeviceData e ' + e);
    }
    ```


### sync<sup>8+</sup> ###

sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void;

Manually triggers KV store synchronization synchronously.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| deviceIdList    |string[]               | Yes|IDs of the devices to be synchronized.|
A
annie_wangli 已提交
4786 4787
| mode            |[SyncMode](#syncmode)  | Yes|Data synchronization mode, which can be **PUSH**, **PULL**, or **PUSH_PULL**.|
| allowedDelayMs  |number                 | No|Allowed synchronization delay time, in ms.|
A
annie_wangli 已提交
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875


-   Example

    ```
    let kvStore;
    const KEY_TEST_SYNC_ELEMENT = 'key_test_sync';
    const VALUE_TEST_SYNC_ELEMENT = 'value-string-001';
    try {
        kvStore.on('syncComplete', function (data) {
            console.log('Sync dataChange');
        });
        kvStore.put(KEY_TEST_SYNC_ELEMENT + 'testSync101', VALUE_TEST_SYNC_ELEMENT, function (err,data) {
            console.log('Sync put success');
            const devices = ['deviceList'];
            const mode = distributedData.SyncMode.PULL_ONLY;
            kvStore.sync(devices, mode);
        });
    }catch(e) {
        console.log('Sync e' + e);
    }
    ```

### on<sup>8+</sup> ###

on(event: 'syncComplete', syncCallback: Callback<Arrary&lt;<[string, number]&gt;): void;

Subscribes to the synchronization completion events. This method uses a synchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| event    |'syncComplete'      | Yes|Event triggered when the synchronization is complete.|
| syncCallback            |Callback<Arrary&lt;<[string, number]&gt; | Yes|Callback used to return the synchronization result.|

-   Example
    ```
    const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
    const VALUE_TEST_FLOAT_ELEMENT = 321.12;
    try {
        kvStore.on('syncComplete', function (data) {
            console.log('syncComplete ' + data)
        });
        kvStore.put(KEY_TEST_FLOAT_ELEMENT, VALUE_TEST_FLOAT_ELEMENT).then((data) => {
            console.log('syncComplete put success');
        }).catch((error) => {
            console.log('syncComplete put fail ' + error);
        });
    }catch(e) {
        console.log('syncComplete put e ' + e);
    }
    ```


### off<sup>8+</sup> ###

off(event: 'syncComplete', syncCallback?: Callback<Array&lt;[string, number]&gt;): void;

Unsubscribes from the synchronization completion events. This method uses a synchronous callback to return the result.

-   Parameters

| Name| Type| Mandatory| Description|
| -----  | ------   | ----  | ----------------------- |
| event         |'syncComplete'                           | Yes|Event triggered when the synchronization is complete.|
| syncCallback  |Callback<Arrary&lt;<[string, number]&gt; | No|Callback used to return the synchronization result.|


-   Example

    ```
    let kvStore;
    try {
        const func = function (data) {
            console.log('syncComplete ' + data)
        };
        kvStore.on('syncComplete', func);
        kvStore.off('syncComplete', func);
    }catch(e) {
        console.log('syncComplete e ' + e);
    }
    ```


## SyncMode

Defines the synchronization mode.
Z
zengyawen 已提交
4876

A
annie_wangli 已提交
4877 4878 4879 4880 4881
| Name| Default Value| Description|
| -----      | ------    | ----------------------- |
| PULL_ONLY  |0          |Pull data from the peer end to the local end only.|
| PUSH_ONLY  |1          |Push data from the local end to the peer end only.|
| PUSH_PULL  |2          |Push data from the local end to the peer end and then pull data from the peer end to the local end.|