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

A
annie_wangli 已提交
3
>![](../../public_sys-resources/icon-note.gif) **NOTE**<br/>
Z
zengyawen 已提交
4 5
>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 已提交
6 7

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

```
import distributedData from '@ohos.data.distributedData';
```


A
annie_wangli 已提交
14
## distributedData.createKVManager
Z
zengyawen 已提交
15

A
annie_wangli 已提交
16 17 18 19
createKVManager(config: KVManagerConfig, callback: AsyncCallback&lt;KVManager&gt;): void

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

A
annie_wangli 已提交
20 21 22
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
A
annie_wangli 已提交
23 24 25

| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
annie_wangli 已提交
26 27
| config | [KVManagerConfig](#kvmanagerconfig) | Yes | Configuration of the **KVManager** object, including the bundle name and user information of the caller.|
| callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | Yes | Callback invoked to return the **KVManager** object created.|
Z
zengyawen 已提交
28

A
annie_wangli 已提交
29
**Example**
A
annie_wangli 已提交
30 31 32 33 34 35 36 37
```
let kvManager;
try {
    const kvManagerConfig = {
        bundleName : 'com.example.datamanagertest',
        userInfo : {
            userId : '0',
            userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
38 39
        }
    }
A
annie_wangli 已提交
40 41 42 43 44 45 46 47 48 49 50 51
    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);
}
```
Z
zengyawen 已提交
52

A
annie_wangli 已提交
53
## distributedData.createKVManager
Z
zengyawen 已提交
54

A
annie_wangli 已提交
55
createKVManager(config: KVManagerConfig): Promise&lt;KVManager&gt;
Z
zengyawen 已提交
56

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

A
annie_wangli 已提交
59 60 61
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
A
annie_wangli 已提交
62

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

A
annie_wangli 已提交
67
**Return value**
Z
zengyawen 已提交
68

A
annie_wangli 已提交
69 70 71
| Type| Description|
| -------- | -------- |
| Promise&lt;[KVManager](#kvmanager)&gt; | Promise used to return the **KVManager** object created.|
A
annie_wangli 已提交
72

A
annie_wangli 已提交
73
**Example**
Z
zengyawen 已提交
74

A
annie_wangli 已提交
75 76 77 78 79 80 81 82
```
let kvManager;
try {
    const kvManagerConfig = {
        bundleName : 'com.example.datamanagertest',
        userInfo : {
            userId : '0',
            userType : distributedData.UserType.SAME_USER_ID
Z
zengyawen 已提交
83 84
        }
    }
A
annie_wangli 已提交
85 86 87 88 89 90 91 92 93 94
    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);
}
```
Z
zengyawen 已提交
95

A
annie_wangli 已提交
96 97 98 99
## KVManagerConfig

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

A
annie_wangli 已提交
100 101
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
annie_wangli 已提交
102 103
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
annie_wangli 已提交
104 105
| userInfo | [UserInfo](#userinfo) | Yes | User information.|
| bundleName | string | Yes | Bundle name.|
A
annie_wangli 已提交
106 107

## UserInfo
Z
zengyawen 已提交
108 109 110

Defines user information.

A
annie_wangli 已提交
111 112
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

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


## UserType
Z
zengyawen 已提交
120 121 122

Defines the user type.

A
annie_wangli 已提交
123 124
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

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


## 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

A
annie_wangli 已提交
136
getKVStore&lt;T extends KVStore&gt;(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 141 142
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**
Z
zengyawen 已提交
143

A
annie_wangli 已提交
144 145
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
A
annie_wangli 已提交
146 147 148
| 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 已提交
149

A
annie_wangli 已提交
150
**Example**
Z
zengyawen 已提交
151

A
annie_wangli 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
```
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, 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);
}
```
Z
zengyawen 已提交
176 177


A
annie_wangli 已提交
178
### getKVStore
Z
zengyawen 已提交
179

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

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

A
annie_wangli 已提交
184
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
185

A
annie_wangli 已提交
186 187 188
**Parameters**

| Name  | Type               | Mandatory | Description   |
A
annie_wangli 已提交
189
| ------- | ---------------------- | ---- | -------------------- |
A
annie_wangli 已提交
190 191
| 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.|
A
annie_wangli 已提交
192 193


A
annie_wangli 已提交
194
**Return value**
A
annie_wangli 已提交
195

A
annie_wangli 已提交
196
| Type                                   | Description       |
A
annie_wangli 已提交
197 198 199
| -------------------------------------- | ------------------------ |
| Promise&lt;T&gt; &lt;T extends KVStore&gt; | Promise used to return the KV store created.|

A
annie_wangli 已提交
200
**Example**
A
annie_wangli 已提交
201

A
annie_wangli 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
```
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);
}
```
A
annie_wangli 已提交
224 225 226

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

A
annie_wangli 已提交
227
closeKVStore(appId: string, storeId: string, kvStore: KVStore, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
228 229 230

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

A
annie_wangli 已提交
231
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
232

A
annie_wangli 已提交
233
**Parameters**
A
annie_wangli 已提交
234

A
annie_wangli 已提交
235 236

| Name  | Type             | Mandatory| Description        |
A
annie_wangli 已提交
237
| ------- | -----------------   | ---- | --------------------------- |
A
annie_wangli 已提交
238 239 240 241
| appId    | string              | Yes  | Bundle name of the app that invokes the KV store.        |
| 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 已提交
242

A
annie_wangli 已提交
243
**Example**
Z
zengyawen 已提交
244

A
annie_wangli 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
```
let kvStore;
let kvManager;
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, async function (err, store) {
    console.log('getKVStore success');
    kvStore = store;
    await kvManager.closeKVStore('appId', 'storeId', kvStore, function (err, data) {
        console.log('closeKVStore success');
    });
    });
} catch (e) {
    console.log('closeKVStore e ' + e);
}
```

Z
zengyawen 已提交
270

A
annie_wangli 已提交
271
### closeKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
272

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

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

A
annie_wangli 已提交
277
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
278

A
annie_wangli 已提交
279 280 281
**Parameters**

| Name | Type| Mandatory | Description       |
A
annie_wangli 已提交
282
| -----  | ------  | ---- | ----------------------------- |
A
annie_wangli 已提交
283 284 285
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.           |
| 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 已提交
286

A
annie_wangli 已提交
287
**Return value**
Z
zengyawen 已提交
288

A
annie_wangli 已提交
289
| Type         | Description           |
A
annie_wangli 已提交
290 291
| ------------- | -------------- |
| 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 已提交
292

A
annie_wangli 已提交
293
**Example**
Z
zengyawen 已提交
294

A
annie_wangli 已提交
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
```
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);
}  
```
A
annie_wangli 已提交
323 324 325 326


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

A
annie_wangli 已提交
327
deleteKVStore(appId: string, storeId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
328 329 330

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

A
annie_wangli 已提交
331
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
332

A
annie_wangli 已提交
333 334 335
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
336
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
337 338 339
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.    |
| 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 已提交
340

A
annie_wangli 已提交
341
**Example**
Z
zengyawen 已提交
342

A
annie_wangli 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
```
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('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 已提交
361
        });
A
annie_wangli 已提交
362 363 364 365 366
    });
} catch (e) {
    console.log('DeleteKVStore e ' + e);
}
```
Z
zengyawen 已提交
367

A
annie_wangli 已提交
368
### deleteKVStore<sup>8+</sup> ###
Z
zengyawen 已提交
369

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

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

A
annie_wangli 已提交
374
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
375

A
annie_wangli 已提交
376 377 378
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
379
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
380 381
| appId  | string  | Yes  | Bundle name of the app that invokes the KV store.    |
| 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 已提交
382 383


A
annie_wangli 已提交
384
**Return value**
Z
zengyawen 已提交
385

A
annie_wangli 已提交
386
| Type         | Description           |
A
annie_wangli 已提交
387 388
| ------------- | -------------- |
| 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 已提交
389

A
annie_wangli 已提交
390
**Example**
Z
zengyawen 已提交
391

A
annie_wangli 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
```
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('storId', options).then(async (store) => {
        console.log('getKVStore success');
        kvStore = store;
        await kvManager.deleteKVStore('appId', 'storeId').then(() => {
            console.log('deleteKVStore success');
Z
zengyawen 已提交
410
        }).catch((err) => {
A
annie_wangli 已提交
411
            console.log('deleteKVStore err ' + JSON.stringify(err));
Z
zengyawen 已提交
412
        });
A
annie_wangli 已提交
413 414 415 416 417 418 419
    }).catch((err) => {
        console.log('getKVStore err ' + JSON.stringify(err));
    });
} catch (e) {
    console.log('deleteKVStore e ' + e);
}
```
Z
zengyawen 已提交
420 421


A
annie_wangli 已提交
422
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
423

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

A
annie_wangli 已提交
426
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 已提交
427

A
annie_wangli 已提交
428
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
429

A
annie_wangli 已提交
430 431 432
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
433
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
434 435
| appId  | string  | Yes   | Bundle name of the app that invokes the KV store.    |
| callback | AsyncCallback&lt;void&gt; | Yes  |Callback used to return the KV store IDs obtained. |
Z
zengyawen 已提交
436

A
annie_wangli 已提交
437
**Example**
Z
zengyawen 已提交
438

A
annie_wangli 已提交
439 440 441 442 443 444 445 446 447 448 449
```
let kvManager;
try {
    kvManager.getAllKVStoreId('appId', function (err, data) {
        console.log('GetAllKVStoreId success');
        console.log('GetAllKVStoreId size = ' + data.length);
    });
} catch (e) {
    console.log('GetAllKVStoreId e ' + e);
}
```
Z
zengyawen 已提交
450 451


A
annie_wangli 已提交
452
### getAllKVStoreId<sup>8+</sup> ###
Z
zengyawen 已提交
453

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

A
annie_wangli 已提交
456
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 已提交
457

A
annie_wangli 已提交
458
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
459

A
annie_wangli 已提交
460 461 462
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
463
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
464
| appId  | string  | Yes   | Bundle name of the app that invokes the KV store.    |
A
annie_wangli 已提交
465 466


A
annie_wangli 已提交
467
**Return value**
A
annie_wangli 已提交
468

A
annie_wangli 已提交
469
| Type         | Description           |
A
annie_wangli 已提交
470 471
| ------------- | -------------- |
| Promise&lt;string[]&gt;| Promise used to return the KV store IDs obtained.|
Z
zengyawen 已提交
472

A
annie_wangli 已提交
473
**Example**
Z
zengyawen 已提交
474

A
annie_wangli 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488
```
let kvManager;
try {
    console.log('GetAllKVStoreId');
    kvManager.getAllKVStoreId('apppId').then((data) => {
        console.log('getAllKVStoreId success');
        console.log('size = ' + data.length);
    }).catch((err) => {
        console.log('getAllKVStoreId err ' + JSON.stringify(err));
    });
} catch(e) {
    console.log('getAllKVStoreId e ' + e);
}
```
Z
zengyawen 已提交
489 490


A
annie_wangli 已提交
491
### on<sup>8+</sup> ###
Z
zengyawen 已提交
492

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

A
annie_wangli 已提交
495
Subscribes to the **distributedDataServiceDie** events. This method uses a synchronous callback to return the result.
Z
zengyawen 已提交
496

A
annie_wangli 已提交
497
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
498

A
annie_wangli 已提交
499 500 501
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
502
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
503 504
| event  | 'distributedDataServiceDie'  | Yes   | Type of events to subscribe to.     |
| deathCallback  | Callback&lt;void&gt;  | Yes   | Callback invoked when the distributed data service is dead.   |
A
annie_wangli 已提交
505

A
annie_wangli 已提交
506
**Example**
Z
zengyawen 已提交
507

A
annie_wangli 已提交
508 509 510 511 512 513 514
```
let kvManager;
try {
    
    console.log('KVManagerOn');
    const deathCallback = function () {
        console.log('death callback call');
A
annie_wangli 已提交
515
    }
A
annie_wangli 已提交
516 517 518 519 520
    kvManager.on('distributedDataServiceDie', deathCallback);
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
Z
zengyawen 已提交
521 522


A
annie_wangli 已提交
523
### off<sup>8+</sup> ###
Z
zengyawen 已提交
524

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

A
annie_wangli 已提交
527
Unsubscribes from the **distributedDataServiceDie** events. This method uses a synchronous callback to return the result.
Z
zengyawen 已提交
528

A
annie_wangli 已提交
529
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
530

A
annie_wangli 已提交
531 532 533
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
534
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
535 536
| 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 已提交
537

Z
zengyawen 已提交
538

A
annie_wangli 已提交
539
**Example**
Z
zengyawen 已提交
540

A
annie_wangli 已提交
541 542 543 544 545 546
```
let kvManager;
try {
    console.log('KVManagerOff');
    const deathCallback = function () {
        console.log('death callback call');
A
annie_wangli 已提交
547
    }
A
annie_wangli 已提交
548 549 550 551
    kvManager.off('distributedDataServiceDie', deathCallback);
} catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
A
annie_wangli 已提交
552
    
A
annie_wangli 已提交
553
```
Z
zengyawen 已提交
554

A
annie_wangli 已提交
555 556 557
## Options

Provides KV store configuration.
Z
zengyawen 已提交
558

A
annie_wangli 已提交
559 560 561
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
562
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
563 564 565 566
| 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.     |
| autoSync  | boolean | No|Whether to automatically synchronize database files. By default, database files are not automatically synchronized.    |
A
annie_wangli 已提交
567
| 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.|
A
annie_wangli 已提交
568
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set. |
A
annie_wangli 已提交
569
| schema<sup>8+</sup> | [Schema](#schema8) | No| Schema used to define the values stored in a KV store.|
Z
zengyawen 已提交
570 571


A
annie_wangli 已提交
572
## KVStoreType
Z
zengyawen 已提交
573

A
annie_wangli 已提交
574
Defines the KV store types.
Z
zengyawen 已提交
575

A
annie_wangli 已提交
576 577 578
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Default Value| Description                   |
A
annie_wangli 已提交
579
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
580 581 582
| 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 已提交
583 584


A
annie_wangli 已提交
585
## SecurityLevel
Z
zengyawen 已提交
586

A
annie_wangli 已提交
587 588
Defines the KV store security levels.

A
annie_wangli 已提交
589 590 591
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Default Value| Description                   |
A
annie_wangli 已提交
592
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
593 594 595 596 597 598
| 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. |
A
annie_wangli 已提交
599 600 601 602 603 604


## Constants

Defines the KV store constants.

A
annie_wangli 已提交
605 606 607
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Default Value| Description                   |
A
annie_wangli 已提交
608
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
609 610 611 612 613 614
| 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. |
| MAX_STORE_ID_LENGTH  | 128 | Maximum length (in bytes) of a KV store ID. |
| MAX_QUERY_LENGTH  | 512000 | Maximum query length. |
| MAX_BATCH_SIZE  | 128 | Maximum size of a batch operation. |
A
annie_wangli 已提交
615 616 617

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

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

A
annie_wangli 已提交
620
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
621

A
annie_wangli 已提交
622 623
| Name | Type| Description                   |
| ---   | ----  | ----------------------- |
A
annie_wangli 已提交
624 625
| root<sup>8+</sup>  | [FieldNode](#fieldnode8) | JSON root object. |
| indexes<sup>8+</sup>  | Array\<string> | String array in JSON format. |
A
annie_wangli 已提交
626 627
| mode<sup>8+</sup>  | number | Schema mode. |
| skip<sup>8+</sup>  | number |  Size of a skip of the schema. |
A
annie_wangli 已提交
628

A
annie_wangli 已提交
629
### constructor<sup>8+</sup> ###
A
annie_wangli 已提交
630

A
annie_wangli 已提交
631
constructor()
A
annie_wangli 已提交
632

A
annie_wangli 已提交
633
A constructor used to create a **Schema** instance.
Z
zengyawen 已提交
634

A
annie_wangli 已提交
635
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
636

A
annie_wangli 已提交
637
## FieldNode<sup>8+</sup> ##
Z
zengyawen 已提交
638

A
annie_wangli 已提交
639
Represents a **Schema** instance, which provides the methods for defining the values stored in a KV store.
Z
zengyawen 已提交
640

A
annie_wangli 已提交
641 642 643 644 645 646 647 648 649 650 651
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore

| Name | Type| Description                   |
| ---   | ----  | ----------------------- |
| nullable<sup>8+</sup>  | boolean | Whether the database field can be null.  |
| default<sup>8+</sup>  | string | Default value of a **FieldNode**.|
| type<sup>8+</sup>  | number | Value to store.|

### constructor<sup>8+</sup> ###

constructor(name: string)
Z
zengyawen 已提交
652

A
annie_wangli 已提交
653 654 655 656 657 658
A constructor used to create a **FieldNode** instance with a string field.

**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore

**Parameters**

A
annie_wangli 已提交
659 660 661
| Name| Type| Mandatory| Description           |
| ------ | -------- | ---- | --------------- |
| name   | string   | Yes  | Value of **FieldNode**.|
Z
zengyawen 已提交
662

A
annie_wangli 已提交
663 664
### appendChild<sup>8+</sup> ###

A
annie_wangli 已提交
665
appendChild(child: FieldNode): boolean
A
annie_wangli 已提交
666

A
annie_wangli 已提交
667
Appends a child node to this **FieldNode**.
Z
zengyawen 已提交
668

A
annie_wangli 已提交
669
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
Z
zengyawen 已提交
670

A
annie_wangli 已提交
671 672 673
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
674
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
675
| child  | [FieldNode](#fieldnode8) | Yes   | Child node to append.  |
A
annie_wangli 已提交
676

A
annie_wangli 已提交
677

A
annie_wangli 已提交
678
**Return value**
Z
zengyawen 已提交
679

A
annie_wangli 已提交
680
| Type         | Description           |
A
annie_wangli 已提交
681 682
| ------------- | -------------- |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.|
Z
zengyawen 已提交
683

A
annie_wangli 已提交
684
**Example**
Z
zengyawen 已提交
685

A
annie_wangli 已提交
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
```
import ddm from '@ohos.data.distributedData';
try {
    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);
}
```
A
annie_wangli 已提交
705 706 707 708


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

A
annie_wangli 已提交
709
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 已提交
710

A
annie_wangli 已提交
711

A
annie_wangli 已提交
712 713
### getCount<sup>8+</sup> ###

A
annie_wangli 已提交
714
getCount(): number
A
annie_wangli 已提交
715 716 717

Obtains the number of rows in the result set.

A
annie_wangli 已提交
718
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
719

A
annie_wangli 已提交
720 721 722
**Return value**

| Type  | Description              |
A
annie_wangli 已提交
723
| ------ | --------------    |
A
annie_wangli 已提交
724
| number |Number of rows obtained.         |
A
annie_wangli 已提交
725

A
annie_wangli 已提交
726
**Example**
A
annie_wangli 已提交
727

A
annie_wangli 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
```
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 count = resultSet.getCount();
    console.log("GetCount " + count);
} catch (e) {
    console.log("GetCount fail " + e);
}
```
Z
zengyawen 已提交
744

A
annie_wangli 已提交
745
### getPosition<sup>8+</sup> ###
Z
zengyawen 已提交
746

A
annie_wangli 已提交
747
getPosition(): number
Z
zengyawen 已提交
748

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

A
annie_wangli 已提交
751
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
Z
zengyawen 已提交
752

A
annie_wangli 已提交
753 754 755
**Return value**

| Type  | Description              |
A
annie_wangli 已提交
756
| ------ | --------------    |
A
annie_wangli 已提交
757
| number |Current data read position obtained.  |
Z
zengyawen 已提交
758

A
annie_wangli 已提交
759
**Example**
Z
zengyawen 已提交
760

A
annie_wangli 已提交
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
```
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 position = resultSet.getPosition();
    console.log("getPosition " + position);
} catch (e) {
    console.log("GetPosition fail " + e);
}
```
Z
zengyawen 已提交
777 778


A
annie_wangli 已提交
779
### moveToFirst<sup>8+</sup> ###
Z
zengyawen 已提交
780

A
annie_wangli 已提交
781
moveToFirst(): boolean
Z
zengyawen 已提交
782

A
annie_wangli 已提交
783 784
Moves the data read position to the first row.

A
annie_wangli 已提交
785
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
786

A
annie_wangli 已提交
787 788 789
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
790
| ------  | --------------    |
A
annie_wangli 已提交
791
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
792

A
annie_wangli 已提交
793
**Example**
A
annie_wangli 已提交
794

A
annie_wangli 已提交
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
```
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);
}
```
A
annie_wangli 已提交
811 812 813 814


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

A
annie_wangli 已提交
815
moveToLast(): boolean
A
annie_wangli 已提交
816 817 818

Moves the data read position to the last row.

A
annie_wangli 已提交
819
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
820

A
annie_wangli 已提交
821 822 823
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
824
| ------  | --------------    |
A
annie_wangli 已提交
825
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
826

A
annie_wangli 已提交
827
**Example**
A
annie_wangli 已提交
828

A
annie_wangli 已提交
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
```
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);
}
```
A
annie_wangli 已提交
845 846 847 848


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

A
annie_wangli 已提交
849
moveToNext(): boolean
A
annie_wangli 已提交
850 851 852

Moves the data read position to the next row.

A
annie_wangli 已提交
853
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
854

A
annie_wangli 已提交
855 856 857
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
858
| ------  | --------------    |
A
annie_wangli 已提交
859
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
860

A
annie_wangli 已提交
861
**Example**
A
annie_wangli 已提交
862

A
annie_wangli 已提交
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
```
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);
}
```
A
annie_wangli 已提交
879 880 881 882


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

A
annie_wangli 已提交
883
moveToPrevious(): boolean
A
annie_wangli 已提交
884 885 886

Moves the data read position to the previous row.

A
annie_wangli 已提交
887
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
888

A
annie_wangli 已提交
889 890 891
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
892
| ------  | --------------    |
A
annie_wangli 已提交
893
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
894

A
annie_wangli 已提交
895
**Example**
A
annie_wangli 已提交
896

A
annie_wangli 已提交
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
```
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);
}
```
A
annie_wangli 已提交
913 914 915 916


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

A
annie_wangli 已提交
917
move(offset: number): boolean
A
annie_wangli 已提交
918 919 920

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

A
annie_wangli 已提交
921
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
922

A
annie_wangli 已提交
923 924 925
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
926
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
927
| 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.  |
A
annie_wangli 已提交
928

A
annie_wangli 已提交
929
**Return value**
A
annie_wangli 已提交
930

A
annie_wangli 已提交
931
| Type   | Description              |
A
annie_wangli 已提交
932
| ------  | --------------    |
A
annie_wangli 已提交
933
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
934

A
annie_wangli 已提交
935
**Example**
A
annie_wangli 已提交
936

A
annie_wangli 已提交
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
```
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);
}
```
A
annie_wangli 已提交
953 954 955 956


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

A
annie_wangli 已提交
957
moveToPosition(position: number): boolean
A
annie_wangli 已提交
958 959 960

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

A
annie_wangli 已提交
961
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
962

A
annie_wangli 已提交
963 964 965
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
966
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
967
| position  | number  | Yes   |Absolute position to move to.         |
A
annie_wangli 已提交
968

A
annie_wangli 已提交
969
**Return value**
A
annie_wangli 已提交
970

A
annie_wangli 已提交
971
| Type   | Description              |
A
annie_wangli 已提交
972
| ------  | --------------    |
A
annie_wangli 已提交
973
| boolean |Returns **true** if the operation is successful; returns **false** otherwise.  |
A
annie_wangli 已提交
974

A
annie_wangli 已提交
975
**Example**
A
annie_wangli 已提交
976

A
annie_wangli 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
```
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);
}
```
A
annie_wangli 已提交
993 994 995 996


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

A
annie_wangli 已提交
997
isFirst(): boolean
A
annie_wangli 已提交
998 999 1000

Checks whether the data read position is the first row.

A
annie_wangli 已提交
1001
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1002

A
annie_wangli 已提交
1003 1004 1005
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1006
| ------  | --------------    |
A
annie_wangli 已提交
1007
| boolean |Returns **true** if the data read position is the first row; returns **false** otherwise.  |
A
annie_wangli 已提交
1008

A
annie_wangli 已提交
1009
**Example**
A
annie_wangli 已提交
1010

A
annie_wangli 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
```
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);
}
```
A
annie_wangli 已提交
1027 1028 1029 1030


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

A
annie_wangli 已提交
1031
isLast(): boolean
A
annie_wangli 已提交
1032 1033 1034

Checks whether the data read position is the last row.

A
annie_wangli 已提交
1035
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1036

A
annie_wangli 已提交
1037 1038 1039
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1040
| ------  | --------------    |
A
annie_wangli 已提交
1041
| boolean |Returns **true** if the data read position is the last row; returns **false** otherwise.  |
A
annie_wangli 已提交
1042

A
annie_wangli 已提交
1043
**Example**
A
annie_wangli 已提交
1044

A
annie_wangli 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
```
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);
}
```
A
annie_wangli 已提交
1061 1062 1063

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

A
annie_wangli 已提交
1064
isBeforeFirst(): boolean
A
annie_wangli 已提交
1065 1066 1067

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

A
annie_wangli 已提交
1068
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1069

A
annie_wangli 已提交
1070 1071 1072
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1073
| ------  | --------------    |
A
annie_wangli 已提交
1074
| boolean |Returns **true** if the read position is before the first row; returns **false** otherwise. |
A
annie_wangli 已提交
1075

A
annie_wangli 已提交
1076
**Example**
A
annie_wangli 已提交
1077

A
annie_wangli 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
```
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);
}
```
A
annie_wangli 已提交
1094 1095 1096 1097


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

A
annie_wangli 已提交
1098
isAfterLast(): boolean
A
annie_wangli 已提交
1099 1100 1101

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

A
annie_wangli 已提交
1102
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1103

A
annie_wangli 已提交
1104 1105 1106
**Return value**

| Type   | Description              |
A
annie_wangli 已提交
1107
| ------  | --------------    |
A
annie_wangli 已提交
1108
| boolean |Returns **true** if the data read position is after the last row; returns **false** otherwise. |
A
annie_wangli 已提交
1109

A
annie_wangli 已提交
1110
**Example**
A
annie_wangli 已提交
1111

A
annie_wangli 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
```
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);
}
```
A
annie_wangli 已提交
1128 1129 1130 1131


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

A
annie_wangli 已提交
1132
getEntry(): Entry
A
annie_wangli 已提交
1133

A
annie_wangli 已提交
1134
Obtains a KV pair.
A
annie_wangli 已提交
1135

A
annie_wangli 已提交
1136
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1137

A
annie_wangli 已提交
1138 1139 1140
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1141
| ------  | -------   |
A
annie_wangli 已提交
1142
| Entry   |KV pair obtained.|
A
annie_wangli 已提交
1143

A
annie_wangli 已提交
1144
**Example**
A
annie_wangli 已提交
1145

A
annie_wangli 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
```
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);
}
```
A
annie_wangli 已提交
1163 1164 1165 1166 1167 1168


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

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

A
annie_wangli 已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

### constructor<sup>8+</sup> ###

constructor() 

A constructor used to create a **Schema** instance.

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


A
annie_wangli 已提交
1180 1181
### reset<sup>8+</sup> ###

A
annie_wangli 已提交
1182
reset(): Query
A
annie_wangli 已提交
1183 1184 1185

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

A
annie_wangli 已提交
1186
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1187

A
annie_wangli 已提交
1188 1189 1190 1191

**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1192
| ------  | -------   |
Z
zengyawen 已提交
1193
| [Query](#query8) |**Query** object reset.|
A
annie_wangli 已提交
1194

A
annie_wangli 已提交
1195
**Example**
A
annie_wangli 已提交
1196

A
annie_wangli 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
```
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);
}
```
A
annie_wangli 已提交
1209 1210 1211 1212 1213 1214


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

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

A
annie_wangli 已提交
1215
Creates a **Query** object to match the specified field whose value is equal to the specified value.
A
annie_wangli 已提交
1216

A
annie_wangli 已提交
1217
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1218

A
annie_wangli 已提交
1219 1220 1221
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1222
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1223 1224
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| value  | number/string/boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1225

A
annie_wangli 已提交
1226
**Return value**
A
annie_wangli 已提交
1227

A
annie_wangli 已提交
1228
| Type   | Description      |
A
annie_wangli 已提交
1229
| ------  | -------   |
Z
zengyawen 已提交
1230
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1231

A
annie_wangli 已提交
1232
**Example**
A
annie_wangli 已提交
1233

A
annie_wangli 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
```
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);
}
```
A
annie_wangli 已提交
1244 1245 1246 1247


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

A
annie_wangli 已提交
1248
notEqualTo(field: string, value: number|string|boolean): Query
A
annie_wangli 已提交
1249

A
annie_wangli 已提交
1250
Creates a **Query** object to match the specified field whose value is not equal to the specified value.
A
annie_wangli 已提交
1251

A
annie_wangli 已提交
1252
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1253

A
annie_wangli 已提交
1254 1255 1256
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1257
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1258 1259
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| value  | number/string/boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1260

A
annie_wangli 已提交
1261
**Return value**
A
annie_wangli 已提交
1262

A
annie_wangli 已提交
1263
| Type   | Description      |
A
annie_wangli 已提交
1264
| ------  | -------   |
Z
zengyawen 已提交
1265
| [Query](#query8) |**Query** object created.|
A
annie_wangli 已提交
1266

A
annie_wangli 已提交
1267
**Example**
A
annie_wangli 已提交
1268

A
annie_wangli 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
```
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);
}
```
A
annie_wangli 已提交
1279 1280 1281 1282


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

A
annie_wangli 已提交
1283
greaterThan(field: string, value: number|string|boolean): Query
A
annie_wangli 已提交
1284

A
annie_wangli 已提交
1285
Creates a **Query** object to match the specified field whose value is greater than the specified value.
A
annie_wangli 已提交
1286

A
annie_wangli 已提交
1287
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1288

A
annie_wangli 已提交
1289 1290 1291
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1292
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1293
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1294
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1295

A
annie_wangli 已提交
1296
**Return value**
A
annie_wangli 已提交
1297

A
annie_wangli 已提交
1298
| Type   | Description      |
A
annie_wangli 已提交
1299
| ------  | -------   |
A
annie_wangli 已提交
1300
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1301

A
annie_wangli 已提交
1302
**Example**
A
annie_wangli 已提交
1303

A
annie_wangli 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
```
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);
}
```
A
annie_wangli 已提交
1314 1315 1316 1317


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

A
annie_wangli 已提交
1318
lessThan(field: string, value: number|string): Query
A
annie_wangli 已提交
1319

A
annie_wangli 已提交
1320
Creates a **Query** object to match the specified field whose value is less than the specified value.
A
annie_wangli 已提交
1321

A
annie_wangli 已提交
1322
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1323

A
annie_wangli 已提交
1324 1325 1326
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1327
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1328
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1329
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1330

A
annie_wangli 已提交
1331
**Return value**
A
annie_wangli 已提交
1332

A
annie_wangli 已提交
1333
| Type   | Description      |
A
annie_wangli 已提交
1334
| ------  | -------   |
A
annie_wangli 已提交
1335
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1336

A
annie_wangli 已提交
1337
**Example**
A
annie_wangli 已提交
1338

A
annie_wangli 已提交
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
```
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);
}
```
A
annie_wangli 已提交
1349 1350 1351 1352


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

A
annie_wangli 已提交
1353
greaterThanOrEqualTo(field: string, value: number|string): Query
A
annie_wangli 已提交
1354

A
annie_wangli 已提交
1355
Creates a **Query** object to match the specified field whose value is greater than or equal to the specified value.
A
annie_wangli 已提交
1356

A
annie_wangli 已提交
1357
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1358

A
annie_wangli 已提交
1359 1360 1361
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1362
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1363
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1364
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1365

A
annie_wangli 已提交
1366
**Return value**
A
annie_wangli 已提交
1367

A
annie_wangli 已提交
1368
| Type   | Description      |
A
annie_wangli 已提交
1369
| ------  | -------   |
A
annie_wangli 已提交
1370
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1371

A
annie_wangli 已提交
1372
**Example**
A
annie_wangli 已提交
1373

A
annie_wangli 已提交
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
```
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);
}
```
A
annie_wangli 已提交
1384 1385 1386 1387


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

A
annie_wangli 已提交
1388
lessThanOrEqualTo(field: string, value: number|string): Query
A
annie_wangli 已提交
1389

A
annie_wangli 已提交
1390
Creates a **Query** object to match the specified field whose value is less than or equal to the specified value.
A
annie_wangli 已提交
1391

A
annie_wangli 已提交
1392
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1393

A
annie_wangli 已提交
1394 1395 1396
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1397
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1398
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1399
| value  | number\|string\|boolean  | Yes   | Value specified.|
A
annie_wangli 已提交
1400

A
annie_wangli 已提交
1401
**Return value**
A
annie_wangli 已提交
1402

A
annie_wangli 已提交
1403
| Type   | Description      |
A
annie_wangli 已提交
1404
| ------  | -------   |
A
annie_wangli 已提交
1405
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1406

A
annie_wangli 已提交
1407
**Example**
A
annie_wangli 已提交
1408

A
annie_wangli 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
```
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);
}
```
A
annie_wangli 已提交
1419 1420 1421 1422


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

A
annie_wangli 已提交
1423
isNull(field: string): Query
A
annie_wangli 已提交
1424 1425 1426 1427

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


A
annie_wangli 已提交
1428 1429 1430 1431 1432
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1433
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1434
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1435

A
annie_wangli 已提交
1436
**Return value**
A
annie_wangli 已提交
1437

A
annie_wangli 已提交
1438
| Type   | Description      |
A
annie_wangli 已提交
1439
| ------  | -------   |
A
annie_wangli 已提交
1440
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1441

A
annie_wangli 已提交
1442
**Example**
A
annie_wangli 已提交
1443

A
annie_wangli 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
```
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);
}
```
A
annie_wangli 已提交
1454 1455 1456 1457


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

A
annie_wangli 已提交
1458
inNumber(field: string, valueList: number[]): Query
A
annie_wangli 已提交
1459 1460 1461 1462

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


A
annie_wangli 已提交
1463 1464 1465 1466 1467
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

**Parameters**

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

A
annie_wangli 已提交
1472
**Return value**
A
annie_wangli 已提交
1473

A
annie_wangli 已提交
1474
| Type   | Description      |
A
annie_wangli 已提交
1475
| ------  | -------   |
A
annie_wangli 已提交
1476
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1477

A
annie_wangli 已提交
1478
**Example**
A
annie_wangli 已提交
1479

A
annie_wangli 已提交
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
```
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);
}
```
A
annie_wangli 已提交
1490 1491 1492 1493


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

A
annie_wangli 已提交
1494
inString(field: string, valueList: string[]): Query
A
annie_wangli 已提交
1495 1496 1497

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

A
annie_wangli 已提交
1498
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1499

A
annie_wangli 已提交
1500 1501 1502
**Parameters**

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

A
annie_wangli 已提交
1507
**Return value**
A
annie_wangli 已提交
1508

A
annie_wangli 已提交
1509
| Type   | Description      |
A
annie_wangli 已提交
1510
| ------  | -------   |
A
annie_wangli 已提交
1511
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1512

A
annie_wangli 已提交
1513
**Example**
A
annie_wangli 已提交
1514

A
annie_wangli 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
```
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);
}
```
A
annie_wangli 已提交
1525 1526 1527 1528


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

A
annie_wangli 已提交
1529
notInNumber(field: string, valueList: number[]): Query
A
annie_wangli 已提交
1530 1531 1532

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

A
annie_wangli 已提交
1533
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1534

A
annie_wangli 已提交
1535 1536 1537
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1538
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1539 1540
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| valueList  | number[]  | Yes   | List of numbers.|
A
annie_wangli 已提交
1541

A
annie_wangli 已提交
1542
**Return value**
A
annie_wangli 已提交
1543

A
annie_wangli 已提交
1544
| Type   | Description      |
A
annie_wangli 已提交
1545
| ------  | -------   |
A
annie_wangli 已提交
1546
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1547

A
annie_wangli 已提交
1548
**Example**
A
annie_wangli 已提交
1549

A
annie_wangli 已提交
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
```
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);
}
```
A
annie_wangli 已提交
1560 1561 1562 1563


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

A
annie_wangli 已提交
1564
notInString(field: string, valueList: string[]): Query
A
annie_wangli 已提交
1565 1566 1567

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

A
annie_wangli 已提交
1568
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1569

A
annie_wangli 已提交
1570 1571 1572
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1573
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1574 1575
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| valueList  | string[]  | Yes   | List of strings.|
A
annie_wangli 已提交
1576

A
annie_wangli 已提交
1577
**Return value**
A
annie_wangli 已提交
1578

A
annie_wangli 已提交
1579
| Type   | Description      |
A
annie_wangli 已提交
1580
| ------  | -------   |
A
annie_wangli 已提交
1581
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1582

A
annie_wangli 已提交
1583
**Example**
A
annie_wangli 已提交
1584

A
annie_wangli 已提交
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
```
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);
}
```
A
annie_wangli 已提交
1595 1596 1597 1598


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

A
annie_wangli 已提交
1599
like(field: string, value: string): Query
A
annie_wangli 已提交
1600

A
annie_wangli 已提交
1601
Creates a **Query** object to match the specified field whose value is similar to the specified string.
A
annie_wangli 已提交
1602

A
annie_wangli 已提交
1603
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1604

A
annie_wangli 已提交
1605 1606 1607
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1608
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1609 1610
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| valueList  | string  | Yes   | String specified.|
A
annie_wangli 已提交
1611

A
annie_wangli 已提交
1612
**Return value**
A
annie_wangli 已提交
1613

A
annie_wangli 已提交
1614
| Type   | Description      |
A
annie_wangli 已提交
1615
| ------  | -------   |
A
annie_wangli 已提交
1616
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1617

A
annie_wangli 已提交
1618
**Example**
A
annie_wangli 已提交
1619

A
annie_wangli 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
```
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);
}
```
A
annie_wangli 已提交
1630 1631 1632 1633


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

A
annie_wangli 已提交
1634
unlike(field: string, value: string): Query
A
annie_wangli 已提交
1635

A
annie_wangli 已提交
1636
Creates a **Query** object to match the specified field whose value is not similar to the specified string.
A
annie_wangli 已提交
1637

A
annie_wangli 已提交
1638
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1639

A
annie_wangli 已提交
1640 1641 1642
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1643
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1644 1645
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
| valueList  | string  | Yes   | String specified.|
A
annie_wangli 已提交
1646

A
annie_wangli 已提交
1647
**Return value**
A
annie_wangli 已提交
1648

A
annie_wangli 已提交
1649
| Type   | Description      |
A
annie_wangli 已提交
1650
| ------  | -------   |
A
annie_wangli 已提交
1651
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1652

A
annie_wangli 已提交
1653
**Example**
A
annie_wangli 已提交
1654

A
annie_wangli 已提交
1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
```
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);
}
```
A
annie_wangli 已提交
1665 1666 1667 1668


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

A
annie_wangli 已提交
1669
and(): Query
A
annie_wangli 已提交
1670 1671 1672

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

A
annie_wangli 已提交
1673
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1674

A
annie_wangli 已提交
1675 1676 1677
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1678
| ------  | -------   |
A
annie_wangli 已提交
1679
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1680

A
annie_wangli 已提交
1681
**Example**
A
annie_wangli 已提交
1682

A
annie_wangli 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
```
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);
}
```
A
annie_wangli 已提交
1695 1696 1697 1698


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

A
annie_wangli 已提交
1699
or(): Query
A
annie_wangli 已提交
1700 1701 1702

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

A
annie_wangli 已提交
1703
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1704

A
annie_wangli 已提交
1705 1706 1707
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1708
| ------  | -------   |
A
annie_wangli 已提交
1709
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1710

A
annie_wangli 已提交
1711
**Example**
A
annie_wangli 已提交
1712

A
annie_wangli 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
```
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);
}
```
A
annie_wangli 已提交
1725 1726 1727 1728


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

A
annie_wangli 已提交
1729
orderByAsc(field: string): Query
A
annie_wangli 已提交
1730 1731 1732

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

A
annie_wangli 已提交
1733
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1734

A
annie_wangli 已提交
1735 1736 1737
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1738
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1739
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1740

A
annie_wangli 已提交
1741
**Return value**
A
annie_wangli 已提交
1742

A
annie_wangli 已提交
1743
| Type   | Description      |
A
annie_wangli 已提交
1744
| ------  | -------   |
A
annie_wangli 已提交
1745
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1746

A
annie_wangli 已提交
1747
**Example**
A
annie_wangli 已提交
1748

A
annie_wangli 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
```
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);
}
```
A
annie_wangli 已提交
1760 1761 1762 1763


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

A
annie_wangli 已提交
1764
orderByDesc(field: string): Query
A
annie_wangli 已提交
1765 1766 1767

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

A
annie_wangli 已提交
1768
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1769

A
annie_wangli 已提交
1770 1771 1772
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1773
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1774
| fieId  | string  | Yes   |Field to match. It must start with $ and cannot contain ^. |
A
annie_wangli 已提交
1775

A
annie_wangli 已提交
1776
**Return value**
A
annie_wangli 已提交
1777

A
annie_wangli 已提交
1778
| Type   | Description      |
A
annie_wangli 已提交
1779
| ------  | -------   |
A
annie_wangli 已提交
1780
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1781

A
annie_wangli 已提交
1782
**Example**
A
annie_wangli 已提交
1783

A
annie_wangli 已提交
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794
```
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);
}
```
A
annie_wangli 已提交
1795 1796 1797 1798


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

A
annie_wangli 已提交
1799
limit(total: number, offset: number): Query
A
annie_wangli 已提交
1800 1801 1802

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

A
annie_wangli 已提交
1803
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1804

A
annie_wangli 已提交
1805 1806 1807
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1808
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1809 1810
| total  | number  | Yes   |Number of results to query. |
| offset | number  | Yes   |Start position for query. |
A
annie_wangli 已提交
1811

A
annie_wangli 已提交
1812
**Return value**
A
annie_wangli 已提交
1813

A
annie_wangli 已提交
1814
| Type   | Description      |
A
annie_wangli 已提交
1815
| ------  | -------   |
A
annie_wangli 已提交
1816
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1817

A
annie_wangli 已提交
1818
**Example**
A
annie_wangli 已提交
1819

A
annie_wangli 已提交
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
```
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);
}
```
A
annie_wangli 已提交
1831 1832 1833 1834


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

A
annie_wangli 已提交
1835
isNotNull(field: string): Query
A
annie_wangli 已提交
1836 1837 1838

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

A
annie_wangli 已提交
1839
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1840

A
annie_wangli 已提交
1841 1842 1843
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1844
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1845
| fieId  | string  | Yes   |Field specified.     |
A
annie_wangli 已提交
1846

A
annie_wangli 已提交
1847
**Return value**
A
annie_wangli 已提交
1848

A
annie_wangli 已提交
1849
| Type   | Description      |
A
annie_wangli 已提交
1850
| ------  | -------   |
A
annie_wangli 已提交
1851
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1852

A
annie_wangli 已提交
1853
**Example**
A
annie_wangli 已提交
1854

A
annie_wangli 已提交
1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
```
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);
}
```
A
annie_wangli 已提交
1865 1866 1867 1868


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

A
annie_wangli 已提交
1869
beginGroup(): Query
A
annie_wangli 已提交
1870

A
annie_wangli 已提交
1871
Creates a **Query** object for a query condition group with a left parenthesis.
A
annie_wangli 已提交
1872

A
annie_wangli 已提交
1873
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1874

A
annie_wangli 已提交
1875 1876 1877
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1878
| ------  | -------   |
A
annie_wangli 已提交
1879
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1880

A
annie_wangli 已提交
1881
**Example**
A
annie_wangli 已提交
1882

A
annie_wangli 已提交
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
```
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);
}
```
A
annie_wangli 已提交
1895 1896 1897 1898


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

A
annie_wangli 已提交
1899
endGroup(): Query
A
annie_wangli 已提交
1900

A
annie_wangli 已提交
1901
Creates a **Query** object for a query condition group with a right parenthesis.
A
annie_wangli 已提交
1902

A
annie_wangli 已提交
1903
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1904

A
annie_wangli 已提交
1905 1906 1907
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
1908
| ------  | -------   |
A
annie_wangli 已提交
1909
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1910

A
annie_wangli 已提交
1911
**Example**
A
annie_wangli 已提交
1912

A
annie_wangli 已提交
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
```
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);
}
```
A
annie_wangli 已提交
1925 1926 1927 1928


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

A
annie_wangli 已提交
1929
prefixKey(prefix: string): Query
A
annie_wangli 已提交
1930 1931 1932

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

A
annie_wangli 已提交
1933
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1934

A
annie_wangli 已提交
1935 1936 1937
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1938
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1939
| prefix | string  | Yes   |Key prefix.    |
A
annie_wangli 已提交
1940

A
annie_wangli 已提交
1941
**Return value**
A
annie_wangli 已提交
1942

A
annie_wangli 已提交
1943
| Type   | Description      |
A
annie_wangli 已提交
1944
| ------  | -------   |
A
annie_wangli 已提交
1945
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1946

A
annie_wangli 已提交
1947
**Example**
A
annie_wangli 已提交
1948

A
annie_wangli 已提交
1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959
```
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);
}
```
A
annie_wangli 已提交
1960 1961 1962 1963


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

A
annie_wangli 已提交
1964
setSuggestIndex(index: string): Query
A
annie_wangli 已提交
1965 1966 1967

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

A
annie_wangli 已提交
1968
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
1969

A
annie_wangli 已提交
1970 1971 1972
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
1973
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
1974
| index  | string  | Yes   |Index preferentially used for query.  |
A
annie_wangli 已提交
1975

A
annie_wangli 已提交
1976
**Return value**
A
annie_wangli 已提交
1977

A
annie_wangli 已提交
1978
| Type   | Description      |
A
annie_wangli 已提交
1979
| ------  | -------   |
A
annie_wangli 已提交
1980
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
1981

A
annie_wangli 已提交
1982
**Example**
A
annie_wangli 已提交
1983

A
annie_wangli 已提交
1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
```
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);
}
```
A
annie_wangli 已提交
1995 1996 1997 1998


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

A
annie_wangli 已提交
1999
deviceId(deviceId:string):Query
A
annie_wangli 已提交
2000 2001 2002

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

A
annie_wangli 已提交
2003
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2004

A
annie_wangli 已提交
2005 2006 2007
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2008
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2009
| deviceId | string  | Yes   |Device ID.  |
A
annie_wangli 已提交
2010 2011


A
annie_wangli 已提交
2012
**Return value**
A
annie_wangli 已提交
2013

A
annie_wangli 已提交
2014
| Type   | Description      |
A
annie_wangli 已提交
2015
| ------  | -------   |
A
annie_wangli 已提交
2016
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
2017

A
annie_wangli 已提交
2018
**Example**
A
annie_wangli 已提交
2019

A
annie_wangli 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028
```
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);
}
```
A
annie_wangli 已提交
2029 2030 2031 2032


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

A
annie_wangli 已提交
2033
getSqlLike():string
A
annie_wangli 已提交
2034 2035 2036

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

A
annie_wangli 已提交
2037
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2038

A
annie_wangli 已提交
2039 2040 2041
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2042
| ------  | -------   |
A
annie_wangli 已提交
2043
| [Query](#query8) |**Query** object Created.|
A
annie_wangli 已提交
2044

A
annie_wangli 已提交
2045
**Example**
A
annie_wangli 已提交
2046

A
annie_wangli 已提交
2047 2048 2049 2050 2051 2052 2053 2054 2055
```
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);
}
```
A
annie_wangli 已提交
2056 2057 2058 2059


## KVStore

A
annie_wangli 已提交
2060
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 已提交
2061

A
annie_wangli 已提交
2062 2063
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
annie_wangli 已提交
2064 2065 2066 2067
### put

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

A
annie_wangli 已提交
2068
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 已提交
2069

A
annie_wangli 已提交
2070
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2071

A
annie_wangli 已提交
2072
**Parameters**
A
annie_wangli 已提交
2073

A
annie_wangli 已提交
2074 2075 2076 2077
| 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).  |
A
annie_wangli 已提交
2078
| callback | AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result.  |
A
annie_wangli 已提交
2079

A
annie_wangli 已提交
2080
**Example**
A
annie_wangli 已提交
2081

A
annie_wangli 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097
```
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);
}
```
A
annie_wangli 已提交
2098 2099 2100 2101 2102 2103


### put

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

A
annie_wangli 已提交
2104
Adds a KV pair of the specified type to this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2105

A
annie_wangli 已提交
2106
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2107

A
annie_wangli 已提交
2108 2109 2110
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2111
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2112 2113
| 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).  |
A
annie_wangli 已提交
2114

A
annie_wangli 已提交
2115
**Return value**
A
annie_wangli 已提交
2116

A
annie_wangli 已提交
2117
| Type   | Description      |
A
annie_wangli 已提交
2118 2119 2120
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2121
**Example**
A
annie_wangli 已提交
2122

A
annie_wangli 已提交
2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
```
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);
}
```
A
annie_wangli 已提交
2137 2138 2139 2140 2141 2142


### delete

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

A
annie_wangli 已提交
2143
Deletes a KV pair from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2144

A
annie_wangli 已提交
2145
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2146

A
annie_wangli 已提交
2147 2148 2149
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2150
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2151 2152
| 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 invoked to return the result.  |
A
annie_wangli 已提交
2153

A
annie_wangli 已提交
2154
**Example**
A
annie_wangli 已提交
2155

A
annie_wangli 已提交
2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
```
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) {
A
annie_wangli 已提交
2168
            if (err != undefined) {
A
annie_wangli 已提交
2169
                console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2170 2171
                return;
            }
A
annie_wangli 已提交
2172
            console.log("delete success");
A
annie_wangli 已提交
2173
        });
