# Distributed Data Service Development ## When to Use The DDS implements synchronization of application data across user devices. When data is added, deleted, or modified for an application on a device, the same application on another device can obtain the data changes. The DDS applies to the distributed gallery, messages, contacts, and file manager. ## Available APIs The following table describes the APIs provided by the OpenHarmony DDS module. **Table 1** APIs provided by the DDS

Function

API

Description

Creating a distributed database

createKVManager(config: KVManagerConfig, callback: AsyncCallback<KVManager>): void

createKVManager(config: KVManagerConfig): Promise<KVManager>

Creates a KVManager object for database management.

getKVStore<T extends KVStore>(storeId: string, options: Options, callback: AsyncCallback<T>): void

getKVStore<T extends KVStore>(storeId: string, options: Options): Promise<T>

Obtains the KV store with specified Options and storeId.

Managing data in a distributed database

put(key: string, value: Uint8Array | string | number | boolean, callback: AsyncCallback<void>): void

put(key: string, value: Uint8Array | string | number | boolean): Promise<void>

Inserts and updates data.

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

delete(key: string): Promise<void>

Deletes data.

get(key: string, callback: AsyncCallback<Uint8Array | string | boolean | number>): void

get(key: string): Promise<Uint8Array | string | boolean | number>

Queries data.

Subscribing to changes in the distributed data

on(event: 'dataChange', type: SubscribeType, observer: Callback<ChangeNotification>): void

on(event: 'syncComplete', syncCallback: Callback<Array<[string, number]>>): void

Subscribes to data changes in the database.

Synchronizing distributed data

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

Triggers database synchronization in manual mode.

## How to Develop The following uses a single KV store as an example to describe the development procedure. 1. Import the distributed database module. ```js import distributedData from '@ohos.data.distributedData'; ``` 2. Create a **KvManager** instance based on the specified **KvManagerConfig** object. 1. Create a **KvManagerConfig** object based on the application context. 2. Create a **KvManager** instance. The sample code is as follows: ```js let kvManager; try { const kvManagerConfig = { bundleName : 'com.example.datamanagertest', userInfo : { userId : '0', userType : distributedData.UserType.SAME_USER_ID } } distributedData.createKVManager(kvManagerConfig, function (err, manager) { if (err) { console.log("createKVManager err: " + JSON.stringify(err)); return; } console.log("createKVManager success"); kvManager = manager; }); } catch (e) { console.log("An unexpected error occurred. Error:" + e); } ``` 3. Create and obtain a single KV store. 1. Declare the ID of the single KV store to create. 2. Create a single KV store. You are advised to disable automatic synchronization \(**autoSync:false**\) and call **sync** if a synchronization is required. The sample code is as follows: ```js let kvStore; try { const options = { createIfMissing : true, encrypt : false, backup : false, autoSync : false, kvStoreType : distributedData.KVStoreType.SINGLE_VERSION, securityLevel : distributedData.SecurityLevel.S2, }; kvManager.getKVStore('storeId', options, function (err, store) { if (err) { console.log("getKVStore err: " + JSON.stringify(err)); return; } console.log("getKVStore success"); kvStore = store; }); } catch (e) { console.log("An unexpected error occurred. Error:" + e); } ``` >![](../public_sys-resources/icon-note.gif) **NOTE:** >For data synchronization between networked devices, you are advised to open the distributed database during application startup to obtain the database handle. With this database handle \(**kvStore** in this example\), you can perform operations, such as inserting data into the database, without creating the database repeatedly during the lifecycle of the handle. 4. Subscribe to changes in the distributed data. The following is the sample code for subscribing to the data changes of a single KV store: ```js kvStore.on('dataChange', distributedData.SubscribeType.SUBSCRIBE_TYPE_ALL, function (data) { console.log("dataChange callback call data: " + JSON.stringify(data)); }); ``` 5. Write data to the single KV store. 1. Construct the key and value to be written into the single KV store. 2. Write key-value pairs into the single KV store. The following is the sample code for writing key-value pairs of the string type into the single KV store: ```js const KEY_TEST_STRING_ELEMENT = 'key_test_string'; const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; try { kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) { if (err != undefined) { console.log("put err: " + JSON.stringify(err)); return; } console.log("put success"); }); }catch (e) { console.log("An unexpected error occurred. Error:" + e); } ``` 6. Query data in the single KV store. 1. Construct the key to be queried from the single KV store. 2. Query data from the single KV store. The following is the sample code for querying data of the string type from the single KV store: ```js const KEY_TEST_STRING_ELEMENT = 'key_test_string'; const VALUE_TEST_STRING_ELEMENT = 'value-test-string'; try { kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, function (err,data) { if (err != undefined) { console.log("put err: " + JSON.stringify(err)); return; } console.log("put success"); kvStore.get(KEY_TEST_STRING_ELEMENT, function (err,data) { console.log("get success data: " + data); }); }); }catch (e) { console.log("An unexpected error occurred. Error:" + e); } ``` 7. Synchronize data to other devices. Select the devices in the same network and the synchronization mode to synchronize data. The following is the sample code for data synchronization in a single KV store. **deviceIds** can be obtained by deviceManager by calling **getTrustedDeviceListSync\(\)**, and **1000** indicates that the maximum delay time is 1000 ms. ```js import deviceManager from '@ohos.distributedHardware.deviceManager'; let devManager; // create deviceManager deviceManager.createDeviceManager("bundleName", (err, value) => { if (!err) { devManager = value; } }); // get deviceIds let deviceIds = []; if (devManager != null) { var deviceList = devManager.getTrustedDeviceListSync(); for (var i = 0; i < deviceList.length; i++) { deviceIds[i] = deviceList[i].deviceId; } } kvStore.sync(deviceIds, distributedData.SyncMode.PUSH_ONLY, 1000); ```