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/>
>
>The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.
| config | [KVManagerConfig](#kvmanagerconfig) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
| callback | AsyncCallback<[KVManager](#kvmanager)> | Yes | Callback invoked to return the **KVManager** instance created.|
| config |[KVManagerConfig](#kvmanager) | Yes | Configuration of the **KVManager** instance, including the bundle name and user information of the caller.|
**Return value**
| Type| Description|
| -------- | -------- |
| Promise<[KVManager](#kvmanager)> | Promise used to return the **KVManager** instance created.|
| context<sup>9+<sup> | Context | Yes| Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md).|
| userInfo | [UserInfo](#userinfo) | Yes | User information.|
| SAME_USER_ID | 0 | User who logs in to different devices using the same account.|
## KVManager
Creates a **KVManager** object to obtain KV store information. Before calling any method in **KVManager**, you must use [createKVManager](#distributeddatacreatekvmanager) to create a **KVManager** object.
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.
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.
| createIfMissing | boolean | No| Whether to create a KV store if no database file exists. By default, a KV store is created.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| encrypt | boolean | No|Whether to encrypt database files. By default, database files are not encrypted.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| backup | boolean | No|Whether to back up database files. By default, database files are backed up. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| autoSync | boolean | No|Whether database files are automatically synchronized. By default, database files are not automatically synchronized.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core<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.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core|
| securityLevel | [SecurityLevel](#securitylevel) | No|Security level of the KV store. By default, the security level is not set.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| schema<sup>8+</sup> | [Schema](#schema8) | No| Schema used to define the values stored in a 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|
| 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. 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.|
| offset | number | Yes | Offset to move the data read position. A negative value means to move backward, and a positive value means to move forward. |
**Return value**
| Type | Description |
| ------ | -------------- |
| boolean |Returns **true** if the operation is successful; returns **false** otherwise. |
console.log("duplicated calls should be ok : "+e);
}
```
## 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.
| key | string | Yes |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). |
| value | Uint8Array \| string \| number \| boolean | Yes |Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX_VALUE_LENGTH](#constants). |
| callback | AsyncCallback<void> | Yes |Callback used to return the result. |
| key | string | Yes |Key of the KV pair to add. It cannot be empty, and the length cannot exceed [MAX_KEY_LENGTH](#constants). |
| value | Uint8Array \| string \| number \| boolean | Yes |Value of the KV pair to add. The value type can be Uint8Array, number, string, or boolean. A value of the Uint8Array or string type cannot exceed [MAX_VALUE_LENGTH](#constants). |
**Return value**
| Type | Description |
| ------ | ------- |
| Promise<void> |Promise that returns no value.|
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| callback | AsyncCallback<void> | Yes |Callback used to return the result. |
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
**Return value**
| Type | Description |
| ------ | ------- |
| Promise<void> |Promise used to return the result.|
| enabled |boolean | Yes |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
| callback |AsyncCallback<void> | Yes |Callback used to return the result. |
**Example**
```js
letkvStore;
try{
kvStore.enableSync(true,function(err,data){
if(err==undefined){
console.log('enableSync success');
}else{
console.log('enableSync fail');
}
});
}catch(e){
console.log('EnableSync e '+e);
}
```
### enableSync<sup>8+</sup> ###
enableSync(enabled: boolean): Promise<void>
Sets data synchronization, which can be enabled or disabled. This API uses a promise to return the result.
| enabled |boolean | Yes |Whether to enable data synchronization. The value **true** means to enable data synchronization, and **false** means the opposite. |
**Return value**
| Type | Description |
| ------ | ------- |
| Promise<void> |Promise that returns no value.|
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.
Obtains a **KvStoreResultSet** object that matches the specified **DataSharePredicates** object. This API uses an asynchronous callback to return the result.
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |**DataSharePredicates** object to match. If this parameter is **null**, define the processing logic. |
| callback |AsyncCallback<[KvStoreResultSet](#kvstoreresultset8)> | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.|
| predicates |[DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |**DataSharePredicates** object to match. If this parameter is **null**, define the processing logic. |
**Return value**
| Type | Description |
| ------ | ------- |
|Promise<[KvStoreResultSet](#kvstoreresultset8)> |Promise used to return the **KvStoreResultSet** object obtained.|
Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
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).
Synchronizes the KV store manually. This API returns the result synchronously. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
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.
Obtains a **KvStoreResultSet** object that matches the specified device ID and key prefix. 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.
Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
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).
Synchronizes the KV store manually. This API returns the result synchronously. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
@@ -113,7 +182,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.|
| context<sup>9+<sup> | Context | Yes| Application context.<br>For the application context of the FA model, see [Context](js-apis-Context.md).<br>For the application context of the stage model, see [Context](js-apis-ability-context.md).|
| userInfo | [UserInfo](#userinfo) | Yes | User information.|
| bundleName | string | Yes | Bundle name.|
...
...
@@ -572,8 +641,8 @@ 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.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| encrypt | boolean | No|Whether to encrypt database files. By default, database files are not encrypted.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| createIfMissing | boolean | No| Whether to create a KV store if no database file exists. By default, a KV store is created.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| encrypt | boolean | No|Whether to encrypt database files. By default, database files are not encrypted.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| backup | boolean | No|Whether to back up database files. By default, database files are backed up. <br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core |
| autoSync | boolean | No|Whether database files are automatically synchronized. By default, database files are not automatically synchronized.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core<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.<br>**System capability**: SystemCapability.DistributedDataManager.KVStore.Core|
...
...
@@ -1238,7 +1307,7 @@ Creates a **Query** object to match the specified field whose value is equal to
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1273,7 +1342,7 @@ Creates a **Query** object to match the specified field whose value is not equal
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1308,7 +1377,7 @@ Creates a **Query** object to match the specified field whose value is greater t
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1343,7 +1412,7 @@ Creates a **Query** object to match the specified field whose value is less than
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1378,7 +1447,7 @@ Creates a **Query** object to match the specified field whose value is greater t
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1413,7 +1482,7 @@ Creates a **Query** object to match the specified field whose value is less than
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1447,7 +1516,7 @@ Creates a **Query** object to match the specified field whose value is **null**.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1483,7 +1552,7 @@ Creates a **Query** object to match the specified field whose value is within th
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1518,7 +1587,7 @@ Creates a **Query** object to match the specified field whose value is within th
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1553,7 +1622,7 @@ Creates a **Query** object to match the specified field whose value is not withi
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1588,7 +1657,7 @@ Creates a **Query** object to match the specified field whose value is not withi
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1623,7 +1692,7 @@ Creates a **Query** object to match the specified field whose value is similar t
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1658,7 +1727,7 @@ Creates a **Query** object to match the specified field whose value is not simil
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1752,7 +1821,7 @@ Creates a **Query** object to sort the query results in ascending order.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1787,7 +1856,7 @@ Creates a **Query** object to sort the query results in descending order.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1823,7 +1892,7 @@ Creates a **Query** object to specify the number of results and where to start.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1860,7 +1929,7 @@ Creates a **Query** object to match the specified field whose value is not **nul
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1888,7 +1957,7 @@ Creates a **Query** object for a query condition group with a left parenthesis.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1918,7 +1987,7 @@ Creates a **Query** object for a query condition group with a right parenthesis.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1954,7 +2023,7 @@ Creates a **Query** object with a specified key prefix.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -1989,7 +2058,7 @@ Creates a **Query** object with an index preferentially used for query.
| Type | Description |
| ------ | ------- |
| [Query](#query8) |**Query** object.|
| [Query](#query8) |**Query** object created.|
**Example**
...
...
@@ -2025,7 +2094,7 @@ Creates a **Query** object with the device ID as the key prefix.
| predicates | Predicates | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| callback | AsyncCallback<void> | Yes |Callback used to return the result. |
| predicates | Predicates | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |Conditions for deleting data. If this parameter is **null**, define the processing logic.|
**Return value**
...
...
@@ -2288,7 +2357,7 @@ Deletes KV pairs that meet the specified predicates. This API uses a promise to
Obtains a **KvStoreResultSet** object that matches the specified **Predicates** object. This API uses an asynchronous callback to return the result.
Obtains a **KvStoreResultSet** object that matches the specified **DataSharePredicates** object. This API uses an asynchronous callback to return the result.
| predicates | Predicates | Yes |**Predicates** object to match. If this parameter is **null**, define the processing logic. |
| predicates | [DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |**DataSharePredicates** object to match. If this parameter is **null**, define the processing logic. |
| callback |AsyncCallback<[KvStoreResultSet](#kvstoreresultset8)> | Yes |Callback invoked to return the **KvStoreResultSet** object obtained.|
| predicates |[Predicates](#) | Yes |**Predicates** object to match. If this parameter is **null**, define the processing logic. |
| predicates |[DataSharePredicates](js-apis-data-dataSharePredicates.md#datasharepredicates) | Yes |**DataSharePredicates** object to match. If this parameter is **null**, define the processing logic. |
**Return value**
...
...
@@ -3695,7 +3763,7 @@ Obtains a **KvStoreResultSet** object that matches the specified **Predicates**
Closes the **KvStoreResultSet** object returned by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses an asynchronous callback to return the result.
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 a promise to return the result.
Closes the **KvStoreResultSet** object obtained by [SingleKvStore.getResultSet](#singlekvstore_getresultset). This API uses a promise to return the result.
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).
...
...
@@ -4108,9 +4176,9 @@ Synchronizes the KV store manually. For details about the synchronization modes
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).
Synchronizes the KV store manually. This API returns the result synchronously. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).
Closes the **KvStoreResultSet** object returned by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses an asynchronous callback to return the result.
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 a promise to return the result.
Closes the **KvStoreResultSet** object obtained by [DeviceKVStore.getResultSet](#devicekvstore_getresultset). This API uses a promise to return the result.
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).
...
...
@@ -5403,9 +5471,9 @@ Synchronizes the KV store manually. For details about the synchronization modes
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).
Synchronizes the KV store manually. This API returns the result synchronously. For details about the synchronization modes of the distributed data service, see [Distributed Data Service Overview] (../../database/database-mdds-overview.md).