The results from database read and write operations must be properly processed before being utilized in your application.
Return a Response
Methods such as gql()
and uql()
return a Response
object. To serve the application, you need to first extract the DataItem
from the Response
object, and then transform it into an appropriate driver data class.
A Response
object includes the following attributes:
Attribute |
Type |
Description |
---|---|---|
aliases |
Alias [] |
The list of result aliases; each Alias includes attributes name and type . |
items |
[key: string]: DataItem |
An object where each key is an alias name and each value is the corresponding data item. |
explainPlan |
ExplainPlan |
The execution plan. |
status |
Status |
The status of the execution, inlcuding attributes code and message . |
statistics |
Statistics |
Statistics related to the execution, including attributes nodeAffected , edgeAffected , totalCost , and engineCost . |
Extract DataItem
To extract DataItem
from a Response
object, use the get()
or alias()
method.
A DataItem
object includes the following attributes:
Attribute |
Type |
Description |
---|---|---|
alias |
string | The alias name. |
type |
ResultType |
The type of the results. |
entities |
any | The result entities. |
get()
Retrieves data by the alias index.
Parameters
index: number
: Index of the alias.
Returns
DataItem
: The returned data.
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);
The GQL query returns two aliases (n
, e
), and the get()
method gets the DataItem
of the alias n
at index 0.
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()
Retrieves data by the alias name.
Parameters
alias: string
: Name of the alias.
Returns
DataItem
: The returned data.
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);
The GQL query returns two aliases (n
, e
), and the alias()
method gets DataItem
of the alias e
.
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}}]
Transform DataItem
You should use a as<DataStructure>()
method to convert the DataItem.entities
into the corresponding driver data class.
For example, the GQL query contained in the request below retrieves 3 nodes from the graph, the asNodes()
method is used to convert the DataItem
associated with the alias n
into a list of Node
objects:
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)
}
The following lists all the transformation methods available on DataItem
. Please note the applicable DataItem.type
and DataItem.alias
of each method.
Method |
DataItem.type |
DataItem.alias |
Returns |
Description |
---|---|---|---|---|
asNodes() |
NODE | Any | Node [] |
Converts to a list of Node objects. |
asFirstNode() |
NODE | Any | Node |
Converts the first returned entity to a Node object. |
asEdges() |
EDGE | Any | Edge [] |
Converts to a list of Edge objects. |
asFirstEdge() |
EDGE | Any | Edge |
Converts the first returned entity to an Edge object. |
asGraph() |
GRAPH | Any | Graph |
Converts to a Graph object. |
asGraphSets() |
TABLE | _graph |
GraphSet [] |
Converts to a list of GraphSet objects. |
asSchemas() |
TABLE | _nodeSchema , _edgeSchema |
Schema [] |
Converts to a list of Schema objects. |
asProperties() |
TABLE | _nodeProperty , _edgeProperty |
Property [] |
Converts to a list of Property objects. |
asAttr() |
ATTR | Any | Attr |
Converts to an Attr object. |
asTable() |
TABLE | Any | Table |
Converts to a Table object. |
asHDCGraphs() |
TABLE | _hdcGraphList |
HDCGraph [] |
Converts to a list of HDCGraph objects. |
asAlgos() |
TABLE | _algoList |
Algo [] |
Converts to a list of Algo objects. |
asProjections() |
TABLE | _projectionList |
Projection [] |
Converts to a list of Projection objects. |
asIndexes() |
TABLE | _nodeIndex , _edgeIndex , _nodeFulltext , _edgeFulltext |
Index [] |
Converts to a list of Index objects. |
asPrivilieges() |
TABLE | _privilege |
Priviliege [] |
Converts to a list of Priviliege objects. |
asPolicies() |
TABLE | _policy |
Policy [] |
Converts to a list of Policy objects. |
asUsers() |
TABLE | _user |
User [] |
Converts to a list of User objects. |
asProcesses() |
TABLE | _top |
Process [] |
Converts to a list of Process objects. |
asJobs() |
TABLE | _job |
Job [] |
Converts to a list of Job objects. |
Return a ResponseWithExistCheck
Methods such as createGraphIfNotExist()
return a ResponseWithExistCheck
object.
A ResponseWithExistCheck
object includes the following attributes:
Attribute |
Type |
Description |
---|---|---|
exist |
boolean | Whether the object to create already exist. |
response |
Response |
Main response of the method. |
Return an InsertResponse
Some data insertion methods such as insertNodesBatchBySchema()
and insertNodesBatchAuto()
return an InsertResponse
object.
An InsertResponse
object includes the following attributes:
Attribute |
Type |
Description |
---|---|---|
ids |
string[] | The list of _id s of the inserted nodes; note that the list is empty when InsertRequestConfig.silent is true . |
uuids |
string[] | The list of _uuid s of the inserted edges; note that the list is empty when InsertRequestConfig.silent is true . |
errorItems |
Map<number,InsertErrorCode > |
A map containing error details, where each key represents the index of a node or edge with an error, and the value is the error code. |
status |
Status |
The status of the execution, inlcuding attributes code and message . |
statistics |
Statistics |
Statistics releated to the execution, including attributes nodeAffected , edgeAffected , totalCost , and engineCost . |
Return a JobResponse
Methods such as createFulltext()
return a JobResponse
object.
A JobResponse
object includes the following attributes:
Attribute |
Type |
Description |
---|---|---|
jobId |
string | ID of the job of the execution. |
status |
Status |
The status of the execution, inlcuding attributes code and message . |
statistics |
Statistics |
Statistics releated to the execution, including attributes nodeAffected , edgeAffected , totalCost , and engineCost . |
Return Driver Data Classes
Some methods return either a single instance or a list of driver data class, which can be used directly. For example:
- The
getGraph()
method returns a singleGraphSet
object. - The
showGraph()
method returnsGraphSet[]
.