A
annie_wangli 已提交
2174 2175 2176 2177 2178
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2179 2180 2181 2182 2183 2184


### delete

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

A
annie_wangli 已提交
2185
Deletes a KV pair from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2186

A
annie_wangli 已提交
2187
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2188

A
annie_wangli 已提交
2189 2190 2191
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2192
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2193
| key    | string  | Yes   |Key of the KV pair to delete. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants).  |
A
annie_wangli 已提交
2194

A
annie_wangli 已提交
2195
**Return value**
A
annie_wangli 已提交
2196

A
annie_wangli 已提交
2197
| Type   | Description      |
A
annie_wangli 已提交
2198 2199 2200
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2201
**Example**
A
annie_wangli 已提交
2202

A
annie_wangli 已提交
2203 2204 2205 2206 2207 2208 2209 2210 2211
```
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");
A
annie_wangli 已提交
2212
        }).catch((err) => {
A
annie_wangli 已提交
2213
            console.log("delete err: " + JSON.stringify(err));
A
annie_wangli 已提交
2214
        });
A
annie_wangli 已提交
2215 2216 2217 2218 2219 2220 2221
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2222 2223 2224 2225 2226 2227 2228 2229


### 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.

A
annie_wangli 已提交
2230
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2231

A
annie_wangli 已提交
2232 2233 2234
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2235
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2236
| event  |'dataChange'  | Yes   |Type of the events.       |
A
annie_wangli 已提交
2237 2238
| type  |[SubscribeType](#subscribetype) | Yes   |Type of data changes.    |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes   |Callback invoked to return the result.|
A
annie_wangli 已提交
2239

A
annie_wangli 已提交
2240
**Example**
A
annie_wangli 已提交
2241

A
annie_wangli 已提交
2242 2243 2244 2245 2246 2247
```
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
    console.log("dataChange callback call data: " + JSON.stringify(data));
});
```
A
annie_wangli 已提交
2248 2249 2250 2251


### on

A
annie_wangli 已提交
2252
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
2253

A
annie_wangli 已提交
2254
Subscribes to data synchronization completion events. This method uses a synchronous callback to return the result.
A
annie_wangli 已提交
2255

A
annie_wangli 已提交
2256
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2257

A
annie_wangli 已提交
2258 2259 2260
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2261
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2262
| event  |'syncComplete' | Yes   |Type of the events.       |
A
annie_wangli 已提交
2263
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes   |Callback invoked to return the result.    |
A
annie_wangli 已提交
2264

A
annie_wangli 已提交
2265
**Example**
A
annie_wangli 已提交
2266

A
annie_wangli 已提交
2267 2268 2269 2270 2271 2272
```
let kvStore;
kvStore.on('syncComplete', function (data) {
    console.log("syncComplete callback call data: " + data);
});
```
A
annie_wangli 已提交
2273 2274 2275

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

A
annie_wangli 已提交
2276
off(event:'dataChange', observer?: Callback&lt;ChangeNotification&gt;): void
A
annie_wangli 已提交
2277

A
annie_wangli 已提交
2278
Unsubscribes from data change events. This method uses a synchronous callback to return the result.
A
annie_wangli 已提交
2279

A
annie_wangli 已提交
2280
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2281

A
annie_wangli 已提交
2282 2283 2284
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2285
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2286 2287
| event  |'dataChange'  | Yes   |Type of the events.       |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; |No   |Callback used to return the result.|
A
annie_wangli 已提交
2288

A
annie_wangli 已提交
2289
**Example**
A
annie_wangli 已提交
2290

A
annie_wangli 已提交
2291 2292 2293 2294 2295 2296 2297 2298 2299
```
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);
});
```
A
annie_wangli 已提交
2300 2301 2302 2303


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

A
annie_wangli 已提交
2304
putBatch(entries: Entry[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2305

A
annie_wangli 已提交
2306
Inserts KV pairs in batches to this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2307

A
annie_wangli 已提交
2308
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2309

A
annie_wangli 已提交
2310 2311 2312
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2313
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2314 2315
| entries  |[Entry](#entry)[] | Yes   |KV pairs to insert in batches. |
| callback |Asyncallback&lt;void&gt; |Yes    |Callback invoked to return the result.|
A
annie_wangli 已提交
2316

A
annie_wangli 已提交
2317
**Example**
A
annie_wangli 已提交
2318

A
annie_wangli 已提交
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329
```
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'
A
annie_wangli 已提交
2330 2331
            }
        }
