1# Querying Assets (ArkTS)
2
3## Available APIs
4
5You can use [query(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetquery), an asynchronous API, and [querySync(query: AssetMap)](../../reference/apis-asset-store-kit/js-apis-asset.md#assetquerysync12), a synchronous API, to query assets.
6
7The following table describes the attributes of **AssetMap** for querying an asset.
8>**NOTE**
9>
10>In the following table, the attributes starting with **DATA_LABEL** are custom asset attributes reserved for services. These attributes are not encrypted. Therefore, do not put personal data in these attributes.
11
12| Attribute Name (Tag)       | Value                                            | Mandatory | Description                                                        |
13| --------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
14| ALIAS                 | Type: Uint8Array<br>Length: 1-256 bytes                           | No    | Asset alias, which uniquely identifies an asset.                      |
15| ACCESSIBILITY         | Type: number<br>Value range: see [Accessibility](../../reference/apis-asset-store-kit/js-apis-asset.md#accessibility)| No    | Access control based on the lock screen status.                               |
16| REQUIRE_PASSWORD_SET  | Type: bool                                                  | No    | Whether the asset is accessible only when a lock screen password is set.                |
17| AUTH_TYPE             | Type: number<br>Value range: see [AuthType](../../reference/apis-asset-store-kit/js-apis-asset.md#authtype)| No    | Type of user authentication required for accessing the asset.                              |
18| SYNC_TYPE             | Type: number<br>Value range: see [SyncType](../../reference/apis-asset-store-kit/js-apis-asset.md#synctype)| No    | Type of sync supported by the asset.                                     |
19| IS_PERSISTENT         | Type: bool                                                  | No    | Whether to retain the asset when the application is uninstalled.                            |
20| DATA_LABEL_CRITICAL_1 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
21| DATA_LABEL_CRITICAL_2 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
22| DATA_LABEL_CRITICAL_3 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
23| DATA_LABEL_CRITICAL_4 | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
24| DATA_LABEL_NORMAL_1   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
25| DATA_LABEL_NORMAL_2   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
26| DATA_LABEL_NORMAL_3   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
27| DATA_LABEL_NORMAL_4   | Type: Uint8Array<br>Length: 1-2048 bytes                       | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
28| DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
29| DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
30| DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
31| DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: Uint8Array<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
32| RETURN_TYPE           | Type: number<br>Value range: see [ReturnType](../../reference/apis-asset-store-kit/js-apis-asset.md#returntype)| No    | Type of the asset query result to return.            |
33| RETURN_LIMIT          | Type: number                                                | No    | Maximum number of asset records to return.                                        |
34| RETURN_OFFSET         | Type: number<br>Value range: 1-65536                             | No    | Offset of the asset query result.<br>**NOTE**: This parameter specifies the starting asset record to return in batch asset query.                                |
35| RETURN_ORDERED_BY     | Type: number<br>Value: asset.Tag.DATA_LABEL_xxx.            | No    | How the query results are sorted. Currently, the results can be sorted only by **DATA_LABEL**.<br>**NOTE**: By default, assets are returned in the order in which they are added.|
36| REQUIRE_ATTR_ENCRYPTED<sup>14+</sup> | Type: bool| No| Whether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.|
37
38## Constraints
39
40The assets queried are transmitted to the service through an IPC channel. Due to the limitation of the IPC buffer size, the maximum number of assets to be queried at a time cannot exceed 40.
41
42## Example
43
44> **NOTE**
45>
46> The **asset** module provides an asynchronous API and a synchronous API for querying assets. The following uses the asynchronous API as an example. For more information about the APIs, see [Asset Store Service](../../reference/apis-asset-store-kit/js-apis-asset.md).
47
48### Querying the Plaintext of an Asset
49
50Query the plaintext of asset **demo_alias**.
51
52```typescript
53import { asset } from '@kit.AssetStoreKit';
54import { util } from '@kit.ArkTS';
55import { BusinessError } from '@kit.BasicServicesKit';
56
57function stringToArray(str: string): Uint8Array {
58  let textEncoder = new util.TextEncoder();
59  return textEncoder.encodeInto(str);
60}
61
62function arrayToString(arr: Uint8Array): string {
63  let textDecoder = util.TextDecoder.create("utf-8", { ignoreBOM: true });
64  let str = textDecoder.decodeToString(arr, { stream: false })
65  return str;
66}
67
68let query: asset.AssetMap = new Map();
69query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query.
70query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ALL); // Return all asset information, including attributes and asset plaintext.
71try {
72  asset.query(query).then((res: Array<asset.AssetMap>) => {
73    for (let i = 0; i < res.length; i++) {
74      // Parse the secret.
75      let secret: Uint8Array = res[i].get(asset.Tag.SECRET) as Uint8Array;
76      // Convert uint8array to string
77      let secretStr: string = arrayToString(secret);
78    }
79  }).catch ((err: BusinessError) => {
80    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
81  });
82} catch (error) {
83  let err = error as BusinessError;
84  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
85}
86```
87
88### Querying Attributes of an Asset
89
90Query attributes of asset **demo_alias**.
91
92```typescript
93import { asset } from '@kit.AssetStoreKit';
94import { util } from '@kit.ArkTS';
95import { BusinessError } from '@kit.BasicServicesKit';
96
97function stringToArray(str: string): Uint8Array {
98  let textEncoder = new util.TextEncoder();
99  return textEncoder.encodeInto(str);
100}
101
102let query: asset.AssetMap = new Map();
103query.set(asset.Tag.ALIAS, stringToArray('demo_alias')); // Specify the alias of the asset to query.
104query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
105try {
106  asset.query(query).then((res: Array<asset.AssetMap>) => {
107    for (let i = 0; i < res.length; i++) {
108      // Parse the attributes.
109      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
110    }
111  }).catch ((err: BusinessError) => {
112    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
113  });
114} catch (error) {
115  let err = error as BusinessError;
116  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
117}
118```
119
120### Querying Attributes of Assets
121
122Query attributes of assets with tag 1 of **demo_label** and return a total of 10 records sorted by **DATA_LABEL_NORMAL_1** starting from the fifth record that matches the search criteria.
123
124```typescript
125import { asset } from '@kit.AssetStoreKit';
126import { util } from '@kit.ArkTS';
127import { BusinessError } from '@kit.BasicServicesKit';
128
129function stringToArray(str: string): Uint8Array {
130  let textEncoder = new util.TextEncoder();
131  return textEncoder.encodeInto(str);
132}
133
134let query: asset.AssetMap = new Map();
135query.set(asset.Tag.RETURN_TYPE, asset.ReturnType.ATTRIBUTES); // Return only the attributes of the asset, that is, the result does not include the asset plaintext.
136query.set(asset.Tag.DATA_LABEL_NORMAL_1, stringToArray('demo_label'));
137query.set(asset.Tag.RETURN_OFFSET, 5); // Return results from the fifth asset that matches the search criteria.
138query.set(asset.Tag.RETURN_LIMIT, 10); // Return information about 10 assets that match the search criteria.
139query.set(asset.Tag.RETURN_ORDERED_BY, asset.Tag.DATA_LABEL_NORMAL_1); // Sort the query results by DATA_LABEL_NORMAL_1.
140try {
141  asset.query(query).then((res: Array<asset.AssetMap>) => {
142    for (let i = 0; i < res.length; i++) {
143      // Parse the attributes.
144      let accessibility: number = res[i].get(asset.Tag.ACCESSIBILITY) as number;
145    }
146  }).catch ((err: BusinessError) => {
147    console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
148  });
149} catch (error) {
150  let err = error as BusinessError;
151  console.error(`Failed to query Asset. Code is ${err.code}, message is ${err.message}`);
152}
153```
154