提交 0f961ad4 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 7d18c4c3
# Distributed Data Management
Distributed data management provides collaboration between databases of different devices for applications. The APIs provided by distributed data management can be used to save data to the distributed database and perform operations such as adding, deleting, modifying, and querying data in the distributed database.
The distributed data management module implements collaboration between databases of different devices for applications. The APIs provided by distributed data management can be used to save data to distributed databases and perform operations such as adding, deleting, modifying, querying, and synchronizing data in distributed databases.
This module provides the following functions:
- [KVManager](#kvmanager): provides a **KVManager** instance to manage key-value (KV) stores.
- [KvStoreResultSet<sup>8+</sup>](#kvstoreresultset8): provides methods to obtain the KV store result set and query or move the data read position.
- [Query<sup>8+</sup>](#query8): provides methods to query data from the database through a **Query** instance by using predicates.
- [KVStore](#kvstore): provides methods to add data, delete data, and observe data changes and data synchronization through a **KVStore** instance.
- [SingleKVStore](#singlekvstore): provides methods to query and synchronize data in a single KV store. This class inherits from [KVStore](#kvstore), and data is not distinguished by device.
- [DeviceKVStore<sup>8+</sup> ](#devicekvstore8): provides methods to query and synchronize data in a device KV store. This class inherits from [KVStore](#kvstore), and data is distinguished by device.
>**NOTE**<br/>
>
......@@ -18,7 +27,7 @@ import distributedData from '@ohos.data.distributedData';
createKVManager(config: KVManagerConfig, callback: AsyncCallback&lt;KVManager&gt;): void
Creates a **KVManager** object to manage key-value (KV) stores. This API uses an asynchronous callback to return the result.
Creates a **KVManager** instance to manage KV stores. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -26,8 +35,8 @@ Creates a **KVManager** object to manage key-value (KV) stores. This API uses an
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| config | [KVManagerConfig](#kvmanagerconfig) | Yes | Configuration of the **KVManager** object, including the bundle name and user information of the caller.|
| callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | Yes | Callback invoked to return the **KVManager** object created.|
| config | [KVManagerConfig](#kvmanagerconfig) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
| callback | AsyncCallback&lt;[KVManager](#kvmanager)&gt; | Yes | Callback invoked to return the **KVManager** instance created.|
**Example**
```js
......@@ -57,7 +66,7 @@ try {
createKVManager(config: KVManagerConfig): Promise&lt;KVManager&gt;
Creates a **KVManager** object to manage KV stores. This API uses a promise to return the result.
Creates a **KVManager** instance to manage KV stores. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -65,13 +74,13 @@ Creates a **KVManager** object to manage KV stores. This API uses a promise to r
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| config |[KVManagerConfig](#kvmanager) | Yes | Configuration of the **KVManager** object, including the bundle name and user information of the caller.|
| config |[KVManagerConfig](#kvmanager) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise&lt;[KVManager](#kvmanager)&gt; | Promise used to return the **KVManager** object created.|
| Promise&lt;[KVManager](#kvmanager)&gt; | Promise used to return the **KVManager** instance created.|
**Example**
......@@ -104,6 +113,7 @@ Provides configuration of the **KVManager** object, including the bundle name an
| Name| Type| Mandatory| Description|
| ----- | ------ | ------ | ------ |
| context | Context | Yes| Context of the application or function.<br>See [Context](js-apis-Context.md) for versions earlier than API version 9.<br>See [Context](js-apis-ability-context.md) for API version 9 or later.|
| userInfo | [UserInfo](#userinfo) | Yes | User information.|
| bundleName | string | Yes | Bundle name.|
......@@ -121,11 +131,11 @@ Defines user information.
## UserType
Defines the user type.
Enumerates the user types.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name| Default Value| Description|
| Name| Value| Description|
| ----- | ------ | ------ |
| SAME_USER_ID | 0 | User who logs in to different devices using the same account.|
......@@ -198,7 +208,7 @@ Creates and obtains a KV store. This API uses a promise to return the result.
| Type | Description |
| -------------------------------------- | ------------------------ |
| Promise&lt;T&gt; &lt;T extends [KVStore](#kvstore)&gt; | Promise used to return the KV store created.|
| Promise&lt;T&gt;, &lt;T extends [KVStore](#kvstore)&gt; | Promise used to return the KV store created.|
**Example**
......@@ -241,7 +251,7 @@ Closes a KV store. This API uses an asynchronous callback to return the result.
| 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 [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. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result.|
**Example**
......@@ -261,7 +271,7 @@ const options = {
kvManager.getKVStore('storeId', options, async function (err, store) {
console.log('getKVStore success');
kvStore = store;
await kvManager.closeKVStore('appId', 'storeId', kvStore, function (err, data) {
kvManager.closeKVStore('appId', 'storeId', kvStore, function (err, data) {
console.log('closeKVStore success');
});
});
......@@ -291,7 +301,7 @@ Closes a KV store. This API uses a promise to return the result.
| Type | Description |
| ------------- | -------------- |
| Promise\<void> | Promise used to return the result. If the KV store is closed, **true** will be returned. Otherwise, **false** will be returned.|
| Promise\<void> | Promise that returns no value.|
**Example**
......@@ -311,7 +321,7 @@ const options = {
kvManager.getKVStore('storeId', options).then(async (store) => {
console.log('getKVStore success');
kvStore = store;
await kvManager.closeKVStore('appId', 'storeId', kvStore).then(() => {
kvManager.closeKVStore('appId', 'storeId', kvStore).then(() => {
console.log('closeKVStore success');
}).catch((err) => {
console.log('closeKVStore err ' + JSON.stringify(err));
......@@ -339,7 +349,7 @@ Deletes a KV store. This API uses an asynchronous callback to return the result.
| ----- | ------ | ---- | ----------------------- |
| 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 [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. |
| callback | AsyncCallback&lt;void&gt; | Yes | Callback used to return the result.|
**Example**
......@@ -359,7 +369,7 @@ try {
kvManager.getKVStore('store', options, async function (err, store) {
console.log('getKVStore success');
kvStore = store;
await kvManager.deleteKVStore('appId', 'storeId', function (err, data) {
kvManager.deleteKVStore('appId', 'storeId', function (err, data) {
console.log('deleteKVStore success');
});
});
......@@ -388,7 +398,7 @@ Deletes a KV store. This API uses a promise to return the result.
| Type | Description |
| ------------- | -------------- |
| Promise&lt;void&gt; | Promise used to return the result. If the KV store is deleted, **true** will be returned. Otherwise, **false** will be returned.|
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
......@@ -408,7 +418,7 @@ try {
kvManager.getKVStore('storeId', options).then(async (store) => {
console.log('getKVStore success');
kvStore = store;
await kvManager.deleteKVStore('appId', 'storeId').then(() => {
kvManager.deleteKVStore('appId', 'storeId').then(() => {
console.log('deleteKVStore success');
}).catch((err) => {
console.log('deleteKVStore err ' + JSON.stringify(err));
......@@ -426,7 +436,7 @@ try {
getAllKVStoreId(appId: string, callback: AsyncCallback&lt;string[]&gt;): void
Obtains the IDs of all the KV stores that are created using [getKVStore](#getkvstore) and have not been deleted using [deleteKVStore](#deletekvstore8). This API uses an asynchronous callback to return the result.
Obtains the IDs of all KV stores that are created by [getKVStore()](#getkvstore) and have not been deleted by [deleteKVStore()](#deletekvstore8). This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -435,7 +445,7 @@ Obtains the IDs of all the KV stores that are created using [getKVStore](#getkvs
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| 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. |
| callback | AsyncCallback&lt;string[]&gt; | Yes |Callback used to return the KV store IDs obtained. |
**Example**
......@@ -456,7 +466,7 @@ try {
getAllKVStoreId(appId: string): Promise&lt;string[]&gt;
Obtains the IDs of all the KV stores that are created using [getKVStore](#getkvstore) and have not been deleted using [deleteKVStore](#deletekvstore8). This API uses a promise to return the result.
Obtains the IDs of all KV stores that are created by [getKVStore()](#getkvstore) and have not been deleted by [deleteKVStore()](#deletekvstore8). This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -495,7 +505,7 @@ try {
on(event: 'distributedDataServiceDie', deathCallback: Callback&lt;void&gt;): void
Subscribes to the **distributedDataServiceDie** events. This API uses a synchronous callback to return the result.
Subscribes to service status changes.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -503,8 +513,8 @@ Subscribes to the **distributedDataServiceDie** events. This API uses a synchron
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event | string | Yes | Name of the event to subscribe to. The value is **distributedDataServiceDie**, which indicates the service status change event.|
| deathCallback | Callback&lt;void&gt; | Yes | Callback invoked when the distributed data service is dead. |
| event | string | Yes | Event to subscribe to. The value is **distributedDataServiceDie**, which indicates service status changes.|
| deathCallback | Callback&lt;void&gt; | Yes | Callback invoked to return service status changes.|
**Example**
......@@ -527,7 +537,7 @@ try {
off(event: 'distributedDataServiceDie', deathCallback?: Callback&lt;void&gt;): void
Unsubscribes from the **distributedDataServiceDie** events. This API uses a synchronous callback to return the result.
Unsubscribes from the service status changes.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -535,8 +545,8 @@ Unsubscribes from the **distributedDataServiceDie** events. This API uses a sync
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event | string | Yes | Name of the event to unsubscribe from. The value is **distributedDataServiceDie**, which indicates the service status change event.|
| deathCallback | Callback&lt;void&gt; | No | Callback used to return the **distributedDataServiceDie** events. |
| event | string | Yes | Event to unsubscribe from. The value is **distributedDataServiceDie**, which indicates service status changes.|
| deathCallback | Callback&lt;void&gt; | No | Callback used to return service status changes.|
**Example**
......@@ -566,7 +576,7 @@ Provides KV store configuration.
| 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.<br>**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC |
| autoSync | boolean | No|Whether database files are automatically synchronized. By default, database files are not automatically synchronized.<br>**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC |
| kvStoreType | [KVStoreType](#kvstoretype) | No|Type of the KV store to create. By default, a device KV store is created. The device KV store stores data for multiple devices that collaborate with each other.|
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set. |
| schema<sup>8+</sup> | [Schema](#schema8) | No| Schema used to define the values stored in a KV store.|
......@@ -574,30 +584,30 @@ Provides KV store configuration.
## KVStoreType
Defines the KV store types.
Enumerates the KV store types.
| Name | Default Value| Description |
| Name | Value| Description |
| --- | ---- | ----------------------- |
| DEVICE_COLLABORATION | 0 | Device KV store. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore |
| SINGLE_VERSION | 1 | Single KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| MULTI_VERSION | 2 | Multi-version KV store. This type is not supported currently. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore|
| DEVICE_COLLABORATION | 0 | Device KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore |
| SINGLE_VERSION | 1 | Single KV store.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core|
| MULTI_VERSION | 2 | Multi-version KV store. This type is not supported currently.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore|
## SecurityLevel
Defines the KV store security levels.
Enumerates the KV store security levels.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Default Value| Description |
| Name | Value| Description |
| --- | ---- | ----------------------- |
| NO_LEVEL | 0 | No security level is set for the KV store. |
| S0 | 1 | The KV store security level is public. |
| S1 | 2 | The KV store security level is low. If data leakage occurs, minor impact will be caused on the database. |
| S2 | 3 | The KV store security level is medium. If data leakage occurs, moderate impact will be caused on the database. |
| S3 | 5 | The KV store security level is high. If data leakage occurs, major impact will be caused on the database. |
| S4 | 6 | The KV store security level is critical. If data leakage occurs, severe impact will be caused on the database. |
| 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. For example, a KV store that contains system data such as wallpapers.|
| S2 | 3 | The KV store security level is medium. If data leakage occurs, moderate impact will be caused on the database. For example, a KV store that contains information created by users or call records, such as audio or video clips.|
| S3 | 5 | The KV store security level is high. If data leakage occurs, major impact will be caused on the database. For example, a KV store that contains information such as user fitness, health, and location data.|
| S4 | 6 | The KV store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, a KV store that contains information such as authentication credentials and financial data.|
## Constants
......@@ -606,18 +616,18 @@ Defines the KV store constants.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Default Value| Description |
| Name | Value| Description |
| --- | ---- | ----------------------- |
| MAX_KEY_LENGTH | 1024 | Maximum length (in bytes) of a key in the KV store. |
| MAX_VALUE_LENGTH | 4194303 | Maximum length (in bytes) of a value in the KV store. |
| MAX_KEY_LENGTH_DEVICE | 896 | Maximum length of the device key, in bytes.|
| MAX_STORE_ID_LENGTH | 128 | Maximum length (in bytes) of a KV store ID. |
| MAX_KEY_LENGTH | 1024 | Maximum length of a key in the KV store, in bytes. |
| MAX_VALUE_LENGTH | 4194303 | Maximum length of a value in the KV store, in bytes. |
| MAX_KEY_LENGTH_DEVICE | 896 | Maximum length of a device key, in bytes.|
| MAX_STORE_ID_LENGTH | 128 | Maximum length of a KV store ID, in bytes. |
| MAX_QUERY_LENGTH | 512000 | Maximum query length, in bytes.|
| MAX_BATCH_SIZE | 128 | Maximum number of batch operations.|
## Schema<sup>8+</sup> ##
Defines the schema of a KV store. When creating or opening a KV store, you can create a **Schema** object and put it in [Options](#options).
Defines the schema of a KV store. You can create a **Schema** object and place it in [Options](#options) when creating or opening a KV store.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -646,7 +656,7 @@ Represents a **Schema** instance, which provides the methods for defining the va
| --- | ---- | ----------------------- |
| 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.|
| type<sup>8+</sup> | number | Value of the data type corresponding to the specified node.|
### constructor<sup>8+</sup> ###
......@@ -694,7 +704,7 @@ try {
node.appendChild(child1);
node.appendChild(child2);
node.appendChild(child3);
console.log("appendNode " + node.toJson());
console.log("appendNode " + JSON.stringify(node));
child1 = null;
child2 = null;
child3 = null;
......@@ -707,14 +717,16 @@ try {
## KvStoreResultSet<sup>8+</sup> ##
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.
Provides methods to obtain the KV store result sets, and query and move the data read position.
Before calling any method in **KvStoreResultSet**, you must use [getKVStore](#getkvstore) to obtain a **KVStore** object.
### getCount<sup>8+</sup> ###
getCount(): number
Obtains the number of rows in the result set.
Obtains the total number of rows in the result set.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -722,7 +734,7 @@ Obtains the number of rows in the result set.
| Type | Description |
| ------ | -------------- |
| number |Number of rows obtained. |
| number |Total number of rows obtained. |
**Example**
......@@ -734,7 +746,7 @@ try {
console.log('getResultSet succeed.');
resultSet = result;
}).catch((err) => {
console.log('getResultSet failed:' + err);
console.log('getResultSet failed: ' + err);
});
const count = resultSet.getCount();
console.log("getCount succeed:" + count);
......@@ -767,7 +779,7 @@ try {
console.log('getResultSet succeeed.');
resultSet = result;
}).catch((err) => {
console.log('getResultSet failed:' + err);
console.log('getResultSet failed: ' + err);
});
const position = resultSet.getPosition();
console.log("getPosition succeed:" + position);
......@@ -781,7 +793,7 @@ try {
moveToFirst(): boolean
Moves the data read position to the first row.
Moves the data read position to the first row. If the result set is empty, **false** will be returned.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -815,7 +827,7 @@ try {
moveToLast(): boolean
Moves the data read position to the last row.
Moves the data read position to the last row. If the result set is empty, **false** will be returned.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -849,7 +861,7 @@ try {
moveToNext(): boolean
Moves the data read position to the next row.
Moves the data read position to the next row. If the result set is empty, **false** will be returned.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -883,7 +895,7 @@ try {
moveToPrevious(): boolean
Moves the data read position to the previous row.
Moves the data read position to the previous row. If the result set is empty, **false** will be returned.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -1005,7 +1017,7 @@ Checks whether the data read position is the first row.
| Type | Description |
| ------ | -------------- |
| boolean |Returns **true** if the data read position is the first row; returns **false** otherwise. |
| boolean |Returns **true** if the first row is being read; returns **false** otherwise. |
**Example**
......@@ -1039,7 +1051,7 @@ Checks whether the data read position is the last row.
| Type | Description |
| ------ | -------------- |
| boolean |Returns **true** if the data read position is the last row; returns **false** otherwise. |
| boolean |Returns **true** if the last row is being read; returns **false** otherwise. |
**Example**
......@@ -1072,7 +1084,7 @@ Checks whether the data read position is before the first row.
| Type | Description |
| ------ | -------------- |
| boolean |Returns **true** if the read position is before the first row; returns **false** otherwise. |
| boolean |Returns **true** if the data read position is before the first row; returns **false** otherwise. |
**Example**
......@@ -1132,7 +1144,7 @@ try {
getEntry(): Entry
Obtains a KV pair.
Obtains the KV pair from the current position.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -1181,7 +1193,7 @@ A constructor used to create a **Schema** instance.
reset(): Query
Resets the **Query** object that contains common query options.
Resets the **Query** object.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -1212,7 +1224,7 @@ try {
equalTo(field: string, value: number|string|boolean): Query
Creates a **Query** object to match the specified field whose value is equal to the specified value.
Creates a **Query** object to match the specified field whose value is equal to the given value.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -1227,7 +1239,7 @@ Creates a **Query** object to match the specified field whose value is equal to
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object reset.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1262,7 +1274,7 @@ Creates a **Query** object to match the specified field whose value is not equal
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1297,7 +1309,7 @@ Creates a **Query** object to match the specified field whose value is greater t
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1332,7 +1344,7 @@ Creates a **Query** object to match the specified field whose value is less than
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1367,7 +1379,7 @@ Creates a **Query** object to match the specified field whose value is greater t
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1402,7 +1414,7 @@ Creates a **Query** object to match the specified field whose value is less than
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1424,7 +1436,6 @@ isNull(field: string): Query
Creates a **Query** object to match the specified field whose value is **null**.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
......@@ -1437,7 +1448,7 @@ Creates a **Query** object to match the specified field whose value is **null**.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1473,7 +1484,7 @@ Creates a **Query** object to match the specified field whose value is within th
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1508,7 +1519,7 @@ Creates a **Query** object to match the specified field whose value is within th
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1543,7 +1554,7 @@ Creates a **Query** object to match the specified field whose value is not withi
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1578,7 +1589,7 @@ Creates a **Query** object to match the specified field whose value is not withi
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1606,14 +1617,14 @@ Creates a **Query** object to match the specified field whose value is similar t
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| fieId | string | Yes Field to match. It cannot contain '^'. |
| fieId | string | Yes |Field to match. It cannot contain '^'. |
| value | string | Yes | String specified.|
**Return value**
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1648,7 +1659,7 @@ Creates a **Query** object to match the specified field whose value is not simil
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1742,7 +1753,7 @@ Creates a **Query** object to sort the query results in ascending order.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1777,7 +1788,7 @@ Creates a **Query** object to sort the query results in descending order.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1813,7 +1824,7 @@ Creates a **Query** object to specify the number of results and where to start.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1834,7 +1845,7 @@ try {
isNotNull(field: string): Query
Creates a **Query** object with a specified field that is not null.
Creates a **Query** object to match the specified field whose value is not **null**.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -1842,13 +1853,13 @@ Creates a **Query** object with a specified field that is not null.
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| fieId | string | Yes |Field specified. |
| fieId | string | Yes |Field to match. It cannot contain '^'. |
**Return value**
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1876,7 +1887,7 @@ Creates a **Query** object for a query condition group with a left parenthesis.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1906,7 +1917,7 @@ Creates a **Query** object for a query condition group with a right parenthesis.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1942,7 +1953,7 @@ Creates a **Query** object with a specified key prefix.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -1977,7 +1988,7 @@ Creates a **Query** object with an index preferentially used for query.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -2013,7 +2024,7 @@ Creates a **Query** object with the device ID as the key prefix.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object Created.|
| [Query](#query8) |**Query** object.|
**Example**
......@@ -2032,7 +2043,7 @@ try {
getSqlLike():string
Obtains the query statement of this **Query** object.
Obtains the query statement of the **Query** object.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2040,7 +2051,7 @@ Obtains the query statement of this **Query** object.
| Type | Description |
| ------ | ------- |
| string |Query statement obtained.|
| string |Returns the query statement obtained.|
**Example**
......@@ -2057,7 +2068,9 @@ try {
## KVStore
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](#getkvstore) to obtain a **KVStore** object.
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](#getkvstore) to obtain a **KVStore** object.
### put
......@@ -2114,7 +2127,7 @@ Adds a KV pair of the specified type to this KV store. This API uses a promise t
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2133,7 +2146,6 @@ try {
}
```
### delete
delete(key: string, callback: AsyncCallback&lt;void&gt;): void
......@@ -2175,7 +2187,6 @@ try {
}
```
### delete
delete(key: string): Promise&lt;void&gt;
......@@ -2194,7 +2205,7 @@ Deletes a KV pair from this KV store. This API uses a promise to return the resu
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2218,12 +2229,91 @@ try {
}
```
### delete<sup>9+</sup>
delete(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;void&gt;): void
Deletes KV pairs that meet the specified predicates. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| predicates | Predicates | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| callback | AsyncCallback&lt;void&gt; | Yes |Callback used to return the result. |
**Example**
```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
let predicates = new dataSharePredicates.DataSharePredicates();
kvStore.delete(predicates, function (err, data) {
if (err == undefined) {
console.log('delete success');
} else {
console.log('delete fail' + err);
}
});
} catch (e) {
console.log('An unexpected error occurred. Error:' + e);
}
```
### delete<sup>9+</sup>
delete(predicates: dataSharePredicates.DataSharePredicates): Promise&lt;void&gt;
Deletes KV pairs that meet the specified predicates. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| predicates | Predicates | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
**Return value**
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
**Example**
```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
let predicates = new dataSharePredicates.DataSharePredicates();
let arr = ["name"];
predicates.inKeys(arr);
kvStore.put("name", "bob").then((data) => {
console.log('put success' + JSON.stringify(data));
kvStore.delete(predicates).then((data) => {
console.log('delete success');
}).catch((err) => {
console.log('delete fail' + JSON.stringify(err));
});
}) .catch((err) => {
console.log(' put fail' + err);
});
}catch (e) {
console.log("An unexpected error occurred. Error:" + e);
}
```
### on('dataChange')
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;ChangeNotification&gt;): void
Subscribes to data changes of the specified type. This API uses a synchronous callback to return the result.
Subscribes to data change notifications of the specified type.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2231,9 +2321,9 @@ Subscribes to data changes of the specified type. This API uses a synchronous ca
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to subscribe to. The value is **dataChange**, which indicates the data change event. |
| event |string | Yes |Event to subscribe to. The value is **dataChange**, which indicates data changes. |
| type |[SubscribeType](#subscribetype) | Yes |Type of data changes. |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes |Callback used to return the result.|
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes |Callback used to return the data changes.|
**Example**
......@@ -2249,7 +2339,7 @@ kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, fun
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Subscribes to data synchronization completion events. This API uses a synchronous callback to return the result.
Subscribes to data synchronization complete events.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2257,8 +2347,8 @@ Subscribes to data synchronization completion events. This API uses a synchronou
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to subscribe to. The value is **syncComplete**, which indicates the synchronization complete event. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes |Callback used to return the result. |
| event |string | Yes |Event to subscribe to. The value is **syncComplete**, which indicates completion of a data synchronization. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes |Callback used to return the data synchronization result. |
**Example**
......@@ -2273,7 +2363,7 @@ kvStore.on('syncComplete', function (data) {
off(event:'dataChange', observer?: Callback&lt;ChangeNotification&gt;): void
Unsubscribes from data change events. This API uses a synchronous callback to return the result.
Unsubscribes from data change notifications.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2281,8 +2371,8 @@ Unsubscribes from data change events. This API uses a synchronous callback to re
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to unsubscribe from. The value is **dataChange**, which indicates the data change event. |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; |No |Callback used to return the result.|
| event |string | Yes |Event to unsubscribe from. The value is **dataChange**, which indicates data changes. |
| observer |Callback&lt;[ChangeNotification](#changenotification)&gt; |No |Callback used to return the data changes.|
**Example**
......@@ -2296,12 +2386,42 @@ kvStore.off('dataChange', function (data) {
});
```
### off('syncComplete')<sup>9+</sup>
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Unsubscribes from data change events. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Event to unsubscribe from. The value is **syncComplete**, which indicates completion of a data synchronization. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | No |Callback used to return the synchronization result. |
**Example**
```js
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);
}
```
### putBatch<sup>8+</sup>
putBatch(entries: Entry[], callback: AsyncCallback&lt;void&gt;): void
Inserts KV pairs in batches to this KV store. This API uses an asynchronous callback to return the result.
Inserts KV pairs to this KV store in batches. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2332,14 +2452,14 @@ try {
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) {
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);
console.log('PutBatch e ' + JSON.stringify(e));
}
```
......@@ -2348,7 +2468,7 @@ try {
putBatch(entries: Entry[]): Promise&lt;void&gt;
Inserts KV pairs in batches to this KV store. This API uses a promise to return the result.
Inserts KV pairs to this KV store in batches. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2362,7 +2482,7 @@ Inserts KV pairs in batches to this KV store. This API uses a promise to return
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2384,7 +2504,7 @@ try {
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) => {
kvStore.getEntries('batch_test_string_key').then((entrys) => {
console.log('getEntries success');
console.log('PutBatch ' + JSON.stringify(entries));
}).catch((err) => {
......@@ -2394,16 +2514,99 @@ try {
console.log('putBatch fail ' + JSON.stringify(err));
});
}catch(e) {
console.log('PutBatch e ' + e);
console.log('PutBatch e ' + JSON.stringify(e));
}
```
### putBatch<sup>9+</sup>
putBatch(value: Array&lt;ValuesBucket&gt;, callback: AsyncCallback&lt;void&gt;): void
Writes values to this KV store. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| value |Array[&lt;ValuesBucket&gt;]()[] | Yes |Values to write. |
| callback |Asyncallback&lt;void&gt; |Yes |Callback used to return the result.|
**Example**
```js
let kvStore;
try {
let v8Arr = [];
let arr = new Uint8Array([4,5,6,7]);
let vb1 = {key : "name_1", value : 32}
let vb2 = {key : "name_2", value : arr};
let vb3 = {key : "name_3", value : "lisi"};
v8Arr.push(vb1);
v8Arr.push(vb2);
v8Arr.push(vb3);
kvStore.putBatch(v8Arr, async function (err,data) {
console.log('putBatch success');
}).catch((err) => {
console.log('putBatch fail ' + JSON.stringify(err));
});
}catch(e) {
console.log('putBatch e ' + JSON.stringify(e));
}
```
### putBatch<sup>9+</sup>
putBatch(value: Array&lt;ValuesBucket&gt;): Promise&lt;void&gt;
Writes values of the **valuesbucket** type to this KV store. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| value |Array&lt;[ValuesBucket&gt;](#)[] | Yes |Values to write. |
**Return value**
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
**Example**
```js
let kvStore;
try {
let v8Arr = [];
let arr = new Uint8Array([4,5,6,7]);
let vb1 = {key : "name_1", value : 32}
let vb2 = {key : "name_2", value : arr};
let vb3 = {key : "name_3", value : "lisi"};
v8Arr.push(vb1);
v8Arr.push(vb2);
v8Arr.push(vb3);
kvStore.putBatch(v8Arr).then(async (err) => {
console.log('putBatch success');
}).catch((err) => {
console.log('putBatch fail ' + JSON.stringify(err));
});
}catch(e) {
console.log('PutBatch e ' + JSON.stringify(e));
}
```
### deleteBatch<sup>8+</sup>
deleteBatch(keys: string[], callback: AsyncCallback&lt;void&gt;): void
Deletes KV pairs in batches from this KV store. This API uses an asynchronous callback to return the result.
Deletes KV pairs from this KV store in batches. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2436,7 +2639,7 @@ try {
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) {
kvStore.deleteBatch(keys, async function (err,data) {
console.log('deleteBatch success');
});
});
......@@ -2450,7 +2653,7 @@ try {
deleteBatch(keys: string[]): Promise&lt;void&gt;
Deletes KV pairs in batches from this KV store. This API uses a promise to return the result.
Deletes KV pairs from this KV store in batches. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2464,7 +2667,7 @@ Deletes KV pairs in batches from this KV store. This API uses a promise to retur
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2488,7 +2691,7 @@ try {
console.log('entries: ' + JSON.stringify(entries));
kvStore.putBatch(entries).then(async (err) => {
console.log('putBatch success');
await kvStore.deleteBatch(keys).then((err) => {
kvStore.deleteBatch(keys).then((err) => {
console.log('deleteBatch success');
}).catch((err) => {
console.log('deleteBatch fail ' + JSON.stringify(err));
......@@ -2544,7 +2747,7 @@ try {
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) {
kvStore.putBatch(entries, async function (err,data) {
console.log('putBatch success');
});
});
......@@ -2566,7 +2769,7 @@ Starts the transaction in this KV store. This API uses a promise to return the r
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2593,7 +2796,7 @@ try {
commit(callback: AsyncCallback&lt;void&gt;): void
Commits the transaction in this KV store. This API uses an asynchronous callback to return the result.
Submits the transaction in this KV store. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2625,7 +2828,7 @@ try {
commit(): Promise&lt;void&gt;
Commits the transaction in this KV store. This API uses a promise to return the result.
Submits the transaction in this KV store. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2633,7 +2836,7 @@ Commits the transaction in this KV store. This API uses a promise to return the
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2695,7 +2898,7 @@ Rolls back the transaction in this KV store. This API uses a promise to return t
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2717,7 +2920,7 @@ try {
enableSync(enabled: boolean, callback: AsyncCallback&lt;void&gt;): void
Sets data synchronization, which can be enabled or disable. This API uses an asynchronous callback to return the result.
Sets data synchronization, which can be enabled or disabled. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2750,7 +2953,7 @@ try {
enableSync(enabled: boolean): Promise&lt;void&gt;
Enables or disables data synchronization. This API uses a promise to return the result.
Sets data synchronization, which can be enabled or disabled. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2764,7 +2967,7 @@ Enables or disables data synchronization. This API uses a promise to return the
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2834,7 +3037,7 @@ Sets the data synchronization range. This API uses a promise to return the resul
| Type | Description |
| ------ | ------- |
| Promise&lt;void&gt; |Promise used to return the result.|
| Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -2856,11 +3059,11 @@ try {
## SubscribeType
Defines the subscription type.
Enumerates the subscription types.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Default Value | Description |
| Name | Value | Description |
| ----- | ------ | ----------------------- |
| SUBSCRIBE_TYPE_LOCAL |0 |Local data changes. |
| SUBSCRIBE_TYPE_REMOTE |1 |Remote data changes. |
......@@ -2893,24 +3096,22 @@ Defines the KV pairs stored in the KV store.
## Value
Defines the value in a KV pair.
Defines the **value** object in a KV store.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Type |Readable |Writable | Description |
| ----- | ------- | -----| ------|------------------------ |
| 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. |
| value | Uint8Array \| string \| number \| boolean| Yes | Yes|Value. |
## ValueType
Enumerates the types of values in KV pairs.
These value types can be used only by internal applications.
Enumerates the data types.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Default Value | Description |
| Name | Value | Description |
| ----- | ------ | ----------------------- |
| STRING |0 |String. |
| INTEGER |1 |Integer. |
......@@ -2922,13 +3123,17 @@ These value types can be used only by internal applications.
## SingleKVStore
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](#getkvstore) to obtain a **SingleKVStore** object.
Provides methods to query and synchronize data in a single KV store. This class inherits from [KVStore](#kvstore).
Data is not distinguished by device in a single KV store. The data written to different devices using the same key will be overwritten. For example, a single KV store can be used to synchronize a user's calendar and contact data between different devices.
Before calling any method in **SingleKVStore**, you must use [getKVStore](#getkvstore) to obtain a **SingleKVStore** instance.
### get
get(key: string, callback: AsyncCallback&lt;Uint8Array | string | boolean | number&gt;): void
Obtains the value of a specified key. This API uses an asynchronous callback to return the result.
Obtains the value of the specified key. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -2937,7 +3142,7 @@ Obtains the value of a specified key. This API uses an asynchronous callback to
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| key |string | Yes |Key of the value to obtain. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). |
| callback |AsyncCallback&lt;Uint8Array \| string \| boolean \| number&gt;) | Yes |Callback used to return the value obtained. |
| callback |AsyncCallback&lt;Uint8Array \| string \| boolean \| number&gt;) | Yes |Callback invoked to return the value obtained. |
**Example**
......@@ -2966,7 +3171,7 @@ try {
get(key: string): Promise&lt;Uint8Array | string | boolean | number&gt;
Obtains the value of a specified key. This API uses a promise to return the result.
Obtains the value of the specified key. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3009,7 +3214,7 @@ try {
getEntries(keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
Obtains the KV pairs that match the specified key prefix. This API uses an asynchronous callback to return the result.
Obtains all KV pairs that match the specified key prefix. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3018,7 +3223,7 @@ Obtains the KV pairs that match the specified key prefix. This API uses an async
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| keyPrefix |string | Yes |Key prefix to match. |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt; | Yes |Callback used to return the KV pairs obtained. |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt; | Yes |Callback invoked to return the KV pairs obtained. |
**Example**
......@@ -3039,7 +3244,7 @@ try {
}
kvStore.putBatch(entries, async function (err,data) {
console.log('putBatch success');
await kvStore.getEntries('batch_test_number_key', function (err,entrys) {
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]));
......@@ -3055,7 +3260,7 @@ try {
getEntries(keyPrefix: string): Promise&lt;Entry[]&gt;
Obtains the KV pairs that match the specified key prefix. This API uses a promise to return the result.
Obtains all KV pairs that match the specified key prefix. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3091,7 +3296,7 @@ try {
console.log('entries: ' + entries);
kvStore.putBatch(entries).then(async (err) => {
console.log('putBatch success');
await kvStore.getEntries('batch_test_string_key').then((entrys) => {
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]));
......@@ -3121,7 +3326,7 @@ Obtains the KV pairs that match the specified **Query** object. This API uses an
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| query |[Query](#query8) | Yes |**Query** object to match. |
| query |[Query](#query8) | Yes |Key prefix to match. |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt; | Yes |Callback used to return the KV pairs obtained. |
**Example**
......@@ -3147,7 +3352,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getEntries(query, function (err,entrys) {
kvStore.getEntries(query, function (err,entrys) {
console.log('getEntries success');
console.log('entrys.length: ' + entrys.length);
console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
......@@ -3183,6 +3388,7 @@ Obtains the KV pairs that match the specified **Query** object. This API uses a
**Example**
```js
let kvStore;
try {
var arr = new Uint8Array([21,31]);
let entries = [];
......@@ -3202,7 +3408,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getEntries(query).then((entrys) => {
kvStore.getEntries(query).then((entrys) => {
console.log('getEntries success');
}).catch((err) => {
console.log('getEntries fail ' + JSON.stringify(err));
......@@ -3221,7 +3427,7 @@ try {
getResultSet(keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains the result set with the specified key prefix from this single KV store. This API uses an asynchronous callback to return the result.
Obtains the result set with the specified prefix. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3230,7 +3436,7 @@ Obtains the result set with the specified key prefix from this single KV store.
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| keyPrefix |string | Yes |Key prefix to match.|
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; | Yes |Callback used to return the result set obtained.|
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; | Yes |Callback invoked to return the result set obtained.|
**Example**
......@@ -3252,7 +3458,7 @@ try {
}
kvStore.putBatch(entries, async function (err, data) {
console.log('GetResultSet putBatch success');
await kvStore.getResultSet('batch_test_string_key', async function (err, result) {
kvStore.getResultSet('batch_test_string_key', async function (err, result) {
console.log('GetResultSet getResultSet succeed.');
resultSet = result;
kvStore.closeResultSet(resultSet, function (err, data) {
......@@ -3270,7 +3476,7 @@ try {
getResultSet(keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
Obtains the result set with the specified key prefix from this single KV store. This API uses a promise to return the result.
Obtains the result set with the specified prefix. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3330,7 +3536,7 @@ try {
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3339,7 +3545,7 @@ Obtains the **KvStoreResultSet** object that matches the specified **Query** obj
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| query |Query | Yes |**Query** object to match. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; | Yes |Callback used to return the **KvStoreResultSet** object obtained.|
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.|
**Example**
......@@ -3363,7 +3569,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getResultSet(query, async function (err, result) {
kvStore.getResultSet(query, async function (err, result) {
console.log('getResultSet succeed.');
resultSet = result;
});
......@@ -3378,7 +3584,7 @@ try {
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3430,11 +3636,86 @@ try {
}
```
### getResultSet<sup>9+</sup> ###
getResultSet(predicates: dataSharePredicates.DataSharePredicates, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains a **KvStoreResultSet** object that matches the specified **Predicates** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| predicates | Predicates | Yes |**Predicates** object to match. If this parameter is **null**, define the processing logic. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultsetsup8sup)&gt; | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.|
**Example**
```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
let resultSet;
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.prefixKey("batch_test_string_key");
kvStore.getResultSet(predicates, async function (err, result) {
console.log(' GetResultSet success');
resultSet = result;
kvStore.closeResultSet(resultSet, function (err, data) {
console.log(' closeResultSet success');
})
});
}catch(e) {
console.log('An unexpected error occurred. Error:' + e);
}
```
### getResultSet<sup>9+</sup> ###
getResultSet(predicates: dataSharePredicates.DataSharePredicates): Promise&lt;KvStoreResultSet&gt;
Obtains a **KvStoreResultSet** object that matches the specified **Predicates** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| predicates |[Predicates](#) | Yes |**Predicates** object to match. If this parameter is **null**, define the processing logic. |
**Return value**
| Type | Description |
| ------ | ------- |
|Promise&lt;[KvStoreResultSet](#kvstoreresultset8)&gt; |Promise used to return the **KvStoreResultSet** object obtained.|
**Example**
```js
import dataSharePredicates from './@ohos.data.dataSharePredicates';
let kvStore;
try {
let resultSet;
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.prefixKey("batch_test_string_key");
kvStore.getResultSet(predicates) .then((result) => {
console.log(' GetResultSet success');
resultSet = result;
kvStore.closeResultSet(resultSet, fun ction (err, data) {
console.log(' closeResultSet success');
})
});
}catch(e) {
console.log('An unexpected error occurred. Error:' + e);
}
```
### closeResultSet<sup>8+</sup> ###
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3468,7 +3749,7 @@ try {
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3482,7 +3763,7 @@ Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](
| Type | Description |
| ------ | ------- |
|Promise&lt;void&gt; |Promise used to return the result.|
|Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -3514,7 +3795,7 @@ Obtains the number of results that matches the specified **Query** object. This
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| query |[Query](#query8) | Yes |**Query** object to match. |
| callback |AsyncCallback&lt;number&gt; | Yes |Callback used to return the number of results obtained. |
| callback |AsyncCallback&lt;number&gt; | Yes |Callback invoked to return the number of results obtained. |
**Example**
......@@ -3537,7 +3818,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getResultSize(query, async function (err, resultSize) {
kvStore.getResultSize(query, async function (err, resultSize) {
console.log('getResultSet succeed.');
});
});
......@@ -3627,12 +3908,12 @@ 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) {
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) {
kvStore.get(KEY_TEST_STRING_ELEMENT, async function (err,data) {
console.log('RemoveDeviceData get success');
});
}
......@@ -3662,7 +3943,7 @@ Deletes data of a device. This API uses a promise to return the result.
| Type | Description |
| ------ | ------- |
|Promise&lt;void&gt; |Promise used to return the result.|
|Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -3697,7 +3978,7 @@ try {
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Subscribes to the synchronization completion events. This API uses a synchronous callback to return the result.
Subscribes to the data synchronization complete events.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3705,8 +3986,8 @@ Subscribes to the synchronization completion events. This API uses a synchronous
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to subscribe to. The value is **syncComplete**, which indicates the synchronization complete event. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes |Callback used to return the synchronization result. |
| event |string | Yes |Event to subscribe to. The value is **syncComplete**, which indicates completion of a data synchronization. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | Yes |Callback invoked to return the synchronization result. |
**Example**
......@@ -3733,7 +4014,7 @@ try {
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Unsubscribes from the synchronization completion events. This API uses a synchronous callback to return the result.
Unsubscribes from the data synchronization complete events.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -3741,7 +4022,7 @@ Unsubscribes from the synchronization completion events. This API uses a synchro
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to unsubscribe from. The value is **syncComplete**, which indicates the synchronization complete event. |
| event |string | Yes |Event to unsubscribe from. The value is **syncComplete**, which indicates completion of a data synchronization. |
| syncCallback |Callback&lt;Array&lt;[string, number]&gt;&gt; | No |Callback used to return the synchronization result. |
**Example**
......@@ -3759,12 +4040,64 @@ try {
}
```
### on('dataChange')<sup>9+</sup> ###
on(event: 'dataChange', type: SubscribeType, listener: Callback&lt;ChangeNotification&gt;): void
Subscribes to data changes of the specified type. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Event to subscribe to. The value is **dataChange**, which indicates data changes. |
| type |[SubscribeType](#subscribetype) | Yes |Subscription type. |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes |Callback used to return the result.|
**Example**
```js
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
console.log("dataChange callback call data: " + JSON.stringify(data));
});
```
### off('dataChange')<sup>9+</sup> ###
off(event:'dataChange', listener?: Callback&lt;ChangeNotification&gt;): void
Unsubscribes from the data change events. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Event to unsubscribe from. The value is **dataChange**, which indicates data changes. |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; |No |Callback used to return the result.|
**Example**
```js
let kvStore;
kvStore.on('dataChange', function (data) {
console.log("callback call data: " + data);
});
kvStore.off('dataChange', function (data) {
console.log("callback call data: " + data);
});
```
### sync<sup>7+</sup>
### sync
sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void
Manually triggers KV store synchronization synchronously. For details about the synchronization mode of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
Synchronizes the KV store manually. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
......@@ -3774,8 +4107,8 @@ Manually triggers KV store synchronization synchronously. For details about the
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| 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. |
| deviceIdList |string[] | Yes |List of IDs of the devices in the same networking environment to be synchronized. |
| mode |[SyncMode](#syncmode) | Yes |Synchronization mode. |
| allowedDelayMs |number | No |Allowed synchronization delay time, in ms. |
**Example**
......@@ -3785,6 +4118,48 @@ let kvStore;
kvStore.sync('deviceIds', distributedData.SyncMode.PULL_ONLY, 1000);
```
### sync<sup>9+</sup>
sync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void
Synchronizes the KV store manually. This API uses a synchronous mode. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| deviceIds |string[] | Yes |List of IDs of the devices in the same networking environment to be synchronized. |
| mode |[SyncMode](#syncmode) | Yes |Synchronization mode. |
| query |[Query](#querysup8sup) | Yes |**Query** object to match. |
| delayMs |number | No |Allowed synchronization delay time, in ms. |
**Example**
```js
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;
const query = new distributedData.Query();
query.prefixKey("batch_test");
query.deviceId('localDeviceId');
kvStore.sync(devices, query, PULL_ONLY , 1000);
});
}catch(e) {
console.log('Sync e' + e);
}
```
### setSyncParam<sup>8+</sup> ###
setSyncParam(defaultAllowedDelayMs: number, callback: AsyncCallback&lt;void&gt;): void
......@@ -3834,7 +4209,7 @@ Sets the default delay allowed for KV store synchronization. This API uses a pro
| Type | Description |
| ------ | ------- |
|Promise&lt;void&gt; |Promise used to return the result.|
|Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -3865,7 +4240,7 @@ Obtains the security level of this KV store. This API uses an asynchronous callb
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| callback |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt; | Yes |Callback used to return the security level obtained. |
| callback |AsyncCallback&lt;[SecurityLevel](#securitylevel)&gt; | Yes |Callback invoked to return the security level obtained. |
**Example**
......@@ -3893,7 +4268,7 @@ Obtains the security level of this KV store. This API uses a promise to return t
| Type | Description |
| ------ | ------- |
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the value obtained.|
|Promise&lt;[SecurityLevel](#securitylevel)&gt; |Promise used to return the security level obtained.|
**Example**
......@@ -3913,13 +4288,19 @@ try {
## DeviceKVStore<sup>8+</sup> ##
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](#getkvstore) to obtain a **DeviceKVStore** object.
Provides methods to query and synchronize data in a device KV store. This class inherits from [KVStore](#kvstore).
Data is distinguished by device in a device KV store. Each device can only write and modify its own data. Data of other devices is read-only and cannot be modified.
For example, a device KV store can be used to implement image sharing between devices. The images of other devices can be viewed, but not be modified or deleted.
Before calling any method in **DeviceKVStore**, you must use [getKVStore](#getkvstore) to obtain a **DeviceKVStore** object.
### get<sup>8+</sup> ###
get(deviceId: string, key: string, callback: AsyncCallback&lt;boolean|string|number|Uint8Array&gt;): void
Obtains the string value that matches the specified key for a device. This API uses an asynchronous callback to return the result.
Obtains a string value that matches the specified device ID and key. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -3954,7 +4335,7 @@ try{
get(deviceId: string, key: string): Promise&lt;boolean|string|number|Uint8Array&gt;
Obtains the string value that matches the specified key for a device. This API uses a promise to return the result.
Obtains a string value that matches the specified device ID and key. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -3969,7 +4350,7 @@ Obtains the string value that matches the specified key for a device. This API u
| Type | Description |
| ------ | ------- |
|Promise&lt;boolean\|string\|number\|Uint8Array&gt; |Promise used to return the value obtained.|
|Promise&lt;boolean\|string\|number\|Uint8Array&gt; |Promise used to return the string value obtained.|
**Example**
......@@ -3998,7 +4379,7 @@ try {
getEntries(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;Entry[]&gt;): void
Obtains the KV pairs that match the specified key prefix for a device. This API uses an asynchronous callback to return the result.
Obtains all KV pairs that match the specified device ID and key prefix. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4030,7 +4411,7 @@ try {
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) {
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]));
......@@ -4046,7 +4427,7 @@ try {
getEntries(deviceId: string, keyPrefix: string): Promise&lt;Entry[]&gt;
Obtains the KV pairs that match the specified key prefix for a device. This API uses a promise to return the result.
Obtains all KV pairs that match the specified device ID and key prefix. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4083,7 +4464,7 @@ try {
console.log('entries: ' + entries);
kvStore.putBatch(entries).then(async (err) => {
console.log('putBatch success');
await kvStore.getEntries('localDeviceId', 'batch_test_string_key').then((entrys) => {
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]));
......@@ -4137,11 +4518,10 @@ try {
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) {
kvStore.getEntries(query, function (err,entrys) {
console.log('getEntries success');
console.log('entrys.length: ' + entrys.length);
console.log('entrys[0]: ' + JSON.stringify(entrys[0]));
......@@ -4197,7 +4577,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getEntries(query).then((entrys) => {
kvStore.getEntries(query).then((entrys) => {
console.log('getEntries success');
}).catch((err) => {
console.log('getEntries fail ' + JSON.stringify(err));
......@@ -4216,7 +4596,7 @@ try {
getEntries(deviceId: string, query: Query, callback: AsyncCallback&lt;Entry[]&gt;): void
Obtains the KV pairs that match the specified **Query** object for a device. This API uses an asynchronous callback to return the result.
Obtains the KV pairs that match the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4226,7 +4606,7 @@ Obtains the KV pairs that match the specified **Query** object for a device. Thi
| ----- | ------ | ---- | ----------------------- |
| 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. |
| callback |AsyncCallback&lt;[Entry](#entry)[]&gt; | Yes |Callback invoked to return the KV pairs obtained. |
**Example**
......@@ -4249,11 +4629,10 @@ try {
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) {
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]));
......@@ -4270,7 +4649,7 @@ try {
getEntries(deviceId: string, query: Query): Promise&lt;Entry[]&gt;
Obtains the KV pairs that match the specified **Query** object for a device. This API uses a promise to return the result.
Obtains the KV pairs that match the specified device ID and **Query** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4311,7 +4690,7 @@ try {
var query = new distributedData.Query();
query.deviceId('localDeviceId');
query.prefixKey("batch_test");
await kvStore.getEntries('localDeviceId', query).then((entrys) => {
kvStore.getEntries('localDeviceId', query).then((entrys) => {
console.log('getEntries success');
}).catch((err) => {
console.log('getEntries fail ' + JSON.stringify(err));
......@@ -4330,7 +4709,7 @@ try {
getResultSet(deviceId: string, keyPrefix: string, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains the **KvStoreResultSet** object that matches the specified key prefix for a device. This API uses an asynchronous callback to return the result.
Obtains a **KvStoreResultSet** object that matches the specified device ID and key prefix. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4340,7 +4719,7 @@ Obtains the **KvStoreResultSet** object that matches the specified key prefix fo
| ----- | ------ | ---- | ----------------------- |
| 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. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; | Yes |Callback invoked to return the **KvStoreResultSet** object obtained. |
**Example**
......@@ -4351,7 +4730,7 @@ try {
kvStore.getResultSet('localDeviceId', 'batch_test_string_key', async function (err, result) {
console.log('getResultSet succeed.');
resultSet = result;
await kvStore.closeResultSet(resultSet, function (err, data) {
kvStore.closeResultSet(resultSet, function (err, data) {
console.log('closeResultSet success');
})
});
......@@ -4365,7 +4744,7 @@ try {
getResultSet(deviceId: string, keyPrefix: string): Promise&lt;KvStoreResultSet&gt;
Obtains the **KvStoreResultSet** object that matches the specified key prefix for a device. This API uses a promise to return the result.
Obtains a **KvStoreResultSet** object that matches the specified device ID and key prefix. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4409,7 +4788,7 @@ try {
getResultSet(query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4418,7 +4797,7 @@ Obtains the **KvStoreResultSet** object that matches the specified **Query** obj
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| query |[Query](#query8) | Yes |**Query** object to match. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; | Yes |Callback used to return the **KvStoreResultSet** object obtained. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; | Yes |Callback invoked to return the **KvStoreResultSet** object obtained. |
**Example**
......@@ -4443,10 +4822,10 @@ try {
const query = new distributedData.Query();
query.prefixKey("batch_test");
query.deviceId('localDeviceId');
await kvStore.getResultSet(query, async function (err, result) {
kvStore.getResultSet(query, async function (err, result) {
console.log('getResultSet succeed.');
resultSet = result;
await kvStore.closeResultSet(resultSet, function (err, data) {
kvStore.closeResultSet(resultSet, function (err, data) {
console.log('closeResultSet success');
})
});
......@@ -4461,7 +4840,7 @@ try {
getResultSet(query: Query): Promise&lt;KvStoreResultSet&gt;
Obtains the **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
Obtains a **KvStoreResultSet** object that matches the specified **Query** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4525,7 +4904,7 @@ try {
getResultSet(deviceId: string, query: Query, callback: AsyncCallback&lt;KvStoreResultSet&gt;): void
Obtains the **KvStoreResultSet** object that matches the specified **Query** object for a device. This API uses an asynchronous callback to return the result.
Obtains a **KvStoreResultSet** object that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4535,7 +4914,7 @@ Obtains the **KvStoreResultSet** object that matches the specified **Query** obj
| ----- | ------ | ---- | ----------------------- |
| 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. |
| callback |AsyncCallback&lt;[KvStoreResultSet](#kvstoreresultset8)[]&gt; | Yes |Callback invoked to return the **KvStoreResultSet** object obtained. |
**Example**
......@@ -4559,10 +4938,10 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getResultSet('localDeviceId', query, async function (err, result) {
kvStore.getResultSet('localDeviceId', query, async function (err, result) {
console.log('getResultSet succeed.');
resultSet = result;
await kvStore.closeResultSet(resultSet, function (err, data) {
kvStore.closeResultSet(resultSet, function (err, data) {
console.log('closeResultSet success');
})
});
......@@ -4577,7 +4956,7 @@ try {
getResultSet(deviceId: string, query: Query): Promise&lt;KvStoreResultSet&gt;
Obtains the **KvStoreResultSet** object that matches the specified **Query** object for a device. This API uses a promise to return the result.
Obtains a **KvStoreResultSet** object that matches the specified device ID and **Query** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4643,7 +5022,7 @@ try {
closeResultSet(resultSet: KvStoreResultSet, callback: AsyncCallback&lt;void&gt;): void
Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4678,7 +5057,7 @@ try {
closeResultSet(resultSet: KvStoreResultSet): Promise&lt;void&gt;
Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4692,7 +5071,7 @@ Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](
| Type | Description |
| ------ | ------- |
|Promise&lt;void&gt; |Promise used to return the result.|
|Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -4725,7 +5104,7 @@ Obtains the number of results that matches the specified **Query** object. This
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| query |[Query](#query8) | Yes |**Query** object to match. |
| callback |AsyncCallback&lt;number&gt; | Yes |Callback used to return the number of results obtained. |
| callback |AsyncCallback&lt;number&gt; | Yes |Callback invoked to return the number of results obtained. |
**Example**
......@@ -4749,7 +5128,7 @@ try {
const query = new distributedData.Query();
query.prefixKey("batch_test");
query.deviceId('localDeviceId');
await kvStore.getResultSize(query, async function (err, resultSize) {
kvStore.getResultSize(query, async function (err, resultSize) {
console.log('getResultSet succeed.');
});
});
......@@ -4819,7 +5198,7 @@ try {
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 API uses an asynchronous callback to return the result.
Obtains the number of results that matches the specified device ID and **Query** object. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4829,7 +5208,7 @@ Obtains the number of results that matches the specified **Query** object for a
| ----- | ------ | ---- | ----------------------- |
| 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. |
| callback |AsyncCallback&lt;number&gt; | Yes |Callback invoked to return the number of results obtained. |
**Example**
......@@ -4852,7 +5231,7 @@ try {
console.log('putBatch success');
const query = new distributedData.Query();
query.prefixKey("batch_test");
await kvStore.getResultSize('localDeviceId', query, async function (err, resultSize) {
kvStore.getResultSize('localDeviceId', query, async function (err, resultSize) {
console.log('getResultSet succeed.');
});
});
......@@ -4866,7 +5245,7 @@ try {
getResultSize(deviceId: string, query: Query): Promise&lt;number&gt;
Obtains the number of results that matches the specified **Query** object for a device. This API uses a promise to return the result.
Obtains the number of results that matches the specified device ID and **Query** object. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4922,7 +5301,7 @@ try {
removeDeviceData(deviceId: string, callback: AsyncCallback&lt;void&gt;): void
Removes data of a device from this KV store. This API uses an asynchronous callback to return the result.
Deletes data of the specified device from this KV store. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4943,12 +5322,12 @@ 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) {
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) {
kvStore.get('localDeviceId', KEY_TEST_STRING_ELEMENT, async function (err,data) {
console.log('RemoveDeviceData get success');
});
}
......@@ -4964,7 +5343,7 @@ try {
removeDeviceData(deviceId: string): Promise&lt;void&gt;
Removes data of a device from this KV store. This API uses a promise to return the result.
Deletes data of the specified device from this KV store. This API uses a promise to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.DistributedKVStore
......@@ -4978,7 +5357,7 @@ Removes data of a device from this KV store. This API uses a promise to return t
| Type | Description |
| ------ | ------- |
|Promise&lt;void&gt; |Promise used to return the result.|
|Promise&lt;void&gt; |Promise that returns no value.|
**Example**
......@@ -5013,7 +5392,7 @@ try {
sync(deviceIdList: string[], mode: SyncMode, allowedDelayMs?: number): void
Manually triggers KV store synchronization synchronously. For details about the synchronization mode of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
Synchronizes the KV store manually. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
......@@ -5048,11 +5427,53 @@ try {
}
```
### sync<sup>9+</sup> ###
sync(deviceIds: string[], query: Query, mode: SyncMode, delayMs?: number): void
Synchronizes the KV store manually. This API uses a synchronous mode. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| deviceIds |string[] | Yes |IDs of the devices to be synchronized.|
| query |[Query](#query8) | Yes | **Query** object to match.|
| delayMs |number | No |Allowed synchronization delay time, in ms. |
**Example**
```js
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;
const query = new distributedData.Query();
query.prefixKey("batch_test");
query.deviceId('localDeviceId');
kvStore.sync(devices, query, 1000);
});
}catch(e) {
console.log('Sync e' + e);
}
```
### on('syncComplete')<sup>8+</sup> ###
on(event: 'syncComplete', syncCallback: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Subscribes to the synchronization completion events. This API uses a synchronous callback to return the result.
Subscribes to the data synchronization complete events.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -5060,12 +5481,13 @@ Subscribes to the synchronization completion events. This API uses a synchronous
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to subscribe to. The value is **syncComplete**, which indicates the synchronization complete event.|
| event |string | Yes |Event to subscribe to. The value is **syncComplete**, which indicates the data synchronization complete event.|
| syncCallback |Callback<Array&lt;[string, number]&gt;> | Yes |Callback used to return the synchronization result. |
**Example**
```js
let kvStore;
const KEY_TEST_FLOAT_ELEMENT = 'key_test_float';
const VALUE_TEST_FLOAT_ELEMENT = 321.12;
try {
......@@ -5087,7 +5509,7 @@ try {
off(event: 'syncComplete', syncCallback?: Callback&lt;Array&lt;[string, number]&gt;&gt;): void
Unsubscribes from the synchronization completion events. This API uses a synchronous callback to return the result.
Unsubscribes from the synchronization complete events. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
......@@ -5095,7 +5517,7 @@ Unsubscribes from the synchronization completion events. This API uses a synchro
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Name of the event to unsubscribe from. The value is **syncComplete**, which indicates the synchronization complete event.|
| event |string | Yes |Event to unsubscribe from. The value is **syncComplete**, which indicates the data synchronization complete event.|
| syncCallback |Callback<Array&lt;[string, number]&gt;&gt; | No |Callback used to return the synchronization result. |
**Example**
......@@ -5113,6 +5535,58 @@ try {
}
```
### on('dataChange')<sup>9+</sup> ###
on(event: 'dataChange', type: SubscribeType, listener: Callback&lt;ChangeNotification&gt;): void
Subscribes to data changes of the specified type. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Event to subscribe to. The value is **dataChange**, which indicates data changes. |
| type |[SubscribeType](#subscribetype) | Yes |Subscription type. |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; | Yes |Callback used to return the result.|
**Example**
```js
let kvStore;
kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_LOCAL, function (data) {
console.log("dataChange callback call data: " + JSON.stringify(data));
});
```
### off('dataChange')<sup>9+</sup> ###
off(event:'dataChange', listener?: Callback&lt;ChangeNotification&gt;): void
Unsubscribes from the data change events. This API uses a synchronous callback to return the result.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
**Parameters**
| Name | Type| Mandatory | Description |
| ----- | ------ | ---- | ----------------------- |
| event |string | Yes |Event to unsubscribe from. The value is **dataChange**, which indicates data changes. |
| listener |Callback&lt;[ChangeNotification](#changenotification)&gt; |No |Callback used to return the result.|
**Example**
```js
let kvStore;
kvStore.on('dataChange', function (data) {
console.log("callback call data: " + data);
});
kvStore.off('dataChange', function (data) {
console.log("callback call data: " + data);
});
```
## SyncMode
......@@ -5120,7 +5594,7 @@ Enumerates the synchronization modes.
**System capability**: SystemCapability.DistributedDataManager.KVStore.Core
| Name | Default Value | Description |
| Name | Value | Description |
| ----- | ------ | ----------------------- |
| PULL_ONLY |0 |Pull data from the peer end to the local end only.|
| PUSH_ONLY |1 |Push data from the local end to the peer end only.|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册