A
annie_wangli 已提交
2332
        entries.push(entry);
A
annie_wangli 已提交
2333
    }
A
annie_wangli 已提交
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
    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);
}
```
A
annie_wangli 已提交
2347 2348 2349 2350


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

A
annie_wangli 已提交
2351
putBatch(entries: Entry[]): Promise&lt;void&gt;
A
annie_wangli 已提交
2352

A
annie_wangli 已提交
2353
Inserts KV pairs in batches to this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2354

A
annie_wangli 已提交
2355
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2356

A
annie_wangli 已提交
2357 2358 2359
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2360
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2361
| entries  |[Entry](#entry)[] | Yes   |KV pairs to insert in batches. |
A
annie_wangli 已提交
2362

A
annie_wangli 已提交
2363
**Return value**
A
annie_wangli 已提交
2364

A
annie_wangli 已提交
2365
| Type   | Description      |
A
annie_wangli 已提交
2366 2367 2368
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2369
**Example**
A
annie_wangli 已提交
2370

A
annie_wangli 已提交
2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381
```
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'
A
annie_wangli 已提交
2382 2383
            }
        }
A
annie_wangli 已提交
2384 2385 2386 2387 2388 2389 2390 2391
        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));
A
annie_wangli 已提交
2392
        }).catch((err) => {
A
annie_wangli 已提交
2393
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2394
        });
