数据库读取和写入操作的结果必须先进行适当处理,然后才能用于应用程序。
返回Response
有些方法(如gql()
和uql()
)返回一个Response
对象。为在应用中使用其中包含的数据,首先要从Response
对象提取DataItem
,再将DataItem
转换成一个合适的驱动数据类。
一个Response
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
aliases |
Alias [] |
结果别名列表;每个Alias 包括属性name 和type |
items |
[key: string]: DataItem |
一个对象,键是别名名称,值是别名代表的数据 |
explainPlan |
ExplainPlan |
执行计划 |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
提取DataItem
使用get()
或alias()
方法从一个Response
对象提取DataItem
。
一个DataItem
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
alias |
string | 别名名称 |
type |
ResultType |
结果类型 |
entities |
any | 结果数据 |
get()
通过别名索引获取数据。
参数
index: number
:别名索引。
返回值
DataItem
:返回的数据。
const response = await conn.gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3");
console.log(response.get(0).alias);
console.log(response.get(0).type);
console.log(response.get(0).entities);
GQL语句返回n
和e
两个别名;get()
方法获取与索引为0的别名n
对应的DataItem
。
n
2
[Node{uuid='72059793061183504', id='ULTIPA800000000000003B', schema='account', values={industry='Publishing', name='Velox', gender='female', year=1976}}, Node{uuid='648520545364606993', id='ULTIPA800000000000003E', schema='account', values={industry='Food&Beverage', name='Claire', gender='female', year=1989}}, Node{uuid='720578139402534937', id='ULTIPA8000000000000050', schema='account', values={industry='Education', name='Holly', gender='male', year=2000}}]
alias()
通过别名名称获取数据。
参数
alias: string
:别名名称。
返回值
DataItem
:返回的数据。
const response = await conn.gql("MATCH (n)-[e]->() RETURN n, e LIMIT 3");
console.log(response.alias("e").alias);
console.log(response.alias("e").type);
console.log(response.alias("e").entities);
GQL语句返回n
和e
两个别名;alias()
方法获取别名e
对应的DataItem
。
e
3
[Edge{uuid=139, fromUuid=6269012880322985990, toUuid=-7998395137233256453, from=ULTIPA800000000000000E, to=ULTIPA800000000000000D, schema=agree, values={targetPost=905, timestamp=1572662452, datetime=2018-10-14T06:27:42}}, Edge{uuid=378, fromUuid=72059793061183493, toUuid=8214567919347040275, from=ULTIPA800000000000003B, to=ULTIPA800000000000000F, schema=follow, values={}}, Edge{uuid=531, fromUuid=72059793061183493, toUuid=4827864298099310661, from=ULTIPA800000000000003B, to=ULTIPA80000000000003F4, schema=wishlist, values={toUuid=1012, uuid=1368, fromUuid=59, timestamp=Sat Mar 23 17:09:12 CST 2019, datetime=2019-03-23T17:09:12}}]
转换DataItem
使用一个as<DataStructure>()
方法将DataItem.entities
转换成对应的驱动数据类。
例如,以下请求中的GQL语句从图中获取三个点,asNodes()
方法将与别名n
对应的DataItem
转换成一个Node
对象列表:
const response = await conn.gql("MATCH (n) RETURN n LIMIT 3");
const nodeList = response.alias("n").asNodes();
for (const node of nodeList) {
console.log(node.id)
}
以下是 DataItem
所有的转换方法。请留意每种方法适用的DataItem.type
和DataItem.alias
。
方法 |
DataItem.type |
DataItem.alias |
返回 |
描述 |
---|---|---|---|---|
asNodes() |
NODE | Any | Node [] |
转换成Node 对象列表 |
asFirstNode() |
NODE | Any | Node |
将返回的第一个结果数据转换成一个Node 对象 |
asEdges() |
EDGE | Any | Edge [] |
转换成Edge 对象列表 |
asFirstEdge() |
EDGE | Any | Edge |
将返回的第一个结果数据转换成一个Edge 对象 |
asGraph() |
GRAPH | Any | Graph |
转换成一个Graph 对象 |
asGraphSets() |
TABLE | _graph |
GraphSet [] |
转换成GraphSet 对象列表 |
asSchemas() |
TABLE | _nodeSchema , _edgeSchema |
Schema [] |
转换成Schema 对象列表 |
asProperties() |
TABLE | _nodeProperty , _edgeProperty |
Property [] |
转换成Property 对象列表 |
asAttr() |
ATTR | Any | Attr |
转换成一个Attr 对象 |
asTable() |
TABLE | Any | Table |
转换成一个Table 对象 |
asHDCGraphs() |
TABLE | _hdcGraphList |
HDCGraph [] |
转换成HDCGraph 对象列表 |
asAlgos() |
TABLE | _algoList |
Algo [] |
转换成Algo 对象列表 |
asProjections() |
TABLE | _projectionList |
Projection [] |
转换成Projection 对象列表 |
asIndexes() |
TABLE | _nodeIndex , _edgeIndex , _nodeFulltext , _edgeFulltext |
Index [] |
转换成Index 对象列表 |
asPrivilieges() |
TABLE | _privilege |
Priviliege [] |
转换成Priviliege 对象列表 |
asPolicies() |
TABLE | _policy |
Policy [] |
转换成Policy 对象列表 |
asUsers() |
TABLE | _user |
User [] |
转换成User 对象列表 |
asProcesses() |
TABLE | _top |
Process [] |
转换成Process 对象列表 |
asJobs() |
TABLE | _job |
Job [] |
转换成Job 对象列表 |
返回ResponseWithExistCheck
有些方法(如createGraphIfNotExist()
)返回一个ResponseWithExistCheck
对象。
一个ResponseWithExistCheck
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
exist |
boolean | 要创建的对象是否已经存在 |
response |
Response |
主要请求结果 |
返回InsertResponse
有些数据插入方法(如insertNodesBatchBySchema()
和insertNodesBatchAuto()
)返回一个InsertResponse
对象。
一个InsertResponse
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
ids |
string[] | 插入的点_id 列表;请留意,InsertRequestConfig.silent 为true 时列表为空 |
uuids |
string[] | 插入的边_uuid 列表;请留意,InsertRequestConfig.silent 为true 时列表为空 |
errorItems |
Map<number,InsertErrorCode > |
错误信息映射,键是发生错误的点或边的顺序索引,值是错误代码 |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
返回JobResponse
有些方法(如createFulltext()
)返回一个JobResponse
对象。
一个JobResponse
对象有以下属性:
属性 |
类型 |
描述 |
---|---|---|
jobId |
string | 执行的作业ID |
status |
Status |
执行状态,包括属性code 和message |
statistics |
Statistics |
执行的统计信息,包括属性nodeAffected 、edgeAffected 、totalCost 和engineCost |
返回驱动数据类
一些方法直接返回驱动数据类的一个实例或一个列表,可直接使用。例如:
getGraph()
方法返回一个GraphSet
对象。showGraph()
方法返回GraphSet[]
。