fa-dataability.md 11.1 KB
Newer Older
1
# Data Ability Development
W
wusongqing 已提交
2

W
wusongqing 已提交
3
## When to Use
W
wusongqing 已提交
4

5 6 7 8
A Data ability helps applications manage access to data stored by themselves and other applications. It also provides APIs for sharing data with other applications either on the same device or across devices.

Data ability providers can customize data access-related APIs such as data inserting, deleting, updating, and querying, as well as file opening, and share data with other applications through these open APIs.

W
wusongqing 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
## URI Introduction

A Uniform Resource Identifier (URI) is used to identify a specific data item, such as a table in the database or a file on the disk. URIs used in OpenHarmony comply with the commonly used URI standard. A URI consists of the components:

![fa-dataability-uri](figures/fa-dataability-uri.png)

- **scheme**: name of the scheme used by the Data ability. The value is fixed at **dataability**.
- **authority**: device ID. To access data on a remote device, set this component to the ID of the remote device. To access data on the local device, leave this component empty.
- **path**: location of the specific resource to access.
- **query**: query parameters.
- **fragment**: subordinate resources to access.

Example URIs:

- Cross-device communication: **dataability://***device_id***/***com.domainname.dataability.persondata***/***person***/***10*
- Local-device communication: **dataability:///***com.domainname.dataability.persondata***/***person***/***10*


> **NOTE**
>
> In the case of local-device communication, **device_id** is empty, and therefore, there are three slashes (/) after **dataability:**.


W
wusongqing 已提交
32 33
## Available APIs