A
annie_wangli 已提交
2395 2396 2397 2398 2399 2400 2401
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
2402 2403 2404 2405


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

A
annie_wangli 已提交
2406
deleteBatch(keys: string[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2407

A
annie_wangli 已提交
2408
Deletes KV pairs in batches from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2409

A
annie_wangli 已提交
2410
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2411

A
annie_wangli 已提交
2412 2413 2414
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2415
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2416 2417
| keys  |string[] | Yes   |KV pairs to delete in batches. |
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2418

A
annie_wangli 已提交
2419
**Example**
A
annie_wangli 已提交
2420

A
annie_wangli 已提交
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432
```
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'
A
annie_wangli 已提交
2433 2434
            }
        }
A
annie_wangli 已提交
2435 2436
        entries.push(entry);
        keys.push(key + i);
A
annie_wangli 已提交
2437
    }
A
annie_wangli 已提交
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
    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);
}
```
A
annie_wangli 已提交
2449 2450 2451 2452


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

A
annie_wangli 已提交
2453
deleteBatch(keys: string[]): Promise&lt;void&gt;
A
annie_wangli 已提交
2454

A
annie_wangli 已提交
2455
Deletes KV pairs in batches from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2456

A
annie_wangli 已提交
2457
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2458

A
annie_wangli 已提交
2459 2460 2461
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2462
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2463
| keys   |string[] | Yes   |KV pairs to delete in batches. |
A
annie_wangli 已提交
2464

A
annie_wangli 已提交
2465
**Return value**
A
annie_wangli 已提交
2466

A
annie_wangli 已提交
2467
| Type   | Description      |
A
annie_wangli 已提交
2468 2469 2470
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2471
**Example**
A
annie_wangli 已提交
2472

A
annie_wangli 已提交
2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
```
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'
A
annie_wangli 已提交
2485 2486
            }
        }
A
annie_wangli 已提交
2487 2488 2489 2490 2491 2492 2493 2494
        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');
A
annie_wangli 已提交
2495
        }).catch((err) => {
A
annie_wangli 已提交
2496
            console.log('deleteBatch fail ' + JSON.stringify(err));
A
annie_wangli 已提交
2497
        });
A
annie_wangli 已提交
2498 2499 2500 2501 2502 2503 2504
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('DeleteBatch e ' + e);
}
```
A
annie_wangli 已提交
2505 2506 2507 2508


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

A
annie_wangli 已提交
2509
startTransaction(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2510

A
annie_wangli 已提交
2511
Starts the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2512

A
annie_wangli 已提交
2513
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2514

A
annie_wangli 已提交
2515 2516 2517
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2518
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2519
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2520

A
annie_wangli 已提交
2521
**Example**
A
annie_wangli 已提交
2522

A
annie_wangli 已提交
2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
```
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'
A
annie_wangli 已提交
2533 2534
            }
        }
A
annie_wangli 已提交
2535
        entries.push(entry);
A
annie_wangli 已提交
2536
    }
A
annie_wangli 已提交
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550
    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');
A
annie_wangli 已提交
2551
        });
A
annie_wangli 已提交
2552 2553 2554 2555 2556
    });
}catch(e) {
    console.log('startTransaction e ' + e);
}
```
A
annie_wangli 已提交
2557 2558 2559 2560


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

A
annie_wangli 已提交
2561
startTransaction(): Promise&lt;void&gt;
A
annie_wangli 已提交
2562

A
annie_wangli 已提交
2563
Starts the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2564

A
annie_wangli 已提交
2565
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2566

A
annie_wangli 已提交
2567 2568 2569
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2570 2571 2572
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2573
**Example**
A
annie_wangli 已提交
2574

A
annie_wangli 已提交
2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
```
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);
}
```
A
annie_wangli 已提交
2592 2593 2594 2595


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

A
annie_wangli 已提交
2596
commit(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2597

A
annie_wangli 已提交
2598
Commits the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2599

A
annie_wangli 已提交
2600
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2601

A
annie_wangli 已提交
2602 2603 2604
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2605
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2606
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2607

A
annie_wangli 已提交
2608
**Example**
A
annie_wangli 已提交
2609

A
annie_wangli 已提交
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
```
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);
}
```
A
annie_wangli 已提交
2624 2625 2626 2627


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

A
annie_wangli 已提交
2628
commit(): Promise&lt;void&gt;
A
annie_wangli 已提交
2629

A
annie_wangli 已提交
2630
Commits the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2631

A
annie_wangli 已提交
2632
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2633

A
annie_wangli 已提交
2634 2635 2636
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2637 2638 2639
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2640
**Example**
A
annie_wangli 已提交
2641

A
annie_wangli 已提交
2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653
```
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);
}
```
A
annie_wangli 已提交
2654 2655 2656 2657


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

A
annie_wangli 已提交
2658
rollback(callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2659

A
annie_wangli 已提交
2660
Rolls back the transaction in this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
2661

A
annie_wangli 已提交
2662
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2663

A
annie_wangli 已提交
2664 2665 2666
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2667
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2668
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2669

A
annie_wangli 已提交
2670
**Example**
A
annie_wangli 已提交
2671

A
annie_wangli 已提交
2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
```
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);
}
```
A
annie_wangli 已提交
2686 2687 2688 2689


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

A
annie_wangli 已提交
2690
rollback(): Promise&lt;void&gt;
A
annie_wangli 已提交
2691

A
annie_wangli 已提交
2692
Rolls back the transaction in this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
2693

A
annie_wangli 已提交
2694
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2695

A
annie_wangli 已提交
2696 2697 2698
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
2699 2700 2701
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2702
**Example**
A
annie_wangli 已提交
2703

A
annie_wangli 已提交
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
```
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);
}
```
A
annie_wangli 已提交
2716 2717 2718 2719


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

A
annie_wangli 已提交
2720
enableSync(enabled: boolean, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2721 2722 2723

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

A
annie_wangli 已提交
2724
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2725

A
annie_wangli 已提交
2726 2727 2728
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2729
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2730 2731
| enabled  |boolean | Yes   |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2732

A
annie_wangli 已提交
2733
**Example**
A
annie_wangli 已提交
2734

A
annie_wangli 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748
```
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);
}
```
A
annie_wangli 已提交
2749 2750 2751 2752


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

A
annie_wangli 已提交
2753
enableSync(enabled: boolean): Promise&lt;void&gt;
A
annie_wangli 已提交
2754

A
annie_wangli 已提交
2755
Enables or disables data synchronization. This method uses a promise to return the result.
A
annie_wangli 已提交
2756

A
annie_wangli 已提交
2757
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2758

A
annie_wangli 已提交
2759 2760 2761
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2762
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2763
| enabled  |boolean | Yes   |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
A
annie_wangli 已提交
2764

A
annie_wangli 已提交
2765
**Return value**
A
annie_wangli 已提交
2766

A
annie_wangli 已提交
2767
| Type   | Description      |
A
annie_wangli 已提交
2768 2769 2770
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2771
**Example**
A
annie_wangli 已提交
2772

A
annie_wangli 已提交
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784
```
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);
}
```
A
annie_wangli 已提交
2785 2786 2787 2788


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

A
annie_wangli 已提交
2789
setSyncRange(localLabels: string[], remoteSupportLabels: string[], callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
2790 2791 2792

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

A
annie_wangli 已提交
2793
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2794

A
annie_wangli 已提交
2795 2796 2797
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2798
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2799 2800 2801
| localLabels  |string[] | Yes   |Synchronization labels set for the local device. |
| remoteSupportLabels  |string[] | Yes   |Synchronization labels set for remote devices. |
| callback  |AsyncCallback&lt;void&gt; | Yes   |Callback invoked to return the result. |
A
annie_wangli 已提交
2802

A
annie_wangli 已提交
2803
**Example**
A
annie_wangli 已提交
2804

A
annie_wangli 已提交
2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816
```
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);
}
```
A
annie_wangli 已提交
2817 2818 2819 2820


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

A
annie_wangli 已提交
2821
setSyncRange(localLabels: string[], remoteSupportLabels: string[]): Promise&lt;void&gt;
A
annie_wangli 已提交
2822 2823 2824

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

A
annie_wangli 已提交
2825
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2826

A
annie_wangli 已提交
2827 2828 2829
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2830
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2831 2832
| localLabels  |string[] | Yes   |Synchronization labels set for the local device. |
| remoteSupportLabels  |string[] | Yes   |Synchronization labels set for remote devices. |
A
annie_wangli 已提交
2833 2834


A
annie_wangli 已提交
2835
**Return value**
A
annie_wangli 已提交
2836

A
annie_wangli 已提交
2837
| Type   | Description      |
A
annie_wangli 已提交
2838 2839 2840
| ------  | -------   |
| Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
2841
**Example**
A
annie_wangli 已提交
2842

A
annie_wangli 已提交
2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856
```
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);
}
```
A
annie_wangli 已提交
2857 2858 2859 2860 2861 2862


## SubscribeType

Defines the subscription type.

A
annie_wangli 已提交
2863 2864 2865
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Default Value  | Description                   |
A
annie_wangli 已提交
2866
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
2867 2868 2869
| SUBSCRIBE_TYPE_LOCAL  |0 |Local data changes. |
| SUBSCRIBE_TYPE_REMOTE |1 |Remote data changes. |
| SUBSCRIBE_TYPE_ALL  |2 |Local and remote data changes. |
A
annie_wangli 已提交
2870 2871 2872 2873 2874

## ChangeNotification

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

A
annie_wangli 已提交
2875 2876 2877
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
2878
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
2879 2880 2881 2882
| 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. |
A
annie_wangli 已提交
2883 2884 2885

## Entry

A
annie_wangli 已提交
2886
Defines the KV pairs stored in the KV store.
A
annie_wangli 已提交
2887

A
annie_wangli 已提交
2888 2889 2890
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
2891
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
2892 2893
| 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 已提交
2894 2895 2896 2897 2898 2899


## Value

Defines the value in a KV pair.

A
annie_wangli 已提交
2900 2901 2902
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Type  |Readable  |Writable  | Description                   |
A
annie_wangli 已提交
2903
| ----- | -------   | -----| ------|------------------------ |
A
annie_wangli 已提交
2904 2905
| type | [ValueType](#value)   | Yes |  Yes|Type of the value.  |
| value | Uint8Array / string / number / boolean| Yes |  Yes|Value of the KV pair stored in the KV store.  |
A
annie_wangli 已提交
2906 2907 2908

## ValueType

A
annie_wangli 已提交
2909
Enumerates the types of values in KV pairs.
A
annie_wangli 已提交
2910

A
annie_wangli 已提交
2911
These value types can be used only by internal applications.
A
annie_wangli 已提交
2912

A
annie_wangli 已提交
2913 2914 2915
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name | Default Value  | Description                   |
A
annie_wangli 已提交
2916
| -----  | ------   | ----------------------- |
A
annie_wangli 已提交
2917 2918 2919 2920 2921 2922
| 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). |
A
annie_wangli 已提交
2923 2924 2925

## SingleKVStore

A
annie_wangli 已提交
2926
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 已提交
2927

A
annie_wangli 已提交
2928 2929
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

A
annie_wangli 已提交
2930 2931 2932 2933 2934 2935
### 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.

A
annie_wangli 已提交
2936
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2937

A
annie_wangli 已提交
2938 2939 2940
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2941
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2942
| 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 已提交
2943
| callback  |AsyncCallback&lt;Uint8Array \| string \| boolean \| number&gt;) | Yes   |Callback used to return the value obtained. |
A
annie_wangli 已提交
2944

A
annie_wangli 已提交
2945
**Example**
A
annie_wangli 已提交
2946

A
annie_wangli 已提交
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959
```
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);
A
annie_wangli 已提交
2960
        });
A
annie_wangli 已提交
2961 2962 2963 2964 2965
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
2966 2967 2968 2969


### get

A
annie_wangli 已提交
2970
get(key: string): Promise&lt;Uint8Array | string | boolean | number&gt;
A
annie_wangli 已提交
2971 2972 2973

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

A
annie_wangli 已提交
2974
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
2975

A
annie_wangli 已提交
2976 2977 2978
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
2979
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
2980
| 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 已提交
2981 2982


A
annie_wangli 已提交
2983
**Return value**
A
annie_wangli 已提交
2984

A
annie_wangli 已提交
2985
| Type   | Description      |
A
annie_wangli 已提交
2986
| ------  | -------   |
A
annie_wangli 已提交
2987
|Promise&lt;Uint8Array \| string \| boolean \| number&gt; |Promise used to return the value obtained.|
A
annie_wangli 已提交
2988

A
annie_wangli 已提交
2989
**Example**
A
annie_wangli 已提交
2990

A
annie_wangli 已提交
2991 2992 2993 2994 2995 2996 2997 2998 2999
```
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);
A
annie_wangli 已提交
3000
        }).catch((err) => {
A
annie_wangli 已提交
3001
            console.log("get err: " + JSON.stringify(err));
A
annie_wangli 已提交
3002
        });
A
annie_wangli 已提交
3003 3004 3005 3006 3007 3008 3009
    }).catch((err) => {
        console.log("put err: " + JSON.stringify(err));
    });
}catch (e) {
    console.log("An unexpected error occurred. Error:" + e);
}
```
A
annie_wangli 已提交
3010 3011 3012

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

A
annie_wangli 已提交
3013
getEntries(keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
3014

A
annie_wangli 已提交
3015
Obtains the KV pairs that match the specified key prefix. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3016

A
annie_wangli 已提交
3017
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3018

A
annie_wangli 已提交
3019 3020 3021
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3022
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3023
| keyPrefix    |string   | Yes   |Key prefix to match. |
A
annie_wangli 已提交
3024
| callback    |AsyncCallback&lt;[Entry](#entry)[]&gt;   | Yes   |Callback used to return the KV pairs obtained. |
A
annie_wangli 已提交
3025

A
annie_wangli 已提交
3026
**Example**
A
annie_wangli 已提交
3027

A
annie_wangli 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
```
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
A
annie_wangli 已提交
3039 3040
            }
        }
