• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.data.graphStore (图数据库)(系统接口)
2
3图数据库(Graph Database,GDB)是以顶点([Vertex](#vertex))、边([Edge](#edge))为基础存储单元,以高效存储、查询图数据为设计原理的数据管理系统。图数据库提供了一套完整的对本地数据库进行管理的机制,对外提供了一系列的读(查询)、写(增删改)、事务管理等接口,可以直接运行用户输入的GQL(Graph Query Language,图查询语言)语句来满足存储、查询、分析高度互联数据的场景需要。
4
5- [Vertex](#vertex):顶点,代表实体或实例,例如人员、企业、帐户或要跟踪的任何其他项目。它们大致相当于关系数据库中的记录、关系或行,或者文档存储数据库中的文档。
6- [Edge](#edge):边,也称作关系,将节点连接到其他节点的线;代表节点之间的关系。边是图数据库中的关键概念,图数据库独有的数据抽象概念,而关系型数据库和文件型数据库并没有“边”这一概念,它们的关系查询必须在运行时进行具体化。
7- [Path](#path):路径,由顶点和边按照一定顺序组成的序列。
8- [PathSegment](#pathsegment):路径段,路径中的某一条边及其起点、终点。
9
10该模块提供以下图数据库相关的常用功能:
11
12- [GraphStore](#graphstore):提供管理图数据库(GDB)方法的接口。
13- [Result](#result):提供用户调用图数据库读、写接口之后返回的结果。
14- [Transaction](#transaction):提供管理事务对象的接口。
15
16> **说明:**
17>
18> - 本模块首批接口从API version 18开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
19> - 当前页面仅包含本模块的系统接口。
20
21## 导入模块
22
23```ts
24import { graphStore } from '@kit.ArkData';
25```
26
27## graphStore.getStore
28
29getStore(context: Context, config: StoreConfig): Promise<GraphStore>
30
31获得一个相关的GraphStore实例,操作图数据库,用户可以根据自己的需求配置GraphStore实例的参数,然后通过GraphStore实例调用相关接口可以执行相关的数据操作,使用promise异步回调。
32
33**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
34
35**参数:**
36
37| 参数名  | 类型                             | 必填 | 说明                                                         |
38| ------- | -------------------------------- | ---- | ------------------------------------------------------------ |
39| context | Context                          | 是   | 应用的上下文,推荐使用Stage模型。 <br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 |
40| config  | [StoreConfig](#storeconfig) | 是   | 与此GDB存储相关的数据库配置。                                |
41
42**返回值**:
43
44| 类型                                      | 说明                              |
45| ----------------------------------------- | --------------------------------- |
46| Promise&lt;[GraphStore](#graphstore)&gt; | Promise对象。返回GraphStore对象。 |
47
48**错误码:**
49
50以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
51
52| **错误码ID** | **错误信息**                                                 |
53|-----------| ------------------------------------------------------------ |
54| 202  | Permission verification failed, application which is not a system application uses system API. |
55| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
56| 31300000  | Inner error. |
57| 31300001  | Database corrupted. |
58| 31300014  | Invalid database path. |
59| 31300015  | Config changed. |
60
61**示例:**
62
63```ts
64import { UIAbility } from '@kit.AbilityKit';
65import { window } from '@kit.ArkUI';
66import { BusinessError } from '@kit.BasicServicesKit';
67
68let store: graphStore.GraphStore | null = null;
69
70class EntryAbility extends UIAbility {
71  onWindowStageCreate(windowStage: window.WindowStage) {
72    const STORE_CONFIG: graphStore.StoreConfig = {
73      name: "testGraphDb",
74      securityLevel: graphStore.SecurityLevel.S2
75    };
76
77    graphStore.getStore(this.context, STORE_CONFIG).then(async (gdb: graphStore.GraphStore) => {
78      store = gdb;
79      console.info('Get GraphStore successfully.')
80    }).catch((err: BusinessError) => {
81      console.error(`Get GraphStore failed, code is ${err.code}, message is ${err.message}`);
82    })
83  }
84}
85```
86
87## graphStore.deleteStore
88
89deleteStore(context: Context, config: StoreConfig): Promise&lt;void&gt;
90
91使用指定的数据库文件配置删除数据库,使用Promise异步回调。
92
93删除前,如果数据库未关闭,建议使用[close](#close)接口关闭数据后再进行删除。删除成功后,打开的数据库句柄已无效,建议将数据库对象置为null,不再使用。
94
95**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
96
97**参数**
98
99| 参数名  | 类型    | 必填 | 说明                                                         |
100| ------- | ------- | ---- | ------------------------------------------------------------ |
101| context | Context                          | 是   | 应用的上下文,推荐使用Stage模型。 <br>FA模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-app-context.md)。<br>Stage模型的应用Context定义见[Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md)。 |
102| config  | [StoreConfig](#storeconfig) | 是   | 与此GDB存储相关的数据库配置。<br/>**使用约束:** <br/>删除数据库时仅以config.name为标识,不关注其他配置项。 |
103
104**返回值**:
105
106| 类型                | 说明                      |
107| ------------------- | ------------------------- |
108| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
109
110**错误码:**
111
112以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
113
114| **错误码ID** | **错误信息**                                                 |
115|-----------| ------------------------------------------------------------ |
116| 202  | Permission verification failed, application which is not a system application uses system API. |
117| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
118| 31300000  | Inner error. |
119| 31300014  | Invalid database path. |
120
121**示例:**
122
123```ts
124import { UIAbility } from '@kit.AbilityKit';
125import { window } from '@kit.ArkUI';
126import { BusinessError } from '@kit.BasicServicesKit';
127
128let store: graphStore.GraphStore | null = null;
129
130const STORE_CONFIG: graphStore.StoreConfig = {
131  name: "testGraphDb",
132  securityLevel: graphStore.SecurityLevel.S2
133};
134
135class EntryAbility extends UIAbility {
136  onWindowStageCreate(windowStage: window.WindowStage){
137    graphStore.deleteStore(this.context, STORE_CONFIG).then(()=>{
138      store = null;
139      console.info('Delete GraphStore successfully.');
140    }).catch((err: BusinessError) => {
141      console.error(`Delete GraphStore failed, code is ${err.code},message is ${err.message}`);
142    })
143  }
144}
145```
146
147## StoreConfig
148
149管理图数据库配置。
150
151**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
152
153| 名称        | 类型          | 必填 | 说明                                                      |
154| ------------- | ------------- | ---- | --------------------------------------------------------- |
155| name          | string        | 是   | 数据库文件名,也是数据库唯一标识符。<br/>**使用约束:** <br/>1. 文件名长度上限为128字节。<br/>2. 文件名不包含.db后缀。 |
156| securityLevel | [SecurityLevel](#securitylevel) | 是   | 设置数据库安全级别。<br/>**使用约束:** <br/>1. 安全级别不支持从高到低变更。<br/>2. 同一个数据库如果需要变更安全等级,需要使用[close](#close)接口关闭已经打开的数据库,再将修改安全等级后的StoreConfig作为参数重新调用[getStore](#graphstoregetstore)。 |
157| encrypt | boolean       | 否   | 指定数据库是否加密,默认不加密。<br/> true:加密。<br/> false:非加密。<br/>**使用约束:** <br/>1. 不支持从加密数据库到非加密数据库的变更。<br/>2. 同一个数据库如果需要从非加密数据库变更为加密数据库,需要使用[close](#close)接口关闭已经打开的数据库,再将修改加密标志后的StoreConfig作为参数重新调用[getStore](#graphstoregetstore)。 |
158
159## SecurityLevel
160
161数据库的安全级别枚举。请使用枚举名称而非枚举值。
162
163**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
164
165| 名称 | 值   | 说明                                                         |
166| ---- | ---- | ------------------------------------------------------------ |
167| S1   | 1    | 表示数据库的安全级别为低级别,当数据泄露时会产生较低影响。例如,包含壁纸等系统数据的数据库。 |
168| S2   | 2    | 表示数据库的安全级别为中级别,当数据泄露时会产生较大影响。例如,包含录音、视频等用户生成数据或通话记录等信息的数据库。 |
169| S3   | 3    | 表示数据库的安全级别为高级别,当数据泄露时会产生重大影响。例如,包含用户运动、健康、位置等信息的数据库。 |
170| S4   | 4    | 表示数据库的安全级别为关键级别,当数据泄露时会产生严重影响。例如,包含认证凭据、财务数据等信息的数据库。 |
171
172## ValueType
173
174type ValueType = null | number | string
175
176用于表示允许的数据字段类型,接口参数具体类型根据其功能而定。
177
178**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
179
180| 类型    | 说明                 |
181| ------- | -------------------- |
182| null    | 表示值类型为空。 |
183| number  | 表示值类型为数字,可取任意值。 |
184| string  | 表示值类型为字符,可取任意值。 |
185
186## Vertex
187
188记录顶点的相关信息。Vertex仅作为返回值([Result](#result))中的类型出现,不支持自定义填写,可通过使用[read](#read)接口查询获得。
189
190**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
191
192| 名称          | 类型                          | 必填  | 说明           |
193| ----------- | --------------------------- | --- | ------------ |
194| vid        | string | 是   | 顶点的标识符。不支持自定义填写,使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口插入顶点后由底层分配的全局唯一标识符。 |
195| labels     | Array&lt;string&gt; | 是   | 顶点的标签。在使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口创建图使用的GQL语句中指定,每个元素长度上限为128字节,不区分大小写,以大写形式存储。 |
196| properties | Record&lt;string, [ValueType](#valuetype)&gt; | 是   | 顶点的属性。key长度上限为128字节,数量上限为1024条,在使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口创建图使用的GQL语句中指定,value类型为string时长度上限为64 * 1024字节。 |
197
198## Edge
199
200记录边的相关信息。Edge仅作为返回值([Result](#result))中的类型出现,不支持自定义填写,可通过使用[read](#read)接口查询获得。
201
202**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
203
204| 名称          | 类型                          | 必填  | 说明           |
205| ----------- | --------------------------- | --- | ------------ |
206| eid        | string | 是   | 边的标识符。不支持自定义填写,使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口插入边后由底层分配的全局唯一标识符。 |
207| type       | string | 是   | 边的类型。在使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口创建图使用的GQL语句中指定,长度上限为128字节,不区分大小写,以大写形式存储。 |
208| startVid   | string | 是   | 起始顶点的标识符。不支持自定义填写,使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口插入顶点后由底层分配的全局唯一标识符。 |
209| endVid     | string | 是   | 终点的标识符。不支持自定义填写,使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口插入顶点后由底层分配的全局唯一标识符。 |
210| properties | Record&lt;string, [ValueType](#valuetype)&gt; | 是   | 边的属性。key长度上限为128字节,数量上限为1024条,在使用[GraphStore.write](#write-1)或[Transaction.write](#write)接口创建图使用的GQL语句中指定,value类型为string时长度上限为64 * 1024字节。 |
211
212## PathSegment
213
214记录路径段(路径中的每条边及其起点、终点)的相关信息。PathSegment仅作为([Path.segments](#path))的类型出现,不支持自定义填写。
215
216**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
217
218| 名称          | 类型                          | 必填  | 说明           |
219| ----------- | --------------------------- | --- | ------------ |
220| start | [Vertex](#vertex) | 是   | 路径段的起始顶点。 |
221| end   | [Vertex](#vertex) | 是   | 路径段的终点。 |
222| edge  | [Edge](#edge)     | 是   | 路径段的边。 |
223
224## Path
225
226记录路径的相关信息。Path仅作为返回值([Result](#result))中的类型出现,不支持自定义填写,可通过使用[read](#read)接口查询获得。
227
228**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
229
230| 名称          | 类型                          | 必填  | 说明           |
231| ----------- | --------------------------- | --- | ------------ |
232| start    | [Vertex](#vertex) | 是   | 路径的起始顶点。 |
233| end      | [Vertex](#vertex) | 是   | 路径的终点。 |
234| length   | number            | 是   | 路径的长度,即路径中路径段的数量(segments.length),上限为1024条。 |
235| segments | Array&lt;[PathSegment](#pathsegment)&gt; | 是   | 路径中所有路径段。 |
236
237## Result
238
239GQL语句执行结果。
240
241**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
242
243| 名称          | 类型                          | 必填  | 说明           |
244| ----------- | --------------------------- | --- | ------------ |
245| records | Array&lt;Record&lt;string, Object&gt;&gt; | 否   | GQL语句执行结果的数据记录。默认值为空。<br/>如果有执行结果,Object具体类型根据查询GQL语句不同,可能是[Vertex](#vertex),[Edge](#edge),[Path](#path)或[ValueType](#valuetype)。 |
246
247
248## Transaction
249
250提供用于管理图数据库的事务处理方法的接口。
251
252不同事务对象之间的操作是隔离的,当前图数据库使用库级锁。所以如果存在一个事务使用[write](#write)接口写入后未使用[commit](#commit)接口提交或使用[rollback](#rollback)接口回滚,此时其他Transaction实例使用[read](#read)/[write](#write)或GraphStore实例使用[GraphStore.read](#read-1)/[GraphStore.write](#write-1)进行读写操作时会返回31300003错误码。另外如果存在一个事务使用[read](#read)接口查询后未使用[commit](#commit)接口提交或使用[rollback](#rollback)接口回滚,此时其他Transaction实例使用[write](#write)或GraphStore实例使用[GraphStore.write](#write-1)进行写操作时也会返回31300003错误码。
253
254下列API示例中都需先使用[createTransaction](#createtransaction)接口获取到Transaction实例,再通过此实例调用对应方法。
255
256### read
257
258read(gql: string): Promise&lt;Result&gt;
259
260执行数据查询语句。查询范围包括使用[GraphStore.write](#write-1)接口写入的数据、其他事务中使用[write](#write)接口写入并使用[commit](#commit)接口提交的数据以及当前事务中使用[write](#write)接口写入的数据。
261
262**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
263
264**参数:**
265
266| 参数名 | 类型                          | 必填 | 说明                       |
267| ------ | ----------------------------- | ---- | -------------------------- |
268| gql  | string  | 是   | 要执行的GQL语句。长度上限为1024 * 1024字节。 |
269
270**返回值**:
271
272| 类型                  | 说明                                              |
273| --------------------- | ------------------------------------------------- |
274| Promise&lt;[Result](#result)&gt; | Promise对象。如果操作成功,返回GQL语句执行结果。 |
275
276**错误码:**
277
278以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
279
280| **错误码ID** | **错误信息**                                                 |
281|-----------| ------------------------------------------------------------ |
282| 202  | Permission verification failed, application which is not a system application uses system API. |
283| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
284| 31300000  | Inner error. |
285| 31300001  | Database corrupted. |
286| 31300002  | Already closed. |
287| 31300003  | The database is busy. |
288| 31300004  | The database is out of memory. |
289| 31300005  | The database is full. |
290| 31300006  | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. |
291| 31300007  | The graph name, vertex or edge type, or vertex or edge property is not defined. |
292| 31300008  | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. |
293| 31300009  | The GQL statement syntax error. |
294| 31300010  | The GQL statement semantic error. |
295
296**示例:**
297
298```ts
299import { BusinessError } from '@kit.BasicServicesKit';
300
301const QUERY_PATH_GQL = "MATCH path=(a:Person {name : \'name_1\'})-[]->{2, 2}(b:Person {name : \'name_3\'}) RETURN path;"
302if(transaction != undefined) {
303  (transaction as graphStore.Transaction).read(QUERY_PATH_GQL).then((result: graphStore.Result) => {
304    console.info('Read successfully');
305    result.records?.forEach((data) => {
306      for (let item of Object.entries(data)) {
307        const key = item[0];
308        const value = item[1];
309        const path = value as graphStore.Path;
310        console.info(`key : ${key}, path.length : ${path.length}`);
311      }
312    });
313  }).catch((err: BusinessError) => {
314    console.error(`Read failed, code is ${err.code}, message is ${err.message}`);
315  })
316}
317```
318
319### write
320
321write(gql: string): Promise&lt;Result&gt;
322
323执行数据写入语句。用于增删改数据等。暂不支持修改表结构([Vertex](#vertex)和[Edge](#edge)的类型或属性)、创建/删除图以及创建/删除索引。不支持使用本接口执行事务操作语句,包括开启事务(START TRANSACTION)、提交事务(COMMIT)以及回滚事务(ROLLBACK)。
324
325**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
326
327**参数:**
328
329| 参数名 | 类型                          | 必填 | 说明                       |
330| ------ | ----------------------------- | ---- | -------------------------- |
331| gql  | string  | 是   | 要执行的GQL语句。长度上限为1024 * 1024字节。 |
332
333**返回值**:
334
335| 类型                  | 说明                                              |
336| --------------------- | ------------------------------------------------- |
337| Promise&lt;[Result](#result)&gt; | Promise对象。如果操作成功,返回GQL语句执行结果。 |
338
339**错误码:**
340
341以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
342
343| **错误码ID** | **错误信息**                                                 |
344|-----------| ------------------------------------------------------------ |
345| 202  | Permission verification failed, application which is not a system application uses system API. |
346| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
347| 31300000  | Inner error. |
348| 31300001  | Database corrupted. |
349| 31300002  | Already closed. |
350| 31300003  | The database is busy. |
351| 31300004  | The database is out of memory. |
352| 31300005  | The database is full. |
353| 31300006  | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. |
354| 31300007  | The graph name, vertex or edge type, or vertex or edge property is not defined. |
355| 31300008  | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. |
356| 31300009  | The GQL statement syntax error. |
357| 31300010  | The GQL statement semantic error. |
358| 31300012  | The number of graph names, vertex or edge types, or vertex or edge properties exceeds the limit. |
359| 31300013  | A conflicting constraint already exists. |
360
361**示例:**
362
363```ts
364import { BusinessError } from '@kit.BasicServicesKit';
365
366const INSERT_GQL = "INSERT (:Person {name: 'name_1', age: 11});"
367if(transaction != undefined) {
368  (transaction as graphStore.Transaction).write(INSERT_GQL).then(() => {
369    console.info('Write successfully');
370  }).catch((err: BusinessError) => {
371    console.error(`Write failed, code is ${err.code}, message is ${err.message}`);
372  })
373}
374```
375
376### commit
377
378commit(): Promise&lt;void&gt;
379
380提交当前事务中已经执行的GQL语句,事务提交后,当前事务不再可用。
381
382**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
383
384**返回值**:
385
386| 类型                | 说明                      |
387| ------------------- | ------------------------- |
388| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
389
390**错误码:**
391
392以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
393
394| **错误码ID** | **错误信息**                                                 |
395|-----------| ------------------------------------------------------------ |
396| 202  | Permission verification failed, application which is not a system application uses system API. |
397| 31300000  | Inner error. |
398| 31300001  | Database corrupted. |
399| 31300002  | Already closed. |
400| 31300003  | The database is busy. |
401| 31300004  | The database is out of memory. |
402| 31300005  | The database is full. |
403
404**示例:**
405
406```ts
407import { BusinessError } from '@kit.BasicServicesKit';
408
409if(transaction != undefined) {
410  (transaction as graphStore.Transaction).commit().then(() => {
411    console.info(`Commit successfully`);
412  }).catch ((err: BusinessError) => {
413    console.error(`Commit failed, code is ${err.code}, message is ${err.message}`);
414  })
415}
416```
417
418### rollback
419
420rollback(): Promise&lt;void&gt;
421
422回滚当前事务中已经执行的GQL语句,事务回滚后,当前事务不再可用。
423
424**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
425
426**返回值**:
427
428| 类型                | 说明                      |
429| ------------------- | ------------------------- |
430| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
431
432**错误码:**
433
434以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
435
436| **错误码ID** | **错误信息**                                                 |
437|-----------| ------------------------------------------------------------ |
438| 202  | Permission verification failed, application which is not a system application uses system API. |
439| 31300000  | Inner error. |
440| 31300001  | Database corrupted. |
441| 31300002  | Already closed. |
442| 31300003  | The database is busy. |
443| 31300004  | The database is out of memory. |
444| 31300005  | The database is full. |
445
446**示例:**
447
448```ts
449import { BusinessError } from '@kit.BasicServicesKit';
450
451if(transaction != undefined) {
452  (transaction as graphStore.Transaction).rollback().then(() => {
453    console.info(`Rollback successfully`);
454  }).catch ((err: BusinessError) => {
455    console.error(`Rollback failed, code is ${err.code}, message is ${err.message}`);
456  })
457}
458```
459
460
461## GraphStore
462
463提供管理图数据库(GDB)方法的接口。
464
465下列API示例中都需先使用[getStore](#graphstoregetstore)接口获取到GraphStore实例,再通过此实例调用对应方法。
466
467### read
468
469read(gql: string): Promise&lt;Result&gt;
470
471执行数据查询语句。查询范围包括使用[write](#write-1)接口写入的数据以及所有事务中使用[Transaction.write](#write)接口写入并使用[Transaction.commit](#commit)接口提交的数据。
472
473**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
474
475**参数:**
476
477| 参数名 | 类型                          | 必填 | 说明                       |
478| ------ | ----------------------------- | ---- | -------------------------- |
479| gql  | string  | 是   | 要执行的GQL语句。长度上限为1024 * 1024字节。 |
480
481**返回值**:
482
483| 类型                  | 说明                                              |
484| --------------------- | ------------------------------------------------- |
485| Promise&lt;[Result](#result)&gt; | Promise对象。如果操作成功,返回GQL语句执行结果。 |
486
487**错误码:**
488
489以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
490
491| **错误码ID** | **错误信息**                                                 |
492|-----------| ------------------------------------------------------------ |
493| 202  | Permission verification failed, application which is not a system application uses system API. |
494| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
495| 31300000  | Inner error. |
496| 31300001  | Database corrupted. |
497| 31300002  | Already closed. |
498| 31300003  | The database is busy. |
499| 31300004  | The database is out of memory. |
500| 31300005  | The database is full. |
501| 31300006  | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. |
502| 31300007  | The graph name, vertex or edge type, or vertex or edge property is not defined. |
503| 31300008  | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. |
504| 31300009  | The GQL statement syntax error. |
505| 31300010  | The GQL statement semantic error. |
506
507**示例:**
508
509```ts
510import { BusinessError } from '@kit.BasicServicesKit';
511
512const QUERY_PATH_GQL = "MATCH path=(a:Person {name : \'name_1\'})-[]->{2, 2}(b:Person {name : \'name_3\'}) RETURN path;"
513if(store != null) {
514  (store as graphStore.GraphStore).read(QUERY_PATH_GQL).then((result: graphStore.Result) => {
515    console.info('Read successfully');
516    result.records?.forEach((data) => {
517      for (let item of Object.entries(data)) {
518        const key = item[0];
519        const value = item[1];
520        const path = value as graphStore.Path;
521        console.info(`key : ${key}, path.length : ${path.length}`);
522      }
523    });
524  }).catch((err: BusinessError) => {
525    console.error(`Read failed, code is ${err.code}, message is ${err.message}`);
526  })
527}
528```
529
530### write
531
532write(gql: string): Promise&lt;Result&gt;
533
534执行数据写入语句。用于创建图、删除图、增删改数据等。暂不支持修改表结构([Vertex](#vertex)和[Edge](#edge)的类型或属性)。不支持使用本接口执行事务操作语句,包括开启事务(START TRANSACTION)、提交事务(COMMIT)以及回滚事务(ROLLBACK)。
535
536**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
537
538**参数:**
539
540| 参数名 | 类型                          | 必填 | 说明                       |
541| ------ | ----------------------------- | ---- | -------------------------- |
542| gql  | string  | 是   | 要执行的GQL语句。长度上限为1024 * 1024字节。 |
543
544**返回值**:
545
546| 类型                  | 说明                                              |
547| --------------------- | ------------------------------------------------- |
548| Promise&lt;[Result](#result)&gt; | Promise对象。如果操作成功,返回GQL语句执行结果。 |
549
550**错误码:**
551
552以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
553
554| **错误码ID** | **错误信息**                                                 |
555|-----------| ------------------------------------------------------------ |
556| 202  | Permission verification failed, application which is not a system application uses system API. |
557| 401  | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
558| 31300000  | Inner error. |
559| 31300001  | Database corrupted. |
560| 31300002  | Already closed. |
561| 31300003  | The database is busy. |
562| 31300004  | The database is out of memory. |
563| 31300005  | The database is full. |
564| 31300006  | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. |
565| 31300007  | The graph name, vertex or edge type, or vertex or edge property is not defined. |
566| 31300008  | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. |
567| 31300009  | The GQL statement syntax error. |
568| 31300010  | The GQL statement semantic error. |
569| 31300012  | The number of graph names, vertex or edge types, or vertex or edge properties exceeds the limit. |
570| 31300013  | A conflicting constraint already exists. |
571
572**示例:**
573
574```ts
575import { BusinessError } from '@kit.BasicServicesKit';
576
577const CREATE_GRAPH_GQL = "CREATE GRAPH test { (person:Person {name STRING, age INT}),(person)-[:Friend {year INT}]->(person) };"
578if(store != null) {
579  (store as graphStore.GraphStore).write(CREATE_GRAPH_GQL).then(() => {
580    console.info('Write successfully');
581  }).catch((err: BusinessError) => {
582    console.error(`Write failed, code is ${err.code}, message is ${err.message}`);
583  })
584}
585```
586
587### createTransaction
588
589createTransaction(): Promise&lt;Transaction&gt;
590
591创建事务实例。当前图数据库同一时刻至多只能创建4个事务实例。
592
593**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
594
595**返回值**:
596
597| 类型                | 说明                      |
598| ------------------- | ------------------------- |
599| Promise&lt;[Transaction](#transaction)&gt; | 如果操作成功,返回创建的事务实例。 |
600
601**错误码:**
602
603以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
604
605| **错误码ID** | **错误信息**                                                 |
606|-----------| ------------------------------------------------------------ |
607| 202  | Permission verification failed, application which is not a system application uses system API. |
608| 31300000  | Inner error. |
609| 31300001  | Database corrupted. |
610| 31300002  | Already closed. |
611| 31300003  | The database is busy. |
612| 31300004  | The database is out of memory. |
613| 31300005  | The database is full. |
614
615**示例:**
616
617```ts
618import { BusinessError } from '@kit.BasicServicesKit';
619
620let transaction: graphStore.Transaction | undefined = undefined;
621
622if(store != undefined) {
623  (store as graphStore.GraphStore).createTransaction().then((trans: graphStore.Transaction) => {
624    transaction = trans;
625    console.info('createTransaction successfully');
626  }).catch((err: BusinessError) => {
627    console.error(`createTransaction failed, code is ${err.code}, message is ${err.message}`);
628  })
629}
630```
631
632### close
633
634close(): Promise&lt;void&gt;
635
636关闭图数据库(GDB),未提交的事务将被回滚。
637
638**系统能力:** SystemCapability.DistributedDataManager.DataIntelligence.Core
639
640**返回值**:
641
642| 类型                | 说明                      |
643| ------------------- | ------------------------- |
644| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
645
646**错误码:**
647
648以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[图数据库错误码](errorcode-data-gdb.md)。
649
650| **错误码ID** | **错误信息**                                                 |
651|-----------| ------------------------------------------------------------ |
652| 202  | Permission verification failed, application which is not a system application uses system API. |
653| 31300000  | Inner error. |
654
655**示例:**
656
657```ts
658import { BusinessError } from '@kit.BasicServicesKit';
659
660if(store != null) {
661  (store as graphStore.GraphStore).close().then(() => {
662    console.info(`Close successfully`);
663  }).catch ((err: BusinessError) => {
664    console.error(`Close failed, code is ${err.code}, message is ${err.message}`);
665  })
666}
667```