This section introduces methods for managing graphs in the database.
showGraph()
Retrieves all graphs from the database.
Parameters
config: RequestConfig
(Optional): Request configuration.
Returns
GraphSet[]
: The list of retrieved graphs.
// Retrieves all graphs and prints the names of those with over 2000 edges
const graphs = await conn.showGraph();
graphs
.filter((graph) => Number(graph.totalEdges) > 2000)
.forEach((graph) => console.log(graph.name));
Display_Ad_Click
ERP_DATA2
wikiKG
getGraph()
Retrieves a specified graph from the database.
Parameters
graphName: string
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
GraphSet
: The retrieved graph.
// Retrieves the graph named 'miniCircle'
const graph = await conn.getGraph("miniCircle");
console.log(graph);
{"id": "444", "name": "miniCircle", "totalNodes": 304, "totalEdges": 1961, "shards": ["1"], "partitionBy": "CityHash64", "status": "NORMAL", "description": "", "slotNum": 256}
hasGraph()
Checks the existence of a specified graph in the database.
Parameters
graphName: string
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
boolean
: Check result.
// Checks the existence of a graph named 'miniCircle'
const response = await conn.hasGraph("miniCircle");
console.log("The graph exists:", response);
The graph exists: true
createGraph()
Creates a graph in the database.
Parameters
graphSet: GraphSet
: The graph to be created; the attributename
is mandatory,shards
,partitionBy
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// Creates a graph
const graph: GraphSet = {
name: "testNodejsSDK",
shards: ["1"],
partitionBy: "Crc32",
description: "testNodejsSDK desc"
};
const response = await conn.createGraph(graph)
console.log(response.status?.message);
SUCCESS
createGraphIfNotExist()
Creates a graph in the database and returns whether a graph with the same name already exists.
Parameters
graphSet: GraphSet
: The graph to be created; the attributename
is mandatory,shards
,partitionBy
anddescription
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
ResponseWithExistCheck
: Response of the request.
const graph:GraphSet = {
name:"testNodejsSDK",
shards:["1"],
partitionBy:"Crc32",
description:"testNodejsSDK desc"
}
const result = await conn.createGraphIfNotExist(graph)
console.log("Does the graph already exist?", result.exist)
if(result.response.statistics?.totalCost == 0){
console.log("Graph creation status: No response")
} else {
console.log("Graph creation status:", result.response.status?.message)
}
await new Promise((resolve) => setTimeout(resolve, 3000));
console.log("----- Creates the graph again -----")
const result_1 = await conn.createGraphIfNotExist(graph)
console.log("Does the graph already exist?", result_1.exist)
if(result_1.response.statistics?.totalCost == 0){
console.log("Graph creation status: No response")
} else {
console.log("Graph creation status:", result_1.response.status?.message)
}
Does the graph already exist? false
Graph creation status: SUCCESS
----- Creates the graph again -----
Does the graph already exist? true
Graph creation status: No response
alterGraph()
Alters the name and description of a graph in the database.
Parameters
graphName: string
: Name of the graph.alterGraphset: GraphSet
: AGraphSet
object used to set the newname
and/ordescription
for the graph.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// Alters the name and description of the graph 'testNodeJSSDK'
const newGraphInfo: GraphSet = {
name: "newGraph",
description: "a new graph",
};
const response = await conn.alterGraph("testNodejsSDK", newGraphInfo);
console.log(response.status?.message);
SUCCESS
dropGraph()
Deletes a specified graph from the database.
Parameters
graphName: string
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Reponse of the request.
// Drops the graph 'testNodeJSSDK'
const response = await conn.dropGraph("testNodejsSDK");
console.log(response.status?.message);
SUCCESS
truncate()
Truncates (Deletes) the specified nodes or edges in a graph or truncates the entire graph. Note that truncating nodes will cause the deletion of edges attached to those affected nodes. The truncating operation retains the definition of schemas and properties in the graph.
Parameters
params: TruncateParams
: The truncate parameters; the attributegraphName
is mandatory,schemaName
anddbType
are optional.config: RequestConfig
(Optional): Request configuration.
Returns
Response
: Response of the request.
// Truncates User nodes in 'myGraph'
const param1:TruncateParams = {graphName:"myGraph",schemaName:"User",dbType:DBType.DBNODE}
const response1 = await conn.truncate(param1);
console.log(response1.status?.message);
// Truncates all edges in the 'myGraph'
const param2:TruncateParams = {graphName:"myGraph",schemaName:"*",dbType:DBType.DBEDGE}
const response2 = await conn.truncate(param2);
console.log(response2.status?.message);
// Truncates 'myGraph'
const param3:TruncateParams = {graphName:"myGraph"}
const response3 = await conn.truncate(param3);
console.log(response3.status?.message);
SUCCESS
SUCCESS
SUCCESS
compact()
Clears invalid and redundant data for a graph. Valid data will not be affected.
Parameters
graphName: string
: Name of the graph.config: RequestConfig
(Optional): Request configuration.
Returns
JobResponse
: Response of the request.
// Compacts the graph 'miniCircle'
const requestConfig: RequestConfig = { graph: "miniCircle" };
const response = await conn.compact("miniCircle");
const jobID = response.jobId;
await new Promise(resolve => setTimeout(resolve, 2000))
const jobs = await conn.showJob(jobID, requestConfig);
for (const job of jobs) {
console.log(`${job.id} - ${job.status}`);
}
29 - FINISHED
29_1 - FINISHED
29_2 - FINISHED
29_3 - FINISHED
Full Example
import { ConnectionPool } from "@ultipa-graph/ultipa-driver";
import { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types";
import { GraphSet } from "@ultipa-graph/ultipa-driver/dist/types/types";
let sdkUsage = async () => {
// URI example: ultipaConfig.hosts: ["mqj4zouys.us-east-1.cloud.ultipa.com:60010"]
const ultipaConfig: ULTIPA.UltipaConfig = {
hosts: ["192.168.1.85:60061", "192.168.1.87:60061", "192.168.1.88:60061"],
username: "<username>",
password: "<password>"
};
const conn = await new ConnectionPool(
ultipaConfig.hosts,
ultipaConfig.username,
ultipaConfig.password
).getActive();
const isSuccess = await conn.test();
console.log(`Connection succeeds: ${isSuccess}`);
// Creates a graph
const graph: GraphSet = {
name: "testNodejsSDK",
shards: ["1"],
partitionBy: "Crc32",
description: "testNodejsSDK desc",
};
const response = await conn.createGraph(graph);
console.log(response.status?.message);
};
sdkUsage().catch(console.error);