1# @ohos.data.graphStore (Graph Database) (System API) 2 3A graph database (GDB) is a data management system designed to efficiently store and query graph data. It organizes data as [vertexes](#vertex) and [edges](#edge). The **graphStore** module provides a complete set of mechanisms for local GDB management. It provides read (query), write (add, delete, and modify), and transaction management APIs that can directly run the Graph Query Language (GQL) statements to store, query, and analyze highly interconnected data. 4 5- [Vertex](#vertex): represents an entity or instance, such as a person, enterprise, account, or any other item to be tracked. It is equivalent to a record, relationship, or row in a relational database, or a document in a document database. 6- [Edge](#edge): represents the relationship between vertexes. Edge is a unique data abstraction exclusive to GDB, and must be materialized at runtime when queried. 7- [Path](#path): a sequence of vertices and edges in a certain sequence. 8- [PathSegment](#pathsegment): an edge along with its start and end vertexes in a path. 9 10The **graphStore** module provides the following functionalities: 11 12- [GraphStore](#graphstore): provides APIs for manipulating a GDB. 13- [Result](#result): provides the result returned by **read()** or **write()**. 14- [Transaction](#transaction): provides APIs for managing transaction objects. 15 16> **NOTE** 17> 18> - The initial APIs of this module are supported since API version 18. Newly added APIs will be marked with a superscript to indicate their earliest API version. 19> - The current page contains only the system interfaces of this module. 20 21## Modules to Import 22 23```ts 24import { graphStore } from '@kit.ArkData'; 25``` 26 27## graphStore.getStore 28 29getStore(context: Context, config: StoreConfig): Promise<GraphStore> 30 31Obtains a GraphStore instance for GDB operations. You can set parameters for the instance as required and call related APIs through this instance to perform data operations. This API uses a promise to return the result. 32 33**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 34 35**Parameters** 36 37| Name | Type | Mandatory| Description | 38| ------- | -------------------------------- | ---- | ------------------------------------------------------------ | 39| context | Context | Yes | Application context. The stage model is recommended.<br>For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| 40| config | [StoreConfig](#storeconfig) | Yes | Configuration of the GDB. | 41 42**Return value** 43 44| Type | Description | 45| ----------------------------------------- | --------------------------------- | 46| Promise<[GraphStore](#graphstore)> | Promise used to return the **GraphStore** instance obtained.| 47 48**Error codes** 49 50For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 51 52| **ID**| **Error Message** | 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**Example** 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<void> 90 91Deletes a graph store. This API uses a promise to return the result. 92 93Before calling this API, call [close](#close) to close the graph store to be deleted. After the deletion, the opened graph store handle becomes invalid. You are advised to set the graph store instance to null. 94 95**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 96 97**Parameters** 98 99| Name | Type | Mandatory| Description | 100| ------- | ------- | ---- | ------------------------------------------------------------ | 101| context | Context | Yes | Application context. The stage model is recommended.<br>For details about the application context of the FA model, see [Context](../apis-ability-kit/js-apis-inner-app-context.md).<br>For details about the application context of the stage model, see [Context](../apis-ability-kit/js-apis-inner-application-uiAbilityContext.md).| 102| config | [StoreConfig](#storeconfig) | Yes | Configuration of the graph store to delete.<br>**Constraints**<br>Only **config.name** is used to identify the graph store to be deleted.| 103 104**Return value** 105 106| Type | Description | 107| ------------------- | ------------------------- | 108| Promise<void> | Promise that returns no value.| 109 110**Error codes** 111 112For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 113 114| **ID**| **Error Message** | 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**Example** 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 149Represents the configuration of a graph store. 150 151**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 152 153| Name | Type | Mandatory| Description | 154| ------------- | ------------- | ---- | --------------------------------------------------------- | 155| name | string | Yes | Database file name, which is the unique identifier of the graph store.<br>**Constraints**<br>1. The file name cannot exceed 128 bytes.<br>2. The file name does not include the .db filename extension.| 156| securityLevel | [SecurityLevel](#securitylevel) | Yes | Security level of the graph store.<br>**Constraints**<br>1. The security level cannot be downgraded. For example, you can change the security level from S2 to S3, but not from S3 to S2.<br>2. If the security level of a graph store needs to be changed, call [close](#close) to close the graph store instance, and then call [getStore](#graphstoregetstore) with **StoreConfig**.| 157| encrypt | boolean | No | Whether to encrypt the graph store.<br>The value **true** means to encrypt the graph store; the value **false** means the opposite.<br>**Constraints**<br>1. It is not allowed to change **encrypt** from **true** to **false**.<br>2. If you need to change **encrypt** from **false** to **true**, call [close](#close) to close the graph store instance, and then call [getStore](#graphstoregetstore) with **StoreConfig**. | 158 159## SecurityLevel 160 161Enumerates the graph store security levels. Use the enum name rather than the enum value. 162 163**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 164 165| Name| Value | Description | 166| ---- | ---- | ------------------------------------------------------------ | 167| S1 | 1 | Low security level, where any data leakage may result in minor impact. An example would be a graph store containing non-sensitive system data such as wallpapers.| 168| S2 | 2 | Medium security level, where any data leakage may result in moderate impact. An example would be a graph store containing audio and video data created by users or call logs.| 169| S3 | 3 | High security level, where any data leakage may result in severe impact. An example would be a graph store containing user fitness, health, and location data.| 170| S4 | 4 | Critical security level, where any data leakage may result in critical impact. An example would be a graph store containing authentication credentials and financial data.| 171 172## ValueType 173 174type ValueType = null | number | string 175 176Represents the allowed value types. The value type varies, depending on its usage. 177 178**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 179 180| Type | Description | 181| ------- | -------------------- | 182| null | The value is null.| 183| number | The value can be any number.| 184| string | The value can be any string.| 185 186## Vertex 187 188Represents information about a vertex. **Vertex** is used only as the type in the return value ([Result](#result)) and cannot be customized. You can use [read](#read) to obtain its value. 189 190**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 191 192| Name | Type | Mandatory | Description | 193| ----------- | --------------------------- | --- | ------------ | 194| vid | string | Yes | Identifier of the vertex. The value cannot be customized. It is a globally unique identifier allocated by the system when a vertex is written by using [GraphStore.write](#write-1) or [Transaction.write](#write). | 195| labels | Array<string> | Yes | Labels of the vertex. The labels are specified in the GQL statement in [GraphStore.write](#write-1) or [Transaction.write](#write). Each label is case-insensitive, stored in uppercase format, and cannot exceed 128 bytes.| 196| properties | Record<string, [ValueType](#valuetype)> | Yes | Properties of the vertex. The key has a maximum length of 128 bytes, with a limit of 1024 entries. It is specified in the GQL statement in [GraphStore.write](#write-1) or [Transaction.write](#write). If the value is a string, it cannot exceed 64 x 1024 bytes. | 197 198## Edge 199 200Represents information about an edge. **Edge** is used only as the type in the return value ([Result](#result)) and cannot be customized. You can use [read](#read) to obtain its value. 201 202**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 203 204| Name | Type | Mandatory | Description | 205| ----------- | --------------------------- | --- | ------------ | 206| eid | string | Yes | Identifier of the edge. The value cannot be customized. It is a globally unique identifier allocated by the system when an edge is written by [GraphStore.write](#write-1) or [Transaction.write](#write). | 207| type | string | Yes | Type of the edge. It is specified in the GQL statement in [GraphStore.write](#write-1) or [Transaction.write](#write). It is case-insensitive, stored in uppercase format, and cannot exceed 128 bytes. | 208| startVid | string | Yes | Identifier of the start vertex. The value cannot be customized. It is a globally unique identifier allocated by the system when a vertex is written by [GraphStore.write](#write-1) or [Transaction.write](#write). | 209| endVid | string | Yes | Identifier of the end vertex. The value cannot be customized. It is a globally unique identifier allocated by the system when a vertex is written by [GraphStore.write](#write-1) or [Transaction.write](#write). | 210| properties | Record<string, [ValueType](#valuetype)> | Yes | Properties of the edge. The key has a maximum length of 128 bytes, with a limit of 1024 entries. It is specified in the GQL statement in [GraphStore.write](#write-1) or [Transaction.write](#write). If the value is a string, it cannot exceed 64 x 1024 bytes. | 211 212## PathSegment 213 214Represents information about a path segment, including the edge along with the start and end vertexes. **PathSegment** is used only as the ([Path.segments](#path)) type and cannot be customized. 215 216**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 217 218| Name | Type | Mandatory | Description | 219| ----------- | --------------------------- | --- | ------------ | 220| start | [Vertex](#vertex) | Yes | Start vertex of the path segment.| 221| end | [Vertex](#vertex) | Yes | End vertex of the path segment.| 222| edge | [Edge](#edge) | Yes | Edge of the path segment.| 223 224## Path 225 226Represents information about a path. **Path** is used only as the type in the return value ([Result](#result)) and cannot be customized. You can use [read](#read) to obtain its value. 227 228**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 229 230| Name | Type | Mandatory | Description | 231| ----------- | --------------------------- | --- | ------------ | 232| start | [Vertex](#vertex) | Yes | Start vertex of the path.| 233| end | [Vertex](#vertex) | Yes | End vertex of the path.| 234| length | number | Yes | Number of segments in the path, which cannot exceed 1024.| 235| segments | Array<[PathSegment](#pathsegment)> | Yes | Segments in the path.| 236 237## Result 238 239Represents the GQL statement execution result. 240 241**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 242 243| Name | Type | Mandatory | Description | 244| ----------- | --------------------------- | --- | ------------ | 245| records | Array<Record<string, Object>> | No | GQL statement execution result. It is left blank by default.<br>If there is an execution result, the object type varies depending on the GQL statement. It can be [Vertex](#vertex), [Edge](#edge), [Path](#path), or [ValueType](#valuetype).| 246 247 248## Transaction 249 250Provides APIs for transaction management in a graph store. 251 252Operations on different transaction objects are isolated. The graph stores use database-level locks. 253 254If [commit](#commit) or [rollback](#rollback) is not called after a transaction is written using [write](#write), calling [read](#read) or [write](#write) for another transaction instance or calling [GraphStore.read](#read-1) or [GraphStore.write](#write-1) with the **GraphStore** instance will return error 31300003. 255 256If [commit](#commit) or [rollback](#rollback) is not called after [read](#read) is called for a transaction, calling [write](#write) for another transaction instance or calling [GraphStore.write](#write-1) with the **GraphStore** instance will return error 31300003. 257 258Before calling any **Transaction** API, you must create a transaction instance by using [createTransaction](#createtransaction). 259 260### read 261 262read(gql: string): Promise<Result> 263 264Reads data, which includes the data written by using [GraphStore.write](#write-1), data written by another transaction using [write](#write) and committed by using [commit](#commit), and data written by the current transaction using [write](#write). 265 266**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 267 268**Parameters** 269 270| Name| Type | Mandatory| Description | 271| ------ | ----------------------------- | ---- | -------------------------- | 272| gql | string | Yes | GQL statement to execute, which cannot exceed 1024 x 1024 bytes.| 273 274**Return value** 275 276| Type | Description | 277| --------------------- | ------------------------------------------------- | 278| Promise<[Result](#result)> | Promise used to return the execution result if the operation is successful.| 279 280**Error codes** 281 282For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 283 284| **ID**| **Error Message** | 285|-----------| ------------------------------------------------------------ | 286| 202 | Permission verification failed, application which is not a system application uses system API. | 287| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 288| 31300000 | Inner error. | 289| 31300001 | Database corrupted. | 290| 31300002 | Already closed. | 291| 31300003 | The database is busy. | 292| 31300004 | The database is out of memory. | 293| 31300005 | The database is full. | 294| 31300006 | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. | 295| 31300007 | The graph name, vertex or edge type, or vertex or edge property is not defined. | 296| 31300008 | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. | 297| 31300009 | The GQL statement syntax error. | 298| 31300010 | The GQL statement semantic error. | 299 300**Example** 301 302```ts 303import { BusinessError } from '@kit.BasicServicesKit'; 304 305const QUERY_PATH_GQL = "MATCH path=(a:Person {name : \'name_1\'})-[]->{2, 2}(b:Person {name : \'name_3\'}) RETURN path;" 306if(transaction != undefined) { 307 (transaction as graphStore.Transaction).read(QUERY_PATH_GQL).then((result: graphStore.Result) => { 308 console.info('Read successfully'); 309 result.records?.forEach((data) => { 310 for (let item of Object.entries(data)) { 311 const key = item[0]; 312 const value = item[1]; 313 const path = value as graphStore.Path; 314 console.info(`key : ${key}, path.length : ${path.length}`); 315 } 316 }); 317 }).catch((err: BusinessError) => { 318 console.error(`Read failed, code is ${err.code}, message is ${err.message}`); 319 }) 320} 321``` 322 323### write 324 325write(gql: string): Promise<Result> 326 327Writes data. You can use this API to add, delete, and modify data. Currently, it cannot be used to modify the table structure (types or properties of [Vertex](#vertex) and [Edge](#edge)), or create or delete graphs or indexes. This API cannot be used to execute transaction operation statements, including **START TRANSACTION**, **COMMIT**, and **ROLLBACK**. 328 329**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 330 331**Parameters** 332 333| Name| Type | Mandatory| Description | 334| ------ | ----------------------------- | ---- | -------------------------- | 335| gql | string | Yes | GQL statement to execute, which cannot exceed 1024 x 1024 bytes.| 336 337**Return value** 338 339| Type | Description | 340| --------------------- | ------------------------------------------------- | 341| Promise<[Result](#result)> | Promise used to return the execution result if the operation is successful.| 342 343**Error codes** 344 345For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 346 347| **ID**| **Error Message** | 348|-----------| ------------------------------------------------------------ | 349| 202 | Permission verification failed, application which is not a system application uses system API. | 350| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 351| 31300000 | Inner error. | 352| 31300001 | Database corrupted. | 353| 31300002 | Already closed. | 354| 31300003 | The database is busy. | 355| 31300004 | The database is out of memory. | 356| 31300005 | The database is full. | 357| 31300006 | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. | 358| 31300007 | The graph name, vertex or edge type, or vertex or edge property is not defined. | 359| 31300008 | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. | 360| 31300009 | The GQL statement syntax error. | 361| 31300010 | The GQL statement semantic error. | 362| 31300012 | The number of graph names, vertex or edge types, or vertex or edge properties exceeds the limit. | 363| 31300013 | A conflicting constraint already exists. | 364 365**Example** 366 367```ts 368import { BusinessError } from '@kit.BasicServicesKit'; 369 370const INSERT_GQL = "INSERT (:Person {name: 'name_1', age: 11});" 371if(transaction != undefined) { 372 (transaction as graphStore.Transaction).write(INSERT_GQL).then(() => { 373 console.info('Write successfully'); 374 }).catch((err: BusinessError) => { 375 console.error(`Write failed, code is ${err.code}, message is ${err.message}`); 376 }) 377} 378``` 379 380### commit 381 382commit(): Promise<void> 383 384Commits the GQL statements executed in this transaction. After the commit, the transaction will be no longer available. 385 386**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 387 388**Return value** 389 390| Type | Description | 391| ------------------- | ------------------------- | 392| Promise<void> | Promise that returns no value.| 393 394**Error codes** 395 396For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 397 398| **ID**| **Error Message** | 399|-----------| ------------------------------------------------------------ | 400| 202 | Permission verification failed, application which is not a system application uses system API. | 401| 31300000 | Inner error. | 402| 31300001 | Database corrupted. | 403| 31300002 | Already closed. | 404| 31300003 | The database is busy. | 405| 31300004 | The database is out of memory. | 406| 31300005 | The database is full. | 407 408**Example** 409 410```ts 411import { BusinessError } from '@kit.BasicServicesKit'; 412 413if(transaction != undefined) { 414 (transaction as graphStore.Transaction).commit().then(() => { 415 console.info(`Commit successfully`); 416 }).catch ((err: BusinessError) => { 417 console.error(`Commit failed, code is ${err.code}, message is ${err.message}`); 418 }) 419} 420``` 421 422### rollback 423 424rollback(): Promise<void> 425 426Rolls back the GQL statements executed in this transaction. After the rollback, the transaction will be no longer available. 427 428**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 429 430**Return value** 431 432| Type | Description | 433| ------------------- | ------------------------- | 434| Promise<void> | Promise that returns no value.| 435 436**Error codes** 437 438For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 439 440| **ID**| **Error Message** | 441|-----------| ------------------------------------------------------------ | 442| 202 | Permission verification failed, application which is not a system application uses system API. | 443| 31300000 | Inner error. | 444| 31300001 | Database corrupted. | 445| 31300002 | Already closed. | 446| 31300003 | The database is busy. | 447| 31300004 | The database is out of memory. | 448| 31300005 | The database is full. | 449 450**Example** 451 452```ts 453import { BusinessError } from '@kit.BasicServicesKit'; 454 455if(transaction != undefined) { 456 (transaction as graphStore.Transaction).rollback().then(() => { 457 console.info(`Rollback successfully`); 458 }).catch ((err: BusinessError) => { 459 console.error(`Rollback failed, code is ${err.code}, message is ${err.message}`); 460 }) 461} 462``` 463 464 465## GraphStore 466 467Provides APIs for managing the data in a graph store. 468 469Before calling any **GraphStore** API, you must create a **GraphStore** instance by using [getStore](#graphstoregetstore). 470 471### read 472 473read(gql: string): Promise<Result> 474 475Reads data, which includes the data written by using [write](#write-1) and the data written by using [Transaction.write](#write) and committed by [Transaction.commit](#commit) in all transactions. 476 477**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 478 479**Parameters** 480 481| Name| Type | Mandatory| Description | 482| ------ | ----------------------------- | ---- | -------------------------- | 483| gql | string | Yes | GQL statement to execute, which cannot exceed 1024 x 1024 bytes.| 484 485**Return value** 486 487| Type | Description | 488| --------------------- | ------------------------------------------------- | 489| Promise<[Result](#result)> | Promise used to return the execution result if the operation is successful.| 490 491**Error codes** 492 493For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 494 495| **ID**| **Error Message** | 496|-----------| ------------------------------------------------------------ | 497| 202 | Permission verification failed, application which is not a system application uses system API. | 498| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 499| 31300000 | Inner error. | 500| 31300001 | Database corrupted. | 501| 31300002 | Already closed. | 502| 31300003 | The database is busy. | 503| 31300004 | The database is out of memory. | 504| 31300005 | The database is full. | 505| 31300006 | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. | 506| 31300007 | The graph name, vertex or edge type, or vertex or edge property is not defined. | 507| 31300008 | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. | 508| 31300009 | The GQL statement syntax error. | 509| 31300010 | The GQL statement semantic error. | 510 511**Example** 512 513```ts 514import { BusinessError } from '@kit.BasicServicesKit'; 515 516const QUERY_PATH_GQL = "MATCH path=(a:Person {name : \'name_1\'})-[]->{2, 2}(b:Person {name : \'name_3\'}) RETURN path;" 517if(store != null) { 518 (store as graphStore.GraphStore).read(QUERY_PATH_GQL).then((result: graphStore.Result) => { 519 console.info('Read successfully'); 520 result.records?.forEach((data) => { 521 for (let item of Object.entries(data)) { 522 const key = item[0]; 523 const value = item[1]; 524 const path = value as graphStore.Path; 525 console.info(`key : ${key}, path.length : ${path.length}`); 526 } 527 }); 528 }).catch((err: BusinessError) => { 529 console.error(`Read failed, code is ${err.code}, message is ${err.message}`); 530 }) 531} 532``` 533 534### write 535 536write(gql: string): Promise<Result> 537 538Writes data. You can use this API to create or delete a graph, and add, delete, or modify data. Currently, the table structure (types or properties of [Vertex](#vertex) and [Edge](#edge)) cannot be modified. This API cannot be used to execute transaction operation statements, including **START TRANSACTION**, **COMMIT**, and **ROLLBACK**. 539 540**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 541 542**Parameters** 543 544| Name| Type | Mandatory| Description | 545| ------ | ----------------------------- | ---- | -------------------------- | 546| gql | string | Yes | GQL statement to execute, which cannot exceed 1024 x 1024 bytes.| 547 548**Return value** 549 550| Type | Description | 551| --------------------- | ------------------------------------------------- | 552| Promise<[Result](#result)> | Promise used to return the execution result if the operation is successful.| 553 554**Error codes** 555 556For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 557 558| **ID**| **Error Message** | 559|-----------| ------------------------------------------------------------ | 560| 202 | Permission verification failed, application which is not a system application uses system API. | 561| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 562| 31300000 | Inner error. | 563| 31300001 | Database corrupted. | 564| 31300002 | Already closed. | 565| 31300003 | The database is busy. | 566| 31300004 | The database is out of memory. | 567| 31300005 | The database is full. | 568| 31300006 | A duplicate graph name, vertex or edge type, or vertex or edge property name exists. | 569| 31300007 | The graph name, vertex or edge type, or vertex or edge property is not defined. | 570| 31300008 | The graph name, vertex or edge type, or vertex or edge property name does not conform to constraints. | 571| 31300009 | The GQL statement syntax error. | 572| 31300010 | The GQL statement semantic error. | 573| 31300012 | The number of graph names, vertex or edge types, or vertex or edge properties exceeds the limit. | 574| 31300013 | A conflicting constraint already exists. | 575 576**Example** 577 578```ts 579import { BusinessError } from '@kit.BasicServicesKit'; 580 581const CREATE_GRAPH_GQL = "CREATE GRAPH test { (person:Person {name STRING, age INT}),(person)-[:Friend {year INT}]->(person) };" 582if(store != null) { 583 (store as graphStore.GraphStore).write(CREATE_GRAPH_GQL).then(() => { 584 console.info('Write successfully'); 585 }).catch((err: BusinessError) => { 586 console.error(`Write failed, code is ${err.code}, message is ${err.message}`); 587 }) 588} 589``` 590 591### createTransaction 592 593createTransaction(): Promise<Transaction> 594 595Creates a transaction instance. Currently, a maximum of four transaction instances can be created at a time in a graph store. 596 597**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 598 599**Return value** 600 601| Type | Description | 602| ------------------- | ------------------------- | 603| Promise<[Transaction](#transaction)> | Promise used to return the created transaction instance.| 604 605**Error codes** 606 607For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 608 609| **ID**| **Error Message** | 610|-----------| ------------------------------------------------------------ | 611| 202 | Permission verification failed, application which is not a system application uses system API. | 612| 31300000 | Inner error. | 613| 31300001 | Database corrupted. | 614| 31300002 | Already closed. | 615| 31300003 | The database is busy. | 616| 31300004 | The database is out of memory. | 617| 31300005 | The database is full. | 618 619**Example** 620 621```ts 622import { BusinessError } from '@kit.BasicServicesKit'; 623 624let transaction: graphStore.Transaction | undefined = undefined; 625 626if(store != undefined) { 627 (store as graphStore.GraphStore).createTransaction().then((trans: graphStore.Transaction) => { 628 transaction = trans; 629 console.info('createTransaction successfully'); 630 }).catch((err: BusinessError) => { 631 console.error(`createTransaction failed, code is ${err.code}, message is ${err.message}`); 632 }) 633} 634``` 635 636### close 637 638close(): Promise<void> 639 640Closes this graph store. After this API is called, uncommitted transactions will be rolled back. 641 642**System capability**: SystemCapability.DistributedDataManager.DataIntelligence.Core 643 644**Return value** 645 646| Type | Description | 647| ------------------- | ------------------------- | 648| Promise<void> | Promise that returns no value.| 649 650**Error codes** 651 652For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Graph Store Error Codes](errorcode-data-gdb.md). 653 654| **ID**| **Error Message** | 655|-----------| ------------------------------------------------------------ | 656| 202 | Permission verification failed, application which is not a system application uses system API. | 657| 31300000 | Inner error. | 658 659**Example** 660 661```ts 662import { BusinessError } from '@kit.BasicServicesKit'; 663 664if(store != null) { 665 (store as graphStore.GraphStore).close().then(() => { 666 console.info(`Close successfully`); 667 }).catch ((err: BusinessError) => { 668 console.error(`Close failed, code is ${err.code}, message is ${err.message}`); 669 }) 670} 671``` 672