提交 1bed655d 编写于 作者: W wusongqing

udpated fa-dataability

Signed-off-by: Nwusongqing <wusongqing@huawei.com>
上级 ec434260
# Data Ability Development # Data Ability Development
## When to Use
## Basic Concepts
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. 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. 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.
## Creating a Data Ability ## Available APIs
### 1. Implementation of the Data Subsystem
1. To meet the basic requirements of the database storage service, implement the **Insert**, **Query**, **Update**, and **Delete** APIs in the **Data** class to provide batch data processing. The traversal logic has been implemented by the **BatchInsert** and **ExecuteBatch** APIs. **Table 1** Data ability lifecycle callbacks
2. The APIs used in the lifecycle of the Data ability are as follows: |API|Description|
|:------|:------|
- onInitialized |onInitialized|Called during ability initialization to initialize the relational database (RDB).|
|update|Updates data in the database.|
Called during ability initialization to initialize the relational database (RDB). |query|Queries data in the database.|
|delete|Deletes one or multiple data records from the database.|
- update |normalizeUri|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|Inserts multiple data records into the database.|
Updates data in the database. |denormalizeUri|Converts a normalized URI generated by **normalizeUri** into a denormalized URI.|
|insert|Inserts a data record into the database.|
- query |openFile|Opens a file.|
|getFileTypes|Obtains the MIME type of a file.|
Queries data in the database. |getType|Obtains the MIME type matching the data specified by the URI.|
|executeBatch|Operates data in the database in batches.|
- delete |call|A customized API.|
Deletes one or multiple data records from the database.
## How to Develop
- normalizeUri ### Creating a Data Ability
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
Inserts multiple data records into the database.
- denormalizeUri
Converts a normalized URI generated by **normalizeUri** into a denormalized URI.
- insert
Inserts a data record into the database.
- openFile
Opens a file.
- getFileTypes
Obtains the MIME type of a file.
- getType
Obtains the MIME type matching the data specified by the URI.
- executeBatch
Operates data in the database in batches.
- call
A customized API.
1. To meet the basic requirements of the database storage service, implement the **Insert**, **Query**, **Update**, and **Delete** APIs in the **Data** class to provide batch data processing. The traversal logic has been implemented by the **BatchInsert** and **ExecuteBatch** APIs.
The following code snippet shows how to create a Data ability: The following code snippet shows how to create a Data ability:
```javascript ```javascript
import dataAbility from '@ohos.data.dataability'
import dataRdb from '@ohos.data.rdb'
const TABLE_NAME = 'book'
const STORE_CONFIG = { name: 'book.db', encryptKey: new Uint8Array([]) }
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER, introduction TEXT NOT NULL)'
let rdbStore = undefined
export default { export default {
onInitialized(abilityInfo) { onInitialized(abilityInfo) {
console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName) console.info('DataAbility onInitialized, abilityInfo:' + abilityInfo.bundleName)
...@@ -116,23 +74,21 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -116,23 +74,21 @@ Data ability providers can customize data access-related APIs such as data inser
rdbStore.delete(rdbPredicates, callback) rdbStore.delete(rdbPredicates, callback)
} }
}; };
``` ```
2. Submodule Configuration
### 2. Subsystem Configuration | 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.|
| JSON Field | Description | **config.json configuration example**
| ------------- | ------------------------------------------------------------ |
| "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.|
**config.json configuration example** ```json
"abilities":[{
```json
"abilities":[{
"srcPath": "DataAbility", "srcPath": "DataAbility",
"name": ".DataAbility", "name": ".DataAbility",
"icon": "$media:icon", "icon": "$media:icon",
...@@ -141,121 +97,142 @@ Data ability providers can customize data access-related APIs such as data inser ...@@ -141,121 +97,142 @@ Data ability providers can customize data access-related APIs such as data inser
"type": "data", "type": "data",
"visible": true, "visible": true,
"uri": "dataability://ohos.samples.etsdataability.DataAbility" "uri": "dataability://ohos.samples.etsdataability.DataAbility"
}] }]
``` ```
### 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
## Accessing a Data ability
### 1. Preparing for JS Application Development
Basic dependency packages:
1. @ohos.ability.featureAbility
2. @ohos.data.dataability
3. @ohos.data.rdb
URI string used for communication with the Data ability.
### 2. JS Application Development APIs 1. Create a Data ability helper.
Create a utility API object.
```js For details about the APIs provided by **DataAbilityHelper**, see [DataAbilityHelper Module](../reference/apis/js-apis-dataAbilityHelper.md).
// 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 (/). ```js
var urivar = "dataability:///com.ix.DataAbility" // 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 (/).
var DAHelper = featureAbility.acquireDataAbilityHelper( var urivar = "dataability:///com.ix.DataAbility"
var DAHelper = featureAbility.acquireDataAbilityHelper(
urivar urivar
); );
``` ```
Construct RDB data. 2. Construct RDB data.
```js ```js
var valuesBucket = {"name": "gaolu"} var valuesBucket = {"name": "gaolu"}
var da = new ohos_data_ability.DataAbilityPredicates() var da = new ohos_data_ability.DataAbilityPredicates()
var valArray =new Array("value1"); var valArray =new Array("value1");
var cars = new Array({"batchInsert1" : "value1",}); var cars = new Array({"batchInsert1" : "value1",});
``` ```
Use **insert** to insert data to the Data subsystem. 3. Use **insert** to insert data to the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.insert( DAHelper.insert(
urivar, urivar,
valuesBucket, valuesBucket,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") expect(typeof(data)).assertEqual("number")
} }
); );
// Promise mode: ```
var datainsert = await DAHelper.insert(
```js
// Promise mode:
var datainsert = await DAHelper.insert(
urivar, urivar,
valuesBucket valuesBucket
); );
``` ```
Use **delete** to delete data from the Data subsystem. 4. Use **delete** to delete data from the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.delete( DAHelper.delete(
urivar, urivar,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") expect(typeof(data)).assertEqual("number")
} }
); );
// Promise mode: ```
var datadelete = await DAHelper.delete(
```js
// Promise mode:
var datadelete = await DAHelper.delete(
urivar, urivar,
da, da,
); );
``` ```
Use **update** to update data in the Data subsystem. 5. Use **update** to update data in the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.update( DAHelper.update(
urivar urivar
valuesBucket, valuesBucket,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") expect(typeof(data)).assertEqual("number")
} }
); );
// Promise mode: ```
var dataupdate = await DAHelper.update(
```js
// Promise mode:
var dataupdate = await DAHelper.update(
urivar, urivar,
valuesBucket, valuesBucket,
da, da,
); );
``` ```
Use **query** to query data in the Data subsystem. 6. Use **query** to query data in the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.query( DAHelper.query(
urivar, urivar,
valArray, valArray,
da, da,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("object") expect(typeof(data)).assertEqual("object")
} }
); );
// Promise mode: ```
var dataquery = await DAHelper.query(
```js
// Promise mode:
var dataquery = await DAHelper.query(
urivar, urivar,
valArray, valArray,
da da
); );
``` ```
Use **batchInsert** to insert data in batches to the Data subsystem. 7. Use **batchInsert** to insert data in batches to the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.batchInsert( DAHelper.batchInsert(
urivar, urivar,
cars, cars,
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("number") expect(typeof(data)).assertEqual("number")
} }
); );
// Promise mode: ```
var databatchInsert = await DAHelper.batchInsert(
```js
// Promise mode:
var databatchInsert = await DAHelper.batchInsert(
urivar, urivar,
cars cars
); );
``` ```
Use **executeBatch** to process data in batches in the Data subsystem. 8. Use **executeBatch** to process data in batches in the Data submodule.
```js ```js
// Callback mode: // Callback mode:
DAHelper.executeBatch( DAHelper.executeBatch(
urivar, urivar,
[ [
{ {
...@@ -271,9 +248,12 @@ DAHelper.executeBatch( ...@@ -271,9 +248,12 @@ DAHelper.executeBatch(
(error, data) => { (error, data) => {
expect(typeof(data)).assertEqual("object") expect(typeof(data)).assertEqual("object")
} }
); );
// Promise mode: ```
var dataexecuteBatch = await DAHelper.executeBatch(
```js
// Promise mode:
var dataexecuteBatch = await DAHelper.executeBatch(
urivar, urivar,
[ [
{ {
...@@ -289,8 +269,8 @@ var dataexecuteBatch = await DAHelper.executeBatch( ...@@ -289,8 +269,8 @@ var dataexecuteBatch = await DAHelper.executeBatch(
interrupted:true, interrupted:true,
} }
] ]
); );
``` ```
## Development Example ## Development Example
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册