A
annie_wangli 已提交
3041
        entries.push(entry);
A
annie_wangli 已提交
3042
    }
A
annie_wangli 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
    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);
}
```
A
annie_wangli 已提交
3055 3056 3057 3058


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

A
annie_wangli 已提交
3059
getEntries(keyPrefix: string): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
3060

A
annie_wangli 已提交
3061
Obtains the KV pairs that match the specified key prefix. This method uses a promise to return the result.
A
annie_wangli 已提交
3062

A
annie_wangli 已提交
3063
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3064

A
annie_wangli 已提交
3065 3066 3067
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3068
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3069
| keyPrefix    |string   | Yes   |Key prefix to match. |
A
annie_wangli 已提交
3070

A
annie_wangli 已提交
3071
**Return value**
A
annie_wangli 已提交
3072

A
annie_wangli 已提交
3073
| Type   | Description      |
A
annie_wangli 已提交
3074
| ------  | -------   |
A
annie_wangli 已提交
3075
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3076

A
annie_wangli 已提交
3077
**Example**
A
annie_wangli 已提交
3078

A
annie_wangli 已提交
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089
```
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'
A
annie_wangli 已提交
3090 3091
            }
        }
A
annie_wangli 已提交
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102
        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);
A
annie_wangli 已提交
3103
        }).catch((err) => {
A
annie_wangli 已提交
3104
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3105
        });
