This section introduces methods for managing graphs in the database.
Retrieves all graphs from the database.
Parameters
config?: RequestConfig: Request configuration.Returns
GraphSet[]: The list of retrieved graphs.TypeScript// Retrieves all graphs const graphs = await driver.showGraph(); graphs.forEach((graph) => console.log(graph.name));
OutputDisplay_Ad_Click ERP_DATA2 wikiKG
Retrieves a specified graph from the database.
Parameters
graphName: string: Name of the graph.config?: RequestConfig: Request configuration.Returns
GraphSet: The retrieved graph.TypeScript// Retrieves the graph named 'miniCircle' const graph = await driver.getGraph("miniCircle"); console.log(graph);
OutputGraphSet { id: '9', name: 'miniCircle', totalNodes: '97', totalEdges: '632', shards: [ '1' ], partitionBy: 'CityHash64', status: 'NORMAL', description: '', slotNum: 256 }
Checks the existence of a specified graph in the database.
Parameters
graphName: string: Name of the graph.config?: RequestConfig: Request configuration.Returns
boolean: Check result.TypeScript// Checks the existence of a graph named 'miniCircle' const graphName = "miniCircle"; const response = await driver.hasGraph(graphName); console.log("Graph", graphName, "exists:", response);
OutputGraph miniCircle exists: true
Creates a graph in the database.
Parameters
graphSet: GraphSet: The graph to be created; the filed name is mandatory, shards, partitionBy and description are optional.config?: RequestConfig: Request configuration.Returns
Response: Response of the request.TypeScript// Creates a graph const graph: GraphSet = { name: "myGraph", shards: ["1"], partitionBy: "Crc32", description: "My first graph" }; const response = await driver.createGraph(graph) console.log(response.status?.message);
OutputSUCCESS
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 filed name is mandatory, shards, partitionBy and description are optional.config?: RequestConfig: Request configuration.Returns
ResponseWithExistCheck: Response of the request.TypeScript// Creates a graph with existence check const graph: GraphSet = { name: "myGraph", shards: ["1"], partitionBy: "Crc32", description: "My first graph" }; const result = await driver.createGraphIfNotExist(graph); console.log("Graph already exists:", result.exist); if (result.response.status?.code !== 0) { console.log("Error message:", result.response.status?.message); } else { if (result.response.statistics?.totalCost === 0) { console.log("New graph created: No"); } else { console.log("New graph created: Yes"); } };
OutputGraph already exists: true New graph created: No
Alters the name and description of a graph in the database.
Parameters
graphName: string: Name of the graph.alterGraphset: GraphSet: A GraphSet object used to set new name and/or description for the graph.config?: RequestConfig: Request configuration.Returns
Response: Response of the request.TypeScript// Alters the name and description of the graph 'myGraph' const newGraphInfo: GraphSet = { name: "newGraph", description: "a new graph" }; const response = await driver.alterGraph("myGraph", newGraphInfo); console.log(response.status?.message);
OutputSUCCESS
Deletes a specified graph from the database.
Parameters
graphName: string: Name of the graph.config?: RequestConfig: Request configuration.Returns
Response: Response of the request.TypeScript// Drops a graph const response = await driver.dropGraph("myGraph"); console.log(response.status?.message);
OutputSUCCESS
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 filed graphName is mandatory, schemaName and dbType are optional.config?: RequestConfig: Request configuration.Returns
Response: Response of the request.TypeScript// Truncates User nodes in 'myGraph' const param1: TruncateParams = { graphName: "myGraph", schemaName: "User", dbType: DBType.DBNODE } const response1 = await driver.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 driver.truncate(param2); console.log(response2.status?.message); // Truncates 'myGraph' const param3: TruncateParams = { graphName: "myGraph" } const response3 = await driver.truncate(param3); console.log(response3.status?.message);
OutputSUCCESS SUCCESS SUCCESS
Clears invalid and redundant data for a graph. Valid data will not be affected.
Parameters
graphName: string: Name of the graph.config?: RequestConfig: Request configuration.Returns
JobResponse: Response of the request.TypeScript// Compacts the graph 'miniCircle' const requestConfig: RequestConfig = { graph: "miniCircle" }; const jobResponse = await driver.compact("miniCircle", requestConfig); console.log("Start compacting:", jobResponse.status?.message) const jobID = jobResponse.jobId; await new Promise(resolve => setTimeout(resolve, 3000)) const jobs = await driver.showJob(jobID, requestConfig); for (const job of jobs) { console.log("Compact graph job:", `${job.id} - ${job.status}`); }
OutputStart compacting: SUCCESS Compact graph job: 4 - FINISHED Compact graph job: 4_1 - FINISHED
Example.tsimport { UltipaDriver } from "@ultipa-graph/ultipa-driver"; import type { ULTIPA } from "@ultipa-graph/ultipa-driver/dist/types/index.js"; import { GraphSet } from "@ultipa-graph/ultipa-driver/dist/types/types.js"; let sdkUsage = async () => { const ultipaConfig: ULTIPA.UltipaConfig = { // URI example: hosts: ["xxxx.us-east-1.cloud.ultipa.com:60010"] hosts: ["10.xx.xx.xx:60010"], username: "<username>", password: "<password>" }; const driver = new UltipaDriver(ultipaConfig); // Creates a graph const graph: GraphSet = { name: "myGraph1", shards: ["1"], partitionBy: "Crc32", description: "My first graph" }; const response = await driver.createGraph(graph) console.log(response.status?.message); }; sdkUsage().catch(console.error);