W
wusongqing 已提交
34
**Table 1** Data ability lifecycle APIs
W
wusongqing 已提交
35 36
|API|Description|
|:------|:------|
W
wusongqing 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49
|onInitialized?(info: AbilityInfo): void|Called during ability initialization to initialize the relational database (RDB).|
|update?(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void|Updates data in the database.|
|query?(uri: string, columns: Array\<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<ResultSet>): void|Queries data in the database.|
|delete?(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback\<number>): void|Deletes one or more data records from the database.|
|normalizeUri?(uri: string, callback: AsyncCallback\<string>): void|Normalizes the URI. A normalized URI applies to cross-device use, persistence, backup, and restore. When the context changes, it ensures that the same data item can be referenced.|
|batchInsert?(uri: string, valueBuckets: Array\<rdb.ValuesBucket>, callback: AsyncCallback\<number>): void|Inserts multiple data records into the database.|
|denormalizeUri?(uri: string, callback: AsyncCallback\<string>): void|Converts a normalized URI generated by **normalizeUri** into a denormalized URI.|
|insert?(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback\<number>): void|Inserts a data record into the database.|
|openFile?(uri: string, mode: string, callback: AsyncCallback\<number>): void|Opens a file.|
|getFileTypes?(uri: string, mimeTypeFilter: string, callback: AsyncCallback\<Array\<string>>): void|Obtains the MIME type of a file.|
|getType?(uri: string, callback: AsyncCallback\<string>): void|Obtains the MIME type matching the data specified by the URI.|
|executeBatch?(ops: Array\<DataAbilityOperation>, callback: AsyncCallback\<Array\<DataAbilityResult>>): void|Operates data in the database in batches.|
|call?(method: string, arg: string, extras: PacMap, callback: AsyncCallback\<PacMap>): void|Calls a custom API.|
W
wusongqing 已提交
50 51 52 53


## How to Develop
### Creating a Data Ability
54

W
wusongqing 已提交
55
1. To meet the basic requirements of the database storage service, implement the **Insert**, **Query**, **Update**, and **Delete** APIs in the **Data** class. The **BatchInsert** and **ExecuteBatch** APIs have already implemented the traversal logic, but not batch data processing.
56

W
wusongqing 已提交
57
    The following code snippet shows how to create a Data ability:
58 59

   ```javascript
W
wusongqing 已提交
60 61 62 63 64 65 66 67
    import dataAbility from '@ohos.data.dataAbility'
    import dataRdb from '@ohos.data.rdb'

    const TABLE_NAME = 'book'
    const STORE_CONFIG = { name: 'book.db' }
    const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)'
    let rdbStore: dataRdb.RdbStore = undefined

68
    export default {
W
wusongqing 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    onInitialized(abilityInfo) {
        console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName)
        dataRdb.getRdbStore(STORE_CONFIG, 1, (err, store) => {
            console.info('DataAbility getRdbStore callback')
            store.executeSql(SQL_CREATE_TABLE, [])
            rdbStore = store
        });
    },
    insert(uri, valueBucket, callback) {
        console.info('DataAbility insert start')
        rdbStore.insert(TABLE_NAME, valueBucket, callback)
    },
    batchInsert(uri, valueBuckets, callback) {
        console.info('DataAbility batch insert start')
        for (let i = 0;i < valueBuckets.length; i++) {
            console.info('DataAbility batch insert i=' + i)
            if (i < valueBuckets.length - 1) {
W
wusongqing 已提交
86
                rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: any, num: number) => {
W
wusongqing 已提交
87 88 89 90
                    console.info('DataAbility batch insert ret=' + num)
                })
            } else {
                rdbStore.insert(TABLE_NAME, valueBuckets[i], callback)
91 92
            }
        }
W
wusongqing 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    },
    query(uri, columns, predicates, callback) {
        console.info('DataAbility query start')
        let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
        rdbStore.query(rdbPredicates, columns, callback)
    },
    update(uri, valueBucket, predicates, callback) {
        console.info('DataAbilityupdate start')
        let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
        rdbStore.update(valueBucket, rdbPredicates, callback)
    },
    delete(uri, predicates, callback) {
        console.info('DataAbilitydelete start')
        let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates)
        rdbStore.delete(rdbPredicates, callback)
    }
109 110 111
    };
   ```

W
wusongqing 已提交
112
2. Configure the submodule.
113

W
wusongqing 已提交
114 115 116 117 118 119
   | JSON Field| Description                                                    |
   | ------------ | ------------------------------------------------------------ |
   | "name"       | Ability name, corresponding to the **Data** class name derived from **Ability**.                    |
   | "type"       | Ability type, which is **Data** for a Data ability.                |
   | "uri"        | URI used for communication.                                             |
   | "visible"    | Whether the Data ability is visible to other applications. When this parameter is set to **true**, the Data ability can communicate with other applications.|
120

W
wusongqing 已提交
121
   **config.json configuration example**
122

W
wusongqing 已提交
123 124
   ```json
   "abilities":[{
W
wusongqing 已提交
125 126 127 128 129 130 131 132 133
       "srcPath": "DataAbility",
       "name": ".DataAbility",
       "icon": "$media:icon",
       "srcLanguage": "ets",
       "description": "$string:description_dataability",
       "type": "data",
       "visible": true,
       "uri": "dataability://ohos.samples.etsdataability.DataAbility"
    }]
W
wusongqing 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
   ```

### Accessing a Data ability
#### Development Preparations

Import the basic dependency packages and obtain the URI string for communicating with the Data submodule.

The basic dependency packages include:
- @ohos.ability.featureAbility
- @ohos.data.dataability
- @ohos.data.rdb

#### Data Ability API Development
147 148


W
wusongqing 已提交
149 150 151 152 153
1. Create a Data ability helper.

   For details about the APIs provided by **DataAbilityHelper**, see [DataAbilityHelper Module](../reference/apis/js-apis-dataAbilityHelper.md).
   ```js
   // Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), because there is a DeviceID parameter between the second and the third slash (/).
W
wusongqing 已提交
154 155 156
   import featureAbility from '@ohos.ability.featureAbility'
   import ohos_data_ability from '@ohos.data.dataAbility'
   import ohos_data_rdb from '@ohos.data.rdb'
G
ge-yafang 已提交
157
   
W
wusongqing 已提交
158 159
   var urivar = "dataability:///com.ix.DataAbility"
   var DAHelper = featureAbility.acquireDataAbilityHelper(
160
    urivar
W
wusongqing 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173
   );
   ```
2. Construct RDB data.
   ```js
   var valuesBucket = {"name": "gaolu"}
   var da = new ohos_data_ability.DataAbilityPredicates()
   var valArray =new Array("value1");
   var cars = new Array({"batchInsert1" : "value1",});
   ```
3. Use **insert** to insert data to the Data submodule.
   ```js
   // Callback mode:
   DAHelper.insert(
174 175 176
    urivar,
    valuesBucket,
    (error, data) => {
W
wusongqing 已提交
177
        console.log("DAHelper insert result: " + data)
178
    }
W
wusongqing 已提交
179 180 181 182 183 184
   );
   ```
   
   ```js
   // Promise mode:
   var datainsert = await DAHelper.insert(
185 186
    urivar,
    valuesBucket
W
wusongqing 已提交
187 188 189 190 191 192
   );
   ```
4. Use **delete** to delete data from the Data submodule.
   ```js
   // Callback mode:
   DAHelper.delete(
193 194 195
    urivar,
    da,
    (error, data) => {
W
wusongqing 已提交
196
        console.log("DAHelper delete result: " + data)
197
    }
W
wusongqing 已提交
198 199 200 201 202 203
   );
   ```
   
   ```js
   // Promise mode:
   var datadelete = await DAHelper.delete(
204 205
    urivar,
    da,
W
wusongqing 已提交
206 207 208 209 210 211
   );
   ```
5. Use **update** to update data in the Data submodule.
   ```js
   // Callback mode:
   DAHelper.update(
212 213 214 215
    urivar
    valuesBucket,
    da,
    (error, data) => {
W
wusongqing 已提交
216
        console.log("DAHelper update result: " + data)
217
    }
W
wusongqing 已提交
218 219 220 221 222 223
   );
   ```
   
   ```js
   // Promise mode:
   var dataupdate = await DAHelper.update(
224 225 226
    urivar,
    valuesBucket,
    da,
W
wusongqing 已提交
227 228 229 230 231 232
   );
   ```
6. Use **query** to query data in the Data submodule.
   ```js
   // Callback mode:
   DAHelper.query(
233 234 235 236
    urivar,
    valArray,
    da,
    (error, data) => {
W
wusongqing 已提交
237
        console.log("DAHelper query result: " + data)
238
    }
W
wusongqing 已提交
239 240 241 242 243 244
   );
   ```
   
   ```js
   // Promise mode:
   var dataquery = await DAHelper.query(
245 246 247
    urivar,
    valArray,
    da
W
wusongqing 已提交
248 249 250 251 252 253
   );
   ```
7. Use **batchInsert** to insert data in batches to the Data submodule.
   ```js
   // Callback mode:
   DAHelper.batchInsert(
254 255 256
    urivar,
    cars,
    (error, data) => {
W
wusongqing 已提交
257
        console.log("DAHelper batchInsert result: " + data)
258
    }
W
wusongqing 已提交
259 260 261 262 263 264
   );
   ```
   
   ```js
   // Promise mode:
   var databatchInsert = await DAHelper.batchInsert(
265 266
    urivar,
    cars
W
wusongqing 已提交
267 268 269 270 271 272
   );
   ```
8. Use **executeBatch** to process data in batches in the Data submodule.
   ```js
   // Callback mode:
   DAHelper.executeBatch(
273 274 275 276 277 278 279 280
    urivar,
    [
        {
            uri: urivar,
            type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
            valuesBucket: {"executeBatch" : "value1",},
            predicates: da,
            expectedCount:0,
W
wusongqing 已提交
281
            predicatesBackReferences: null,
282 283 284 285
            interrupted:true,
        }
    ],
    (error, data) => {
W
wusongqing 已提交
286
        console.log("DAHelper executeBatch result: " + data)
287
    }
W
wusongqing 已提交
288 289 290 291 292 293
   );
   ```
   
   ```js
   // Promise mode:
   var dataexecuteBatch = await DAHelper.executeBatch(
294 295 296 297 298 299 300 301 302 303 304
    urivar,
    [
        {
            uri: urivar,
            type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
            valuesBucket:
            {
                "executeBatch" : "value1",
            },
            predicates: da,
            expectedCount:0,
W
wusongqing 已提交
305
            predicatesBackReferences: null,
306 307 308
            interrupted:true,
        }
    ]
W
wusongqing 已提交
309 310
   );
   ```