A
annie_wangli 已提交
3106 3107 3108 3109 3110 3111 3112
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
3113 3114 3115 3116


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

A
annie_wangli 已提交
3117
getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
3118

A
annie_wangli 已提交
3119
Obtains the KV pairs that match the specified **Query** object. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3120

A
annie_wangli 已提交
3121
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3122

A
annie_wangli 已提交
3123 3124 3125
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3126
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3127
| query  |[Query](#query8)   | Yes   |**Query** object to match. |
A
annie_wangli 已提交
3128
| callback  |AsyncCallback&lt;[Entry](#entry)[]&gt;   | Yes   |Callback used to return the KV pairs obtained. |
A
annie_wangli 已提交
3129

A
annie_wangli 已提交
3130
**Example**
A
annie_wangli 已提交
3131

A
annie_wangli 已提交
3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143
```
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
A
annie_wangli 已提交
3144 3145
            }
        }
A
annie_wangli 已提交
3146
        entries.push(entry);
A
annie_wangli 已提交
3147
    }
A
annie_wangli 已提交
3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
    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);
}
```
A
annie_wangli 已提交
3164 3165 3166 3167


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

A
annie_wangli 已提交
3168
getEntries(query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
3169

A
annie_wangli 已提交
3170
Obtains the KV pairs that match the specified **Query** object. This method uses a promise to return the result.
A
annie_wangli 已提交
3171

A
annie_wangli 已提交
3172
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3173

A
annie_wangli 已提交
3174 3175 3176
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3177
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3178
| query  |[Query](#query8)   | Yes   |**Query** object to match. |
A
annie_wangli 已提交
3179

A
annie_wangli 已提交
3180
**Return value**
A
annie_wangli 已提交
3181

A
annie_wangli 已提交
3182
| Type   | Description      |
A
annie_wangli 已提交
3183
| ------  | -------   |
A
annie_wangli 已提交
3184
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
3185

A
annie_wangli 已提交
3186
**Example**
A
annie_wangli 已提交
3187

A
annie_wangli 已提交
3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198
```
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
A
annie_wangli 已提交
3199 3200
            }
        }
A
annie_wangli 已提交
3201 3202 3203 3204 3205 3206 3207 3208 3209
        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');
A
annie_wangli 已提交
3210
        }).catch((err) => {
A
annie_wangli 已提交
3211
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
3212
        });
A
annie_wangli 已提交
3213 3214 3215 3216 3217 3218 3219 3220
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
3221 3222 3223 3224


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

A
annie_wangli 已提交
3225
getResultSet(keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
3226

A
annie_wangli 已提交
3227
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 已提交
3228

A
annie_wangli 已提交
3229
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3230

A
annie_wangli 已提交
3231 3232 3233
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3234
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3235 3236
| keyPrefix  |string   | Yes   |Key prefix to match.|
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback used to return the result set obtained.|
A
annie_wangli 已提交
3237

A
annie_wangli 已提交
3238
**Example**
A
annie_wangli 已提交
3239

A
annie_wangli 已提交
3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251
```
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'
A
annie_wangli 已提交
3252 3253
            }
        }
A
annie_wangli 已提交
3254
        entries.push(entry);
A
annie_wangli 已提交
3255
    }
A
annie_wangli 已提交
3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269
    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);
}
```
A
annie_wangli 已提交
3270 3271 3272 3273


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

A
annie_wangli 已提交
3274
getResultSet(keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3275

A
annie_wangli 已提交
3276
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 已提交
3277

A
annie_wangli 已提交
3278
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3279

A
annie_wangli 已提交
3280 3281 3282
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3283
| -----  | ------  | ----  | ----------------------- |
A
annie_wangli 已提交
3284
| keyPrefix  |string   | Yes   |Key prefix to match.|
A
annie_wangli 已提交
3285

A
annie_wangli 已提交
3286
**Return value**
A
annie_wangli 已提交
3287

A
annie_wangli 已提交
3288
| Type   | Description      |
A
annie_wangli 已提交
3289
| ------  | -------   |
Z
zengyawen 已提交
3290
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the result set obtained.|
A
annie_wangli 已提交
3291

A
annie_wangli 已提交
3292
**Example**
A
annie_wangli 已提交
3293

A
annie_wangli 已提交
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
```
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'
A
annie_wangli 已提交
3306 3307
            }
        }
A
annie_wangli 已提交
3308
        entries.push(entry);
A
annie_wangli 已提交
3309
    }
A
annie_wangli 已提交
3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
    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);
}
```
A
annie_wangli 已提交
3330 3331 3332 3333


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

A
annie_wangli 已提交
3334
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
3335 3336 3337

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

A
annie_wangli 已提交
3338
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3339

A
annie_wangli 已提交
3340 3341 3342
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3343
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3344 3345
| query  |Query    | Yes   |**Query** object to match.            |
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt;   | Yes   |Callback used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3346

A
annie_wangli 已提交
3347
**Example**
A
annie_wangli 已提交
3348

A
annie_wangli 已提交
3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
```
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'
A
annie_wangli 已提交
3361 3362
            }
        }
A
annie_wangli 已提交
3363
        entries.push(entry);
A
annie_wangli 已提交
3364
    }
A
annie_wangli 已提交
3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377
    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);
}
```
A
annie_wangli 已提交
3378 3379 3380 3381


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

A
annie_wangli 已提交
3382
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
3383 3384 3385

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

A
annie_wangli 已提交
3386
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3387

A
annie_wangli 已提交
3388 3389 3390
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3391
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3392
| query  |[Query](#query8)    | Yes   |**Query** object to match.            |
A
annie_wangli 已提交
3393

A
annie_wangli 已提交
3394
**Return value**
A
annie_wangli 已提交
3395

A
annie_wangli 已提交
3396
| Type   | Description      |
A
annie_wangli 已提交
3397
| ------  | -------   |
Z
zengyawen 已提交
3398
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
3399

A
annie_wangli 已提交
3400
**Example**
A
annie_wangli 已提交
3401

A
annie_wangli 已提交
3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
```
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'
A
annie_wangli 已提交
3414 3415
            }
        }
A
annie_wangli 已提交
3416
        entries.push(entry);
A
annie_wangli 已提交
3417
    }
A
annie_wangli 已提交
3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434
    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);
}
```
A
annie_wangli 已提交
3435

A
annie_wangli 已提交
3436 3437
### closeResultSet<sup>8+</sup> ###

A
annie_wangli 已提交
3438
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3439 3440 3441

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

A
annie_wangli 已提交
3442
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3443

A
annie_wangli 已提交
3444 3445 3446
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3447
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3448 3449
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the execution result.            |
A
annie_wangli 已提交
3450

A
annie_wangli 已提交
3451
**Example**
A
annie_wangli 已提交
3452

A
annie_wangli 已提交
3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467
```
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);
}
```
A
annie_wangli 已提交
3468 3469 3470 3471


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

A
annie_wangli 已提交
3472
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
3473 3474 3475

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

A
annie_wangli 已提交
3476
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3477

A
annie_wangli 已提交
3478 3479 3480
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3481
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3482
| resultSet  |[KvStoreResultSet](#kvstoreresultset8)   | Yes   |**KvStoreResultSet** object to close.            |
A
annie_wangli 已提交
3483

A
annie_wangli 已提交
3484
**Return value**
A
annie_wangli 已提交
3485

A
annie_wangli 已提交
3486
| Type   | Description      |
A
annie_wangli 已提交
3487 3488 3489
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
3490
**Example**
A
annie_wangli 已提交
3491

A
annie_wangli 已提交
3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
```
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);
}
```
A
annie_wangli 已提交
3505 3506 3507 3508


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

A
annie_wangli 已提交
3509 3510 3511
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.
A
annie_wangli 已提交
3512

A
annie_wangli 已提交
3513
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3514

A
annie_wangli 已提交
3515
**Parameters**
A
annie_wangli 已提交
3516

A
annie_wangli 已提交
3517
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3518
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3519 3520
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
| callback  |AsyncCallback&lt;number&gt;   | Yes   |Callback used to return the number of results obtained.        |
A
annie_wangli 已提交
3521

A
annie_wangli 已提交
3522
**Example**
A
annie_wangli 已提交
3523

A
annie_wangli 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
```
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'
A
annie_wangli 已提交
3535 3536
            }
        }
A
annie_wangli 已提交
3537
        entries.push(entry);
A
annie_wangli 已提交
3538
    }
A
annie_wangli 已提交
3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
    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);
}
```
A
annie_wangli 已提交
3551 3552 3553 3554


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

A
annie_wangli 已提交
3555 3556 3557
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.
A
annie_wangli 已提交
3558

A
annie_wangli 已提交
3559
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3560

A
annie_wangli 已提交
3561
**Parameters**
A
annie_wangli 已提交
3562

A
annie_wangli 已提交
3563
| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3564
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3565
| query  |[Query](#query8)   | Yes   |**Query** object to match.        |
A
annie_wangli 已提交
3566

A
annie_wangli 已提交
3567
**Return value**
A
annie_wangli 已提交
3568

A
annie_wangli 已提交
3569
| Type   | Description      |
A
annie_wangli 已提交
3570
| ------  | -------   |
A
annie_wangli 已提交
3571
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
3572

A
annie_wangli 已提交
3573
**Example**
A
annie_wangli 已提交
3574

A
annie_wangli 已提交
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585
```
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'
A
annie_wangli 已提交
3586 3587
            }
        }
A
annie_wangli 已提交
3588
        entries.push(entry);
A
annie_wangli 已提交
3589
    }
A
annie_wangli 已提交
3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605
    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);
}
```
A
annie_wangli 已提交
3606 3607 3608 3609


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

A
annie_wangli 已提交
3610
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3611 3612 3613

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

A
annie_wangli 已提交
3614
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3615

A
annie_wangli 已提交
3616 3617 3618
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3619
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3620 3621
| deviceId  |string   | Yes   |ID of the target device.      |
| callback  |AsyncCallback&lt;void&gt;   | Yes   |Callback used to return the result.     |
A
annie_wangli 已提交
3622

A
annie_wangli 已提交
3623
**Example**
A
annie_wangli 已提交
3624

A
annie_wangli 已提交
3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641
```
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');
                });
            }
A
annie_wangli 已提交
3642
        });
A
annie_wangli 已提交
3643 3644 3645 3646 3647
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
3648 3649 3650 3651


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

A
annie_wangli 已提交
3652
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
3653 3654 3655

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

A
annie_wangli 已提交
3656
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3657

A
annie_wangli 已提交
3658 3659 3660
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3661
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3662
| deviceId  |string   | Yes   |ID of the target device.      |
A
annie_wangli 已提交
3663

A
annie_wangli 已提交
3664
**Return value**
A
annie_wangli 已提交
3665

A
annie_wangli 已提交
3666
| Type   | Description      |
A
annie_wangli 已提交
3667 3668 3669
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
3670
**Example**
A
annie_wangli 已提交
3671

A
annie_wangli 已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696
```
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);
}
```
A
annie_wangli 已提交
3697 3698 3699 3700


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

A
annie_wangli 已提交
3701
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
3702 3703 3704

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

A
annie_wangli 已提交
3705
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3706

A
annie_wangli 已提交
3707 3708 3709
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3710
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3711 3712
| event  |'syncComplete'   | Yes   |Event triggered when the synchronization is complete.   |
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt;   | Yes   |Callback used to return the synchronization result.   |
A
annie_wangli 已提交
3713

A
annie_wangli 已提交
3714
**Example**
A
annie_wangli 已提交
3715

A
annie_wangli 已提交
3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732
```
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);
}
```
A
annie_wangli 已提交
3733 3734 3735 3736


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

A
annie_wangli 已提交
3737
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
3738

A
annie_wangli 已提交
3739
Unsubscribes from the synchronization completion events. This method uses a synchronous callback to return the result.
A
annie_wangli 已提交
3740

A
annie_wangli 已提交
3741
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3742

A
annie_wangli 已提交
3743 3744 3745
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3746
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3747 3748
| event  |'syncComplete'   | Yes   |Event triggered when the synchronization is complete.   |
| syncCallback  |Callback&lt;Array&lt;[string, number]&gt;&gt;   | No   |Callback used to return the synchronization result.   |
A
annie_wangli 已提交
3749

A
annie_wangli 已提交
3750
**Example**
A
annie_wangli 已提交
3751

A
annie_wangli 已提交
3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763
```
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);
}
```
A
annie_wangli 已提交
3764 3765 3766 3767 3768 3769 3770


### sync

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

Manually triggers KV store synchronization synchronously.
A
annie_wangli 已提交
3771
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
3772

A
annie_wangli 已提交
3773
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3774

A
annie_wangli 已提交
3775 3776 3777
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3778
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3779 3780 3781
| deviceIdList  |string[]  | Yes   |IDs of the devices to be synchronized. These devices must be in the same networking environment.   |
| mode  |[SyncMode](#syncmode)   | Yes  |Data synchronization mode.   |
| allowedDelayMs  |number   | No  |Allowed synchronization delay time, in ms.  |
A
annie_wangli 已提交
3782

A
annie_wangli 已提交
3783
**Example**
A
annie_wangli 已提交
3784

A
annie_wangli 已提交
3785 3786 3787 3788
```
let kvStore;
kvStore.sync('deviceIds', distributedData.SyncMode.PULL_ONLY, 1000);
```
A
annie_wangli 已提交
3789 3790 3791

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

A
annie_wangli 已提交
3792
setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
3793 3794 3795

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

A
annie_wangli 已提交
3796
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3797

A
annie_wangli 已提交
3798 3799 3800
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3801
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3802 3803
| defaultAllowedDelayMs  |number  | Yes   |Default delay allowed for database synchronization, in ms.   |
| callback  |AsyncCallback&lt;void&gt;  | Yes  |Callback used to return the result.  |
A
annie_wangli 已提交
3804

A
annie_wangli 已提交
3805
**Example**
A
annie_wangli 已提交
3806

A
annie_wangli 已提交
3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
```
let kvStore;
try {
    const defaultAllowedDelayMs = 500;
    kvStore.setSyncParam(defaultAllowedDelayMs, function (err,data) {
        console.log('SetSyncParam put success');
    });
}catch(e) {
    console.log('testSingleKvStoreSetSyncParam e ' + e);
}
```
A
annie_wangli 已提交
3818 3819 3820 3821


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

A
annie_wangli 已提交
3822
setSyncParam(defaultAllowedDelayMs: number): Promise&lt;void&gt;
A
annie_wangli 已提交
3823 3824 3825

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

A
annie_wangli 已提交
3826
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3827

A
annie_wangli 已提交
3828 3829 3830
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3831
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3832
| defaultAllowedDelayMs  |number  | Yes   |Default delay allowed for database synchronization, in ms.   |
A
annie_wangli 已提交
3833 3834


A
annie_wangli 已提交
3835
**Return value**
A
annie_wangli 已提交
3836

A
annie_wangli 已提交
3837
| Type   | Description      |
A
annie_wangli 已提交
3838 3839 3840
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
3841
**Example**
A
annie_wangli 已提交
3842

A
annie_wangli 已提交
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855
```
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);
}
```
A
annie_wangli 已提交
3856 3857 3858 3859


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

A
annie_wangli 已提交
3860
getSecurityLevel(callback: AsyncCallback&lt;SecurityLevel&gt;): void
A
annie_wangli 已提交
3861

A
annie_wangli 已提交
3862
Obtains the security level of this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
3863

A
annie_wangli 已提交
3864
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3865

A
annie_wangli 已提交
3866 3867 3868
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3869
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3870
| callback  |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt;  | Yes   |Callback used to return the security level obtained.   |
A
annie_wangli 已提交
3871

A
annie_wangli 已提交
3872
**Example**
A
annie_wangli 已提交
3873

A
annie_wangli 已提交
3874 3875 3876 3877 3878 3879 3880 3881 3882 3883
```
let kvStore;
try {
    kvStore.getSecurityLevel(function (err,data) {
        console.log('getSecurityLevel success');
    });
}catch(e) {
    console.log('GetSecurityLeve e ' + e);
}
```
A
annie_wangli 已提交
3884 3885 3886 3887


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

A
annie_wangli 已提交
3888
getSecurityLevel(): Promise&lt;SecurityLevel&gt;
A
annie_wangli 已提交
3889

A
annie_wangli 已提交
3890
Obtains the security level of this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
3891

A
annie_wangli 已提交
3892
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
3893

A
annie_wangli 已提交
3894 3895 3896
**Return value**

| Type   | Description      |
A
annie_wangli 已提交
3897
| ------  | -------   |
A
annie_wangli 已提交
3898
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the security level obtained.|
A
annie_wangli 已提交
3899

A
annie_wangli 已提交
3900
**Example**
A
annie_wangli 已提交
3901

A
annie_wangli 已提交
3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913
```
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);
}
```
A
annie_wangli 已提交
3914 3915 3916 3917


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

A
annie_wangli 已提交
3918
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 已提交
3919

A
annie_wangli 已提交
3920 3921
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore

A
annie_wangli 已提交
3922 3923
### get<sup>8+</sup> ###

A
annie_wangli 已提交
3924
get(deviceId: string, key: string, callback: AsyncCallback&lt;boolean|string|number|Uint8Array&gt;): void
A
annie_wangli 已提交
3925 3926 3927

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

A
annie_wangli 已提交
3928
**System capability**:  SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
3929

A
annie_wangli 已提交
3930 3931 3932
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3933
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3934 3935
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
3936
| callback  |AsyncCallback&lt;boolean\|string\|number\|Uint8Array&gt;  | Yes   |Callback used to return the value obtained.   |
A
annie_wangli 已提交
3937

A
annie_wangli 已提交
3938
**Example**
A
annie_wangli 已提交
3939

A
annie_wangli 已提交
3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
```
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);
}
```
A
annie_wangli 已提交
3955 3956 3957 3958


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

A
annie_wangli 已提交
3959
get(deviceId: string, key: string): Promise&lt;boolean|string|number|Uint8Array&gt;
A
annie_wangli 已提交
3960 3961 3962

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

A
annie_wangli 已提交
3963
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
3964

A
annie_wangli 已提交
3965 3966 3967
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
3968
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
3969 3970
| deviceId  |string  | Yes   |ID of the target device.   |
| key       |string  | Yes   |Key to match.   |
A
annie_wangli 已提交
3971

A
annie_wangli 已提交
3972
**Return value**
A
annie_wangli 已提交
3973

A
annie_wangli 已提交
3974
| Type   | Description      |
A
annie_wangli 已提交
3975
| ------  | -------   |
A
annie_wangli 已提交
3976
|Promise&lt;boolean/string/number/Uint8Array&gt; |Promise used to return the value obtained.|
A
annie_wangli 已提交
3977

A
annie_wangli 已提交
3978
**Example**
A
annie_wangli 已提交
3979

A
annie_wangli 已提交
3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990
```
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));
A
annie_wangli 已提交
3991
        });
A
annie_wangli 已提交
3992 3993 3994 3995 3996 3997 3998
    }).catch((error) => {
        console.log('put error' + error);
    });
} catch (e) {
    console.log('Get e ' + e);
}
```
A
annie_wangli 已提交
3999 4000 4001 4002


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

A
annie_wangli 已提交
4003
getEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4004

A
annie_wangli 已提交
4005
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 已提交
4006

A
annie_wangli 已提交
4007
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4008

A
annie_wangli 已提交
4009 4010 4011
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4012
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4013 4014 4015
| 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.   |
A
annie_wangli 已提交
4016

A
annie_wangli 已提交
4017
**Example**
A
annie_wangli 已提交
4018

A
annie_wangli 已提交
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029
```
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'
A
annie_wangli 已提交
4030 4031
            }
        }
A
annie_wangli 已提交
4032
        entries.push(entry);
A
annie_wangli 已提交
4033
    }
A
annie_wangli 已提交
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046
    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);
}
```
A
annie_wangli 已提交
4047 4048 4049 4050


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

A
annie_wangli 已提交
4051
getEntries(deviceId: string, keyPrefix: string): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4052

A
annie_wangli 已提交
4053
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 已提交
4054

A
annie_wangli 已提交
4055
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4056

A
annie_wangli 已提交
4057 4058 4059
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4060
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4061 4062
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4063

A
annie_wangli 已提交
4064
**Return value**
A
annie_wangli 已提交
4065

A
annie_wangli 已提交
4066
| Type   | Description      |
A
annie_wangli 已提交
4067
| ------  | -------   |
A
annie_wangli 已提交
4068
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4069

A
annie_wangli 已提交
4070
**Example**
A
annie_wangli 已提交
4071

A
annie_wangli 已提交
4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082
```
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'
A
annie_wangli 已提交
4083 4084
            }
        }
A
annie_wangli 已提交
4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095
        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);
A
annie_wangli 已提交
4096
        }).catch((err) => {
A
annie_wangli 已提交
4097
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4098
        });
A
annie_wangli 已提交
4099 4100 4101 4102 4103 4104 4105
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
}catch(e) {
    console.log('PutBatch e ' + e);
}
```
A
annie_wangli 已提交
4106 4107 4108 4109


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

A
annie_wangli 已提交
4110
getEntries(query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4111

A
annie_wangli 已提交
4112
Obtains the KV pairs that match the specified **Query** object. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4113

A
annie_wangli 已提交
4114
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4115

A
annie_wangli 已提交
4116 4117 4118
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4119
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4120 4121
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes   |Callback used to return the KV pairs obtained.   |
A
annie_wangli 已提交
4122

A
annie_wangli 已提交
4123
**Example**
A
annie_wangli 已提交
4124

A
annie_wangli 已提交
4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136
```
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
A
annie_wangli 已提交
4137 4138
            }
        }
A
annie_wangli 已提交
4139
        entries.push(entry);
A
annie_wangli 已提交
4140
    }
A
annie_wangli 已提交
4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158
    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);
}
```
A
annie_wangli 已提交
4159 4160 4161 4162


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

A
annie_wangli 已提交
4163
getEntries(query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4164

A
annie_wangli 已提交
4165
Obtains the KV pairs that match the specified **Query** object. This method uses a promise to return the result.
A
annie_wangli 已提交
4166

A
annie_wangli 已提交
4167
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4168

A
annie_wangli 已提交
4169 4170 4171
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4172
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4173
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4174

A
annie_wangli 已提交
4175
**Return value**
A
annie_wangli 已提交
4176

A
annie_wangli 已提交
4177
| Type   | Description      |
A
annie_wangli 已提交
4178
| ------  | -------   |
A
annie_wangli 已提交
4179
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4180

A
annie_wangli 已提交
4181
**Example**
A
annie_wangli 已提交
4182

A
annie_wangli 已提交
4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194
```
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
A
annie_wangli 已提交
4195 4196
            }
        }
A
annie_wangli 已提交
4197 4198 4199 4200 4201 4202 4203 4204 4205
        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');
A
annie_wangli 已提交
4206
        }).catch((err) => {
A
annie_wangli 已提交
4207
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4208
        });
A
annie_wangli 已提交
4209 4210 4211 4212 4213 4214 4215 4216
    }).catch((err) => {
        console.log('GetEntries putBatch fail ' + JSON.stringify(err))
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4217 4218 4219 4220


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

A
annie_wangli 已提交
4221
getEntries(deviceId: string, query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
A
annie_wangli 已提交
4222

A
annie_wangli 已提交
4223
Obtains the KV pairs that match the specified **Query** object for a device. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4224

A
annie_wangli 已提交
4225
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4226

A
annie_wangli 已提交
4227 4228 4229
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4230
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4231 4232 4233
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt;  | Yes   |Callback used to return the KV pairs obtained.   |
A
annie_wangli 已提交
4234

A
annie_wangli 已提交
4235
**Example**
A
annie_wangli 已提交
4236

A
annie_wangli 已提交
4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248
```
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
A
annie_wangli 已提交
4249 4250
            }
        }
A
annie_wangli 已提交
4251
        entries.push(entry);
A
annie_wangli 已提交
4252
    }
A
annie_wangli 已提交
4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270
    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);
}
```
A
annie_wangli 已提交
4271 4272 4273 4274


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

A
annie_wangli 已提交
4275
getEntries(deviceId: string, query: Query): Promise&lt;Entry[]&gt;
A
annie_wangli 已提交
4276

A
annie_wangli 已提交
4277
Obtains the KV pairs that match the specified **Query** object for a device. This method uses a promise to return the result.
A
annie_wangli 已提交
4278

A
annie_wangli 已提交
4279
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4280

A
annie_wangli 已提交
4281 4282 4283
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4284
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4285 4286
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4287

A
annie_wangli 已提交
4288
**Return value**
A
annie_wangli 已提交
4289

A
annie_wangli 已提交
4290
| Type   | Description      |
A
annie_wangli 已提交
4291
| ------  | -------   |
A
annie_wangli 已提交
4292
|Promise&lt;[Entry](#entry)[]&gt; |Promise used to return the KV pairs obtained.|
A
annie_wangli 已提交
4293

A
annie_wangli 已提交
4294
**Example**
A
annie_wangli 已提交
4295

A
annie_wangli 已提交
4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307
```
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
A
annie_wangli 已提交
4308 4309
            }
        }
A
annie_wangli 已提交
4310 4311 4312 4313 4314 4315 4316 4317 4318 4319
        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');
A
annie_wangli 已提交
4320
        }).catch((err) => {
A
annie_wangli 已提交
4321
            console.log('getEntries fail ' + JSON.stringify(err));
A
annie_wangli 已提交
4322
        });
A
annie_wangli 已提交
4323 4324 4325 4326 4327 4328 4329 4330
    }).catch((err) => {
        console.log('putBatch fail ' + JSON.stringify(err));
    });
    console.log('GetEntries success');
}catch(e) {
    console.log('GetEntries e ' + e);
}
```
A
annie_wangli 已提交
4331 4332 4333 4334


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

A
annie_wangli 已提交
4335
getResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4336 4337 4338

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

A
annie_wangli 已提交
4339
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4340

A
annie_wangli 已提交
4341 4342 4343
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4344
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4345 4346 4347
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
| callback  |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt;  | Yes |Callback used to return the **KvStoreResultSet** object obtained.   |
A
annie_wangli 已提交
4348

A
annie_wangli 已提交
4349
**Example**
A
annie_wangli 已提交
4350

A
annie_wangli 已提交
4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365
```
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);
}
```
A
annie_wangli 已提交
4366 4367 4368 4369


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

A
annie_wangli 已提交
4370
getResultSet(deviceId: string, keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4371 4372 4373

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

A
annie_wangli 已提交
4374
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4375

A
annie_wangli 已提交
4376 4377 4378
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4379
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4380 4381
| deviceId  |string  | Yes   |ID of the target device.   |
| keyPrefix |string  | Yes   |Key prefix to match.   |
A
annie_wangli 已提交
4382

A
annie_wangli 已提交
4383
**Return value**
A
annie_wangli 已提交
4384

A
annie_wangli 已提交
4385
| Type   | Description      |
A
annie_wangli 已提交
4386
| ------  | -------   |
Z
zengyawen 已提交
4387
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4388

A
annie_wangli 已提交
4389
**Example**
A
annie_wangli 已提交
4390

A
annie_wangli 已提交
4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409
```
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);
}
```
A
annie_wangli 已提交
4410 4411 4412 4413


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

A
annie_wangli 已提交
4414
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4415 4416 4417

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

A
annie_wangli 已提交
4418
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4419

A
annie_wangli 已提交
4420 4421 4422
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4423
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4424 4425
| 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 已提交
4426

A
annie_wangli 已提交
4427
**Example**
A
annie_wangli 已提交
4428

A
annie_wangli 已提交
4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440
```
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'
A
annie_wangli 已提交
4441 4442
            }
        }
A
annie_wangli 已提交
4443
        entries.push(entry);
A
annie_wangli 已提交
4444
    }
A
annie_wangli 已提交
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
    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);
}
```
A
annie_wangli 已提交
4462 4463 4464 4465


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

A
annie_wangli 已提交
4466
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4467 4468 4469

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

A
annie_wangli 已提交
4470
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4471

A
annie_wangli 已提交
4472 4473 4474
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4475
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4476
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4477

A
annie_wangli 已提交
4478
**Return value**
A
annie_wangli 已提交
4479

A
annie_wangli 已提交
4480
| Type   | Description      |
A
annie_wangli 已提交
4481
| ------  | -------   |
Z
zengyawen 已提交
4482
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4483

A
annie_wangli 已提交
4484
**Example**
A
annie_wangli 已提交
4485

A
annie_wangli 已提交
4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497
```
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'
A
annie_wangli 已提交
4498 4499
            }
        }
A
annie_wangli 已提交
4500
        entries.push(entry);
A
annie_wangli 已提交
4501
    }
A
annie_wangli 已提交
4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
    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);
}
```
A
annie_wangli 已提交
4526 4527 4528 4529


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

A
annie_wangli 已提交
4530
getResultSet(deviceId: string, query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
A
annie_wangli 已提交
4531 4532 4533

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

A
annie_wangli 已提交
4534
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4535

A
annie_wangli 已提交
4536 4537 4538
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4539
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4540 4541 4542
| deviceId  |string  | Yes   |ID of the target device.   |
| 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 已提交
4543

A
annie_wangli 已提交
4544
**Example**
A
annie_wangli 已提交
4545

A
annie_wangli 已提交
4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557
```
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'
A
annie_wangli 已提交
4558 4559
            }
        }
A
annie_wangli 已提交
4560
        entries.push(entry);
A
annie_wangli 已提交
4561
    }
A
annie_wangli 已提交
4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577
    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);
}
```
A
annie_wangli 已提交
4578 4579 4580 4581


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

A
annie_wangli 已提交
4582
getResultSet(deviceId: string, query: Query): Promise&lt;KvStoreResultSet&gt;
A
annie_wangli 已提交
4583 4584 4585

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

A
annie_wangli 已提交
4586
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4587

A
annie_wangli 已提交
4588 4589 4590
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4591
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4592 4593
| deviceId  |string  | Yes   |ID of the target device.   |
| query  |[Query](#query8)  | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4594

A
annie_wangli 已提交
4595
**Return value**
A
annie_wangli 已提交
4596

A
annie_wangli 已提交
4597
| Type   | Description      |
A
annie_wangli 已提交
4598
| ------  | -------   |
Z
zengyawen 已提交
4599
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
A
annie_wangli 已提交
4600

A
annie_wangli 已提交
4601
**Example**
A
annie_wangli 已提交
4602

A
annie_wangli 已提交
4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614
```
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'
A
annie_wangli 已提交
4615 4616
            }
        }
A
annie_wangli 已提交
4617
        entries.push(entry);
A
annie_wangli 已提交
4618
    }
A
annie_wangli 已提交
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
    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);
}
```
A
annie_wangli 已提交
4644 4645 4646 4647


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

A
annie_wangli 已提交
4648
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
4649 4650 4651

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

A
annie_wangli 已提交
4652
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4653

A
annie_wangli 已提交
4654 4655 4656
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4657
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4658 4659
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
| callback   |AsyncCallback&lt;void&gt;                 | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
4660

A
annie_wangli 已提交
4661
**Example**
A
annie_wangli 已提交
4662

A
annie_wangli 已提交
4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678
```
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);
}
```
A
annie_wangli 已提交
4679 4680 4681 4682


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

A
annie_wangli 已提交
4683
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
A
annie_wangli 已提交
4684 4685 4686

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

A
annie_wangli 已提交
4687
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4688

A
annie_wangli 已提交
4689 4690 4691
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4692
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4693
| resultSet  |[KvStoreResultSet](#getresultset8)  | Yes   |**KvStoreResultSet** object to close.  |
A
annie_wangli 已提交
4694

A
annie_wangli 已提交
4695
**Return value**
A
annie_wangli 已提交
4696

A
annie_wangli 已提交
4697
| Type   | Description      |
A
annie_wangli 已提交
4698 4699 4700
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
4701
**Example**
A
annie_wangli 已提交
4702

A
annie_wangli 已提交
4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716
```
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);
}
```
A
annie_wangli 已提交
4717 4718 4719 4720


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

A
annie_wangli 已提交
4721
getResultSize(query: Query, callback: AsyncCallback&lt;number&gt;): void
A
annie_wangli 已提交
4722 4723 4724

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

A
annie_wangli 已提交
4725
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4726

A
annie_wangli 已提交
4727 4728 4729
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4730
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4731 4732
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback used to return the number of results obtained.   |
A
annie_wangli 已提交
4733

A
annie_wangli 已提交
4734
**Example**
A
annie_wangli 已提交
4735

A
annie_wangli 已提交
4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746
```
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'
A
annie_wangli 已提交
4747 4748
            }
        }
A
annie_wangli 已提交
4749
        entries.push(entry);
A
annie_wangli 已提交
4750
    }
A
annie_wangli 已提交
4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763
    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);
}
```
A
annie_wangli 已提交
4764 4765 4766 4767


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

A
annie_wangli 已提交
4768
getResultSize(query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
4769 4770 4771

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

A
annie_wangli 已提交
4772
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4773

A
annie_wangli 已提交
4774 4775 4776
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4777
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4778
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4779

A
annie_wangli 已提交
4780
**Return value**
A
annie_wangli 已提交
4781

A
annie_wangli 已提交
4782
| Type   | Description      |
A
annie_wangli 已提交
4783
| ------  | -------   |
A
annie_wangli 已提交
4784
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
4785

A
annie_wangli 已提交
4786
**Example**
A
annie_wangli 已提交
4787

A
annie_wangli 已提交
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798
```
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'
A
annie_wangli 已提交
4799 4800
            }
        }
A
annie_wangli 已提交
4801
        entries.push(entry);
A
annie_wangli 已提交
4802
    }
A
annie_wangli 已提交
4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819
    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);
}
```
A
annie_wangli 已提交
4820 4821 4822 4823 4824 4825 4826 4827


### 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.

A
annie_wangli 已提交
4828
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4829

A
annie_wangli 已提交
4830 4831 4832
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4833
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4834 4835 4836
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
| callback  |AsyncCallback&lt;number&gt;  | Yes   |Callback used to return the number of results obtained.   |
A
annie_wangli 已提交
4837

A
annie_wangli 已提交
4838
**Example**
A
annie_wangli 已提交
4839

A
annie_wangli 已提交
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850
```
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'
A
annie_wangli 已提交
4851 4852
            }
        }
A
annie_wangli 已提交
4853
        entries.push(entry);
A
annie_wangli 已提交
4854
    }
A
annie_wangli 已提交
4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866
    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);
}
```
A
annie_wangli 已提交
4867 4868 4869 4870


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

A
annie_wangli 已提交
4871
getResultSize(deviceId: string, query: Query): Promise&lt;number&gt;
A
annie_wangli 已提交
4872 4873 4874

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

A
annie_wangli 已提交
4875
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4876

A
annie_wangli 已提交
4877 4878 4879
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4880
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4881 4882
| deviceId  |string                       | Yes   |ID of the target device.   |
| query     |[Query](#query8)       | Yes   |**Query** object to match.   |
A
annie_wangli 已提交
4883

A
annie_wangli 已提交
4884
**Return value**
A
annie_wangli 已提交
4885

A
annie_wangli 已提交
4886
| Type   | Description      |
A
annie_wangli 已提交
4887
| ------  | -------   |
A
annie_wangli 已提交
4888
|Promise&lt;number&gt; |Promise used to return the number of results obtained.|
A
annie_wangli 已提交
4889

A
annie_wangli 已提交
4890
**Example**
A
annie_wangli 已提交
4891

A
annie_wangli 已提交
4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902
```
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'
A
annie_wangli 已提交
4903 4904
            }
        }
A
annie_wangli 已提交
4905
        entries.push(entry);
A
annie_wangli 已提交
4906
    }
A
annie_wangli 已提交
4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922
    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);
}
```
A
annie_wangli 已提交
4923 4924 4925 4926


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

A
annie_wangli 已提交
4927
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
A
annie_wangli 已提交
4928

A
annie_wangli 已提交
4929
Removes data of a device from this KV store. This method uses an asynchronous callback to return the result.
A
annie_wangli 已提交
4930

A
annie_wangli 已提交
4931
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4932

A
annie_wangli 已提交
4933 4934 4935
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4936
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4937 4938
| deviceId  |string                       | Yes   |ID of the target device. |
| callback  |AsyncCallback&lt;void&gt;    | Yes   |Callback used to return the result.   |
A
annie_wangli 已提交
4939

A
annie_wangli 已提交
4940
**Example**
A
annie_wangli 已提交
4941

A
annie_wangli 已提交
4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958
```
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');
                });
            }
A
annie_wangli 已提交
4959
        });
A
annie_wangli 已提交
4960 4961 4962 4963 4964
    });
}catch(e) {
    console.log('RemoveDeviceData e ' + e);
}
```
A
annie_wangli 已提交
4965 4966 4967 4968


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

A
annie_wangli 已提交
4969
removeDeviceData(deviceId: string): Promise&lt;void&gt;
A
annie_wangli 已提交
4970

A
annie_wangli 已提交
4971
Removes data of a device from this KV store. This method uses a promise to return the result.
A
annie_wangli 已提交
4972

A
annie_wangli 已提交
4973
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
A
annie_wangli 已提交
4974

A
annie_wangli 已提交
4975 4976 4977
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
4978
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
4979
| deviceId  |string  | Yes   |ID of the target device. |
A
annie_wangli 已提交
4980

A
annie_wangli 已提交
4981
**Return value**
A
annie_wangli 已提交
4982

A
annie_wangli 已提交
4983
| Type   | Description      |
A
annie_wangli 已提交
4984 4985 4986
| ------  | -------   |
|Promise&lt;void&gt; |Promise used to return the result.|

A
annie_wangli 已提交
4987
**Example**
A
annie_wangli 已提交
4988

A
annie_wangli 已提交
4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
```
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);
}
```
A
annie_wangli 已提交
5014 5015 5016 5017


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

A
annie_wangli 已提交
5018
sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void
A
annie_wangli 已提交
5019 5020

Manually triggers KV store synchronization synchronously.
A
annie_wangli 已提交
5021
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
A
annie_wangli 已提交
5022

A
annie_wangli 已提交
5023
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5024

A
annie_wangli 已提交
5025 5026 5027
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5028
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5029 5030 5031
| deviceIdList    |string[]               | Yes   |IDs of the devices to be synchronized.|
| 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 已提交
5032

A
annie_wangli 已提交
5033
**Example**
A
annie_wangli 已提交
5034

A
annie_wangli 已提交
5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052
```
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);
}
```
A
annie_wangli 已提交
5053 5054 5055

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

A
annie_wangli 已提交
5056
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
5057 5058 5059

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

A
annie_wangli 已提交
5060
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5061

A
annie_wangli 已提交
5062 5063 5064
**Parameters**

| Name | Type| Mandatory | Description                   |
A
annie_wangli 已提交
5065
| -----  | ------   | ----  | ----------------------- |
A
annie_wangli 已提交
5066
| event    |'syncComplete'      | Yes   |Event triggered when the synchronization is complete.|
A
annie_wangli 已提交
5067
| syncCallback            |Callback<Array&lt;<[string, number]&gt; | Yes   |Callback used to return the synchronization result. |
A
annie_wangli 已提交
5068

A
annie_wangli 已提交
5069
**Example**
A
annie_wangli 已提交
5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086

```
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);
}
```
A
annie_wangli 已提交
5087 5088 5089 5090


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

A
annie_wangli 已提交
5091
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
A
annie_wangli 已提交
5092 5093 5094

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

A
annie_wangli 已提交
5095
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
A
annie_wangli 已提交
5096

A
annie_wangli 已提交
5097 5098 5099
**Parameters**

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

A
annie_wangli 已提交
5104
**Example**
A
annie_wangli 已提交
5105

A
annie_wangli 已提交
5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117
```
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);
}
```
A
annie_wangli 已提交
5118 5119 5120 5121 5122


## SyncMode

Defines the synchronization mode.
Z
zengyawen 已提交
5123

A
annie_wangli 已提交
5124 5125 5126
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core

| Name      | Default Value    | Description                   |
A
annie_wangli 已提交
5127 5128 5129 5130
| -----      | ------    | ----------------------- |
